SimpleMaster columns are defined with def_column. At load time, type conversion,
cache helpers, and accessor methods are generated.
The behavior depends on type and DSL options.
class Weapon < ApplicationMaster
def_column :id
def_column :name, type: :string
def_column :attack, type: :float
def_column :rarity
enum :rarity, { common: 0, rare: 1, epic: 2 }
end- Example:
def_column :attack, type: :float - See the column type list below.
- Example:
def_column :lv, type: :integer, group_key: true - You can also use
group_key :lv.
- Use when the DB column name differs.
- Example:
def_column :start_at, type: :time, db_column_name: :start_time
- Adds locale-aware values using
I18n.locale. - Example:
def_column :name, globalize: true - You can also use
globalize :name. - Translation values live in
@_globalized_namelike{ en: "Storm Edge", ja: "..." }. - Not supported on
id/enum/bitmask/sti/polymorphic_type. - Cannot be used with
group_key.
Usage
def_column :idBehavior
- Converts to
to_ion assignment. - In tests, updates
id_hashwhen changed.
Usage
def_column :lv, type: :integerBehavior
- Converts to
to_ion assignment (empty string becomesnil).
Usage
def_column :attack, type: :floatBehavior
- Converts to
to_fon assignment (empty string becomesnil).
Usage
def_column :name, type: :stringBehavior
- Converts to
to_son assignment. - Values are cached to reuse identical objects (
object_cache).
Usage
def_column :kind, type: :symbolBehavior
- Converts to
to_s+to_symon assignment. - SQL/CSV output uses a string.
Usage
def_column :is_boss, type: :booleanBehavior
- Integers use 0/1, strings accept "true" or "1".
- Adds a
name?predicate. - SQL/CSV output is 0/1.
Usage
def_column :info, type: :jsonOptions
symbolize_names: trueconverts JSON keys to symbols.
Behavior
- Parses string values with
JSON.parse. - SQL/CSV output uses
JSON.generate. - Non-string assignments are not transformed by
symbolize_names.
Usage
def_column :start_at, type: :timeOptions
db_type: :timeoutputsHH:MM:SSonly.
Behavior
- Parses strings with
Date._parseintoTime. - Sub-seconds are truncated.
Usage
def_column :rarity, enum: { common: 0, rare: 1, epic: 2 }
# or
def_column :rarity
enum :rarity, { common: 0, rare: 1, epic: 2 }Options
prefix,suffixadd a prefix/suffix to predicates.prefix: true=>rarity_common?suffix: :rarity=>common_rarity?
Behavior
- Values are stored as symbols.
- Adds
raritiesandrarity_before_type_cast. - Predicate methods (e.g.
common?) are generated.
Usage
def_column :flags, type: :integer
bitmask :flags, as: [:tradeable, :soulbound, :limited]Behavior
- Accepts array/symbol/integer and converts to bit integer.
flagsreturns an array of symbols.- Adds
flags_value/flags_value=for raw integer bits.
Usage
def_column :type, sti: trueBehavior
- Converts
typeto a string. - Defines
sti_base_classandsti_column. - Loader should resolve classes by
type.
Usage
def_column :reward_type, polymorphic_type: trueBehavior
- Used for
belongs_to polymorphictype columns. - Stores a class name string and sets
reward_type_class. - Empty strings become
nil.
Define custom columns by subclassing SimpleMaster::Master::Column.
If the class name ends with Column, the type is auto-registered.
class MoneyColumn < SimpleMaster::Master::Column
private
def code_for_conversion
<<-RUBY
value = value&.to_i
RUBY
end
def code_for_sql_value
<<-RUBY
#{name}
RUBY
end
end
class Product < ApplicationMaster
def_column :price, type: :money
end- Ensure the file is loaded before use.
- Override
initif you need custom methods. - See lib/simple_master/master/column.rb.