Class: ContentsCore::ItemHash

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Item

#as_json, #attr_id, #class_name, #config, #data_type, #editable, item_types, #opt_input, #process_data, #set, #update_data

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



27
28
29
30
# File 'app/models/contents_core/item_hash.rb', line 27

def method_missing( method, *args, &block )
  matches = /data_(.+)=/.match method.to_s
  self.data[matches[1]] = args[0] if matches[1]
end

Class Method Details

.permitted_attributesObject



40
41
42
# File 'app/models/contents_core/item_hash.rb', line 40

def self.permitted_attributes
  [ :data_hash ]
end

.type_nameObject



44
45
46
# File 'app/models/contents_core/item_hash.rb', line 44

def self.type_name
  'hash'
end

Instance Method Details

#from_string(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'app/models/contents_core/item_hash.rb', line 16

def from_string( value )
  if value.is_a? String
    val = {}
    value.each_line do |line|
      m = line.match( /([^:]*):(.*)/ )
      val[m[1]] = m[2].strip if m && !m[1].blank?
    end
    self.data_hash = val
  end
end

#initObject



7
8
9
10
# File 'app/models/contents_core/item_hash.rb', line 7

def init
  self.data = {}
  self
end

#keysObject



12
13
14
# File 'app/models/contents_core/item_hash.rb', line 12

def keys
  config[:keys] ? config[:keys] : self.data_hash.keys
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/contents_core/item_hash.rb', line 32

def respond_to?( method, include_private = false )
  method.to_s.starts_with?( 'data_' ) || super
end

#to_sObject



36
37
38
# File 'app/models/contents_core/item_hash.rb', line 36

def to_s
  self.data_hash ? self.data_hash.inject( '' ) { |k, v| k + v[0] + ': ' + v[1] + "\n" } : {}
end