Class: ContentsCore::ItemArray

Inherits:
Item show all
Defined in:
app/models/contents_core/item_array.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Item

#as_json, #attr_id, #class_name, #config, #editable, #on_after_initialize, #on_before_create, #opt_input, permitted_attributes, #process_data, #set, #to_s, types, #update_data, #validate_item

Class Method Details

.type_nameObject



58
59
60
# File 'app/models/contents_core/item_array.rb', line 58

def self.type_name
  'array'
end

Instance Method Details

#dataObject

after_initialize do

config[:data_type] ||= :integer  # TODO: this overrides the config !

end



10
11
12
# File 'app/models/contents_core/item_array.rb', line 10

def data
  is_multiple? ? self.data_text : converted_data
end

#data=(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/contents_core/item_array.rb', line 14

def data=( value )
  if is_multiple?
    if data_type
      self.data_text = case data_type
        # when :boolean
        #   self.data_boolean = value  # ( value == 1 ) || ( value == '1' ) || ( value == 'true' ) || ( value == 'yes' )
        when :float
          value.map( &:to_f )
        when :integer
          value.map( &:to_i )
        when :string  # , :text
          value.map( &:to_s )
        else
          value
        end
    else
      self.data_text = value
    end
  else
    convert_data( value )
  end
end

#data_typeObject



37
38
39
# File 'app/models/contents_core/item_array.rb', line 37

def data_type
  @data_type ||= ( config[:data_type] || :integer ).to_sym
end

#enum(params = nil) ⇒ Object



41
42
43
# File 'app/models/contents_core/item_array.rb', line 41

def enum( params = nil )
  config[:values] ? config[:values] : ( config[:values_method] ? config[:values_method].call( params ) : self.data_hash )
end

#initObject



45
46
47
48
49
50
51
52
# File 'app/models/contents_core/item_array.rb', line 45

def init
  unless self.data
    self.data_string = []
    self.data_hash = []
    self.data_text = []
  end
  self
end

#is_multiple?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/contents_core/item_array.rb', line 54

def is_multiple?
  config[:multiple] ? true : false
end