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