Module: Tengine::Core::CollectionAccessible::ClassMethods

Defined in:
lib/tengine/core/collection_accessible.rb

Instance Method Summary collapse

Instance Method Details

#array_text_accessor(attr_name, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tengine/core/collection_accessible.rb', line 8

def array_text_accessor(attr_name, options = {})
  options = {:delimeter => ","}.update(options || {})
  delimeter = options[:delimeter]
  self.module_eval(<<-"EOS", __FILE__, __LINE__ + 1)
    def #{attr_name}_text
      #{attr_name} ? #{attr_name}.join(#{delimeter.inspect}) : ""
    end
    def #{attr_name}_text=(value)
      self.#{attr_name} = value.nil? ? [] :
        value.split(#{delimeter.inspect}).map(&:strip)
    end
  EOS
end

#map_yaml_accessor(attr_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/tengine/core/collection_accessible.rb', line 22

def map_yaml_accessor(attr_name)
  self.module_eval(<<-"EOS", __FILE__, __LINE__ + 1)
    def #{attr_name}_yaml
      YAML.dump({}.update(#{attr_name} || {}))
    end
    def #{attr_name}_yaml=(value)
      self.#{attr_name} = value.blank? ? nil : YAML.load(value)
    end
  EOS
end