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, #on_after_initialize, #on_before_create, #opt_input, #process_data, #set, types, #update_data, #validate_item

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



27
28
29
30
31
32
33
34
35
36
# File 'app/models/contents_core/item_hash.rb', line 27

def method_missing( method, *args, &block )
  matches = /data_([^=]+)([=]{0,1})/.match method.to_s
  if matches
    if matches[2].blank?
      return self.data[matches[1].to_sym]
    elsif matches[1]
      self.data[matches[1].to_sym] = args[0]
    end
  end
end

Class Method Details

.permitted_attributesObject



46
47
48
# File 'app/models/contents_core/item_hash.rb', line 46

def self.permitted_attributes
  [ :data_hash ]
end

.type_nameObject



50
51
52
# File 'app/models/contents_core/item_hash.rb', line 50

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.symbolize_keys
  end
end

#initObject



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

def init
  self.data = {} unless 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)


38
39
40
# File 'app/models/contents_core/item_hash.rb', line 38

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

#to_sObject



42
43
44
# File 'app/models/contents_core/item_hash.rb', line 42

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