Module: NAttributes

Defined in:
lib/n_attributes.rb,
lib/n_attributes/railtie.rb,
lib/n_attributes/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Railtie

Constant Summary collapse

VERSION =
"0.2.7"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



18
19
20
21
22
23
24
# File 'lib/n_attributes.rb', line 18

def method_missing(meth, *args, &block)
  if meth.present?
    run_split(meth, *args, &block)
  else
    super
  end
end

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/n_attributes.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#get_key_value(field, key) ⇒ Object



45
46
47
48
49
# File 'lib/n_attributes.rb', line 45

def get_key_value(field, key)
  if key.present?
    eval(self.send(field))[key]
  end
end

#run_split(meth, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/n_attributes.rb', line 26

def run_split(meth, *args, &block)
  meth = meth.to_s
  attribute, key = meth.gsub('=', '').split('_')
  if args[0].nil?
    get_key_value(attribute, key)
  else
    set_key_pair(attribute, key, args[0])
  end
end

#set_key_pair(field, key, value) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/n_attributes.rb', line 36

def set_key_pair(field, key, value)
  if self.send(field).nil?
    self.send(field+ '=', Hash[key, value].to_s)
  else
    self.send(field+ '=', eval(self.send(field)).merge(Hash[key, value]).to_s)
  end
  self.save
end