Module: Coaster::SerializedProperties

Defined in:
lib/coaster/serialized_properties.rb

Defined Under Namespace

Classes: DuplicatedProperty, InvalidProperty

Instance Method Summary collapse

Instance Method Details

#serialized_column(serialize_column) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/coaster/serialized_properties.rb', line 14

def serialized_column(serialize_column)
  define_method serialize_column.to_sym do
    return read_attribute(serialize_column.to_sym) if read_attribute(serialize_column.to_sym)
    write_attribute(serialize_column.to_sym, {})
    read_attribute(serialize_column)
  end
end

#serialized_properties(serialize_column, *keys, type: nil, getter: nil, setter: nil, setter_callback: nil, default: nil, rescuer: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coaster/serialized_properties.rb', line 34

def serialized_properties(serialize_column, *keys, type: nil, getter: nil, setter: nil, setter_callback: nil, default: nil, rescuer: nil)
  keys.flatten.each do |key|
    key_name = key
    prop_hash = {type: type, getter: getter, setter: setter, setter_callback: setter_callback, default: default, rescuer: rescuer}
    if key.is_a? Hash
      key_name = key[:key]
      prop_hash = {type: type, getter: getter, setter: setter, setter_callback: setter_callback, default: default}.merge(key)
      prop_hash.delete(:key)
    end
    serialized_property(serialize_column, key_name, **prop_hash)
  end
end

#serialized_property(serialize_column, key, type: nil, comment: nil, getter: nil, setter: nil, setter_callback: nil, default: nil, rescuer: nil) ⇒ Object

Raises:



28
29
30
31
32
# File 'lib/coaster/serialized_properties.rb', line 28

def serialized_property(serialize_column, key, type: nil, comment: nil, getter: nil, setter: nil, setter_callback: nil, default: nil, rescuer: nil)
  raise DuplicatedProperty, "#{self.name}##{key} duplicated\n#{caller[0..5].join("\n")}" if serialized_property_settings[key.to_sym]
  serialized_property_settings[key.to_sym] = {type: type, comment: comment, getter: getter, setter: setter, setter_callback: setter_callback, default: default, rescuer: rescuer}
  _typed_serialized_property(serialize_column, key, type: type, getter: getter, setter: setter, setter_callback: setter_callback, default: default, rescuer: rescuer)
end

#serialized_property_comment(key) ⇒ Object



22
23
24
25
26
# File 'lib/coaster/serialized_properties.rb', line 22

def serialized_property_comment(key)
  cm = serialized_property_settings[key.to_sym] &&
       serialized_property_settings[key.to_sym][:comment]
  cm || key.to_s
end

#serialized_property_setting(key) ⇒ Object



10
11
12
# File 'lib/coaster/serialized_properties.rb', line 10

def serialized_property_setting(key)
  serialized_property_settings[key.to_sym]
end

#serialized_property_settingsObject



6
7
8
# File 'lib/coaster/serialized_properties.rb', line 6

def serialized_property_settings
  @serialized_property_settings ||= {}
end