Class: LucidWorks::Schema::ListAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/lucid_works/schema/list_attribute.rb

Constant Summary

Constants inherited from Attribute

Attribute::ATTRIBUTES_TYPES, Attribute::RESERVED_ATTRIBUTE_NAMES

Instance Attribute Summary collapse

Attributes inherited from Attribute

#name, #origin, #schema, #values

Instance Method Summary collapse

Methods inherited from Attribute

#encode_and_insert, factory, #human_value, #nil_when_blank?, #omit_during_update?, #omit_when_blank?, sanitize_name, #to_select

Constructor Details

#initialize(schema, name, options = {}) ⇒ ListAttribute

Returns a new instance of ListAttribute.



8
9
10
11
# File 'lib/lucid_works/schema/list_attribute.rb', line 8

def initialize(schema, name, options={})
  @separator = options[:separator] || " "
  super
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



6
7
8
# File 'lib/lucid_works/schema/list_attribute.rb', line 6

def separator
  @separator
end

Instance Method Details

#create_accessors_for_attribute(klass) ⇒ Object

:nodoc:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lucid_works/schema/list_attribute.rb', line 17

def create_accessors_for_attribute(klass) # :nodoc:
  super

  klass.class_eval <<-EOF, __FILE__, __LINE__+1
    def #{name}
      list = @attributes[:#{name}]
      list.nil? ? "" : list.join(self.class.schema[:#{name}].separator)
    end

    def #{name}=(new_value)
      if new_value.kind_of?(Array)
        @attributes[:#{name}] = new_value
      elsif new_value.kind_of?(String)
        separator = self.class.schema[:#{name}].separator
        separator = /[\\r\\n]+/ if separator == "\n"  # Special case
        @attributes[:#{name}] = new_value.split(separator)
      elsif new_value.nil?
        @attributes[:#{name}] = []
      else
        raise "Can't interpret \#{new_value.inspect} as a #{name}"
      end
      @attributes[:#{name}]
    end
  EOF
end

#typeObject



13
14
15
# File 'lib/lucid_works/schema/list_attribute.rb', line 13

def type
  :list
end