|
| 1 | +# SimpleMaster Columns (English) |
| 2 | + |
| 3 | +## Overview |
| 4 | +SimpleMaster columns are defined with `def_column`. At load time, type conversion, |
| 5 | +cache helpers, and accessor methods are generated. |
| 6 | +The behavior depends on `type` and DSL options. |
| 7 | + |
| 8 | +```ruby |
| 9 | +class Weapon < ApplicationMaster |
| 10 | + def_column :id |
| 11 | + def_column :name, type: :string |
| 12 | + def_column :attack, type: :float |
| 13 | + def_column :rarity |
| 14 | + |
| 15 | + enum :rarity, { common: 0, rare: 1, epic: 2 } |
| 16 | +end |
| 17 | +``` |
| 18 | + |
| 19 | +## Common options |
| 20 | +### `type:` |
| 21 | +- Example: `def_column :attack, type: :float` |
| 22 | +- See the column type list below. |
| 23 | + |
| 24 | +### `group_key:` |
| 25 | +- Example: `def_column :lv, type: :integer, group_key: true` |
| 26 | +- You can also use `group_key :lv`. |
| 27 | + |
| 28 | +### `db_column_name:` |
| 29 | +- Use when the DB column name differs. |
| 30 | +- Example: `def_column :start_at, type: :time, db_column_name: :start_time` |
| 31 | + |
| 32 | +### `globalize:` |
| 33 | +- Adds locale-aware values using `I18n.locale`. |
| 34 | +- Example: `def_column :name, globalize: true` |
| 35 | +- You can also use `globalize :name`. |
| 36 | +- Translation values live in `@_globalized_name` like `{ en: "Storm Edge", ja: "..." }`. |
| 37 | +- Not supported on `id` / `enum` / `bitmask` / `sti` / `polymorphic_type`. |
| 38 | +- Cannot be used with `group_key`. |
| 39 | + |
| 40 | +## Column types |
| 41 | + |
| 42 | +### id (IdColumn) |
| 43 | +**Usage** |
| 44 | +```ruby |
| 45 | +def_column :id |
| 46 | +``` |
| 47 | +**Behavior** |
| 48 | +- Converts to `to_i` on assignment. |
| 49 | +- In tests, updates `id_hash` when changed. |
| 50 | + |
| 51 | +### integer |
| 52 | +**Usage** |
| 53 | +```ruby |
| 54 | +def_column :lv, type: :integer |
| 55 | +``` |
| 56 | +**Behavior** |
| 57 | +- Converts to `to_i` on assignment (empty string becomes `nil`). |
| 58 | + |
| 59 | +### float |
| 60 | +**Usage** |
| 61 | +```ruby |
| 62 | +def_column :attack, type: :float |
| 63 | +``` |
| 64 | +**Behavior** |
| 65 | +- Converts to `to_f` on assignment (empty string becomes `nil`). |
| 66 | + |
| 67 | +### string |
| 68 | +**Usage** |
| 69 | +```ruby |
| 70 | +def_column :name, type: :string |
| 71 | +``` |
| 72 | +**Behavior** |
| 73 | +- Converts to `to_s` on assignment. |
| 74 | +- Values are cached to reuse identical objects (`object_cache`). |
| 75 | + |
| 76 | +### symbol |
| 77 | +**Usage** |
| 78 | +```ruby |
| 79 | +def_column :kind, type: :symbol |
| 80 | +``` |
| 81 | +**Behavior** |
| 82 | +- Converts to `to_s` + `to_sym` on assignment. |
| 83 | +- SQL/CSV output uses a string. |
| 84 | + |
| 85 | +### boolean |
| 86 | +**Usage** |
| 87 | +```ruby |
| 88 | +def_column :is_boss, type: :boolean |
| 89 | +``` |
| 90 | +**Behavior** |
| 91 | +- Integers use 0/1, strings accept "true" or "1". |
| 92 | +- Adds a `name?` predicate. |
| 93 | +- SQL/CSV output is 0/1. |
| 94 | + |
| 95 | +### json |
| 96 | +**Usage** |
| 97 | +```ruby |
| 98 | +def_column :info, type: :json |
| 99 | +``` |
| 100 | +**Options** |
| 101 | +- `symbolize_names: true` converts JSON keys to symbols. |
| 102 | + |
| 103 | +**Behavior** |
| 104 | +- Parses string values with `JSON.parse`. |
| 105 | +- SQL/CSV output uses `JSON.generate`. |
| 106 | +- Non-string assignments are not transformed by `symbolize_names`. |
| 107 | + |
| 108 | +### time |
| 109 | +**Usage** |
| 110 | +```ruby |
| 111 | +def_column :start_at, type: :time |
| 112 | +``` |
| 113 | +**Options** |
| 114 | +- `db_type: :time` outputs `HH:MM:SS` only. |
| 115 | + |
| 116 | +**Behavior** |
| 117 | +- Parses strings with `Date._parse` into `Time`. |
| 118 | +- Sub-seconds are truncated. |
| 119 | + |
| 120 | +### enum |
| 121 | +**Usage** |
| 122 | +```ruby |
| 123 | +def_column :rarity, enum: { common: 0, rare: 1, epic: 2 } |
| 124 | +# or |
| 125 | +def_column :rarity |
| 126 | +enum :rarity, { common: 0, rare: 1, epic: 2 } |
| 127 | +``` |
| 128 | +**Options** |
| 129 | +- `prefix`, `suffix` add a prefix/suffix to predicates. |
| 130 | + - `prefix: true` => `rarity_common?` |
| 131 | + - `suffix: :rarity` => `common_rarity?` |
| 132 | + |
| 133 | +**Behavior** |
| 134 | +- Values are stored as symbols. |
| 135 | +- Adds `rarities` and `rarity_before_type_cast`. |
| 136 | +- Predicate methods (e.g. `common?`) are generated. |
| 137 | + |
| 138 | +### bitmask |
| 139 | +**Usage** |
| 140 | +```ruby |
| 141 | +def_column :flags, type: :integer |
| 142 | +bitmask :flags, as: [:tradeable, :soulbound, :limited] |
| 143 | +``` |
| 144 | +**Behavior** |
| 145 | +- Accepts array/symbol/integer and converts to bit integer. |
| 146 | +- `flags` returns an array of symbols. |
| 147 | +- Adds `flags_value` / `flags_value=` for raw integer bits. |
| 148 | + |
| 149 | +### sti (STI type column) |
| 150 | +**Usage** |
| 151 | +```ruby |
| 152 | +def_column :type, sti: true |
| 153 | +``` |
| 154 | +**Behavior** |
| 155 | +- Converts `type` to a string. |
| 156 | +- Defines `sti_base_class` and `sti_column`. |
| 157 | +- Loader should resolve classes by `type`. |
| 158 | + |
| 159 | +### polymorphic_type |
| 160 | +**Usage** |
| 161 | +```ruby |
| 162 | +def_column :reward_type, polymorphic_type: true |
| 163 | +``` |
| 164 | +**Behavior** |
| 165 | +- Used for `belongs_to polymorphic` type columns. |
| 166 | +- Stores a class name string and sets `reward_type_class`. |
| 167 | +- Empty strings become `nil`. |
| 168 | + |
| 169 | +## Custom column types |
| 170 | +Define custom columns by subclassing `SimpleMaster::Master::Column`. |
| 171 | +If the class name ends with `Column`, the `type` is auto-registered. |
| 172 | + |
| 173 | +```ruby |
| 174 | +class MoneyColumn < SimpleMaster::Master::Column |
| 175 | + private |
| 176 | + |
| 177 | + def code_for_conversion |
| 178 | + <<-RUBY |
| 179 | + value = value&.to_i |
| 180 | + RUBY |
| 181 | + end |
| 182 | + |
| 183 | + def code_for_sql_value |
| 184 | + <<-RUBY |
| 185 | + #{name} |
| 186 | + RUBY |
| 187 | + end |
| 188 | +end |
| 189 | + |
| 190 | +class Product < ApplicationMaster |
| 191 | + def_column :price, type: :money |
| 192 | +end |
| 193 | +``` |
| 194 | + |
| 195 | +- Ensure the file is loaded before use. |
| 196 | +- Override `init` if you need custom methods. |
| 197 | +- See [lib/simple_master/master/column.rb](lib/simple_master/master/column.rb). |
0 commit comments