Module: Adify::Model::ClassMethods

Defined in:
lib/adify/model.rb

Overview

TODO: refactor this up so both model and controller use same code. DRY

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adificationObject



18
19
20
# File 'lib/adify/model.rb', line 18

def self.adification
  @adification_of_item = self.adify_attributes.update_values{|v| get_adify_value(self,v)}
end

.adify_attributesObject



22
23
24
# File 'lib/adify/model.rb', line 22

def self.adify_attributes
  super.deep_merge(@@adify_class_attributes) rescue @@adify_class_attributes
end

.adify_attributes=(hash) ⇒ Object



14
15
16
# File 'lib/adify/model.rb', line 14

def self.adify_attributes=(hash)
  @@adify_class_attributes = hash
end

Instance Method Details

#adify(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adify/model.rb', line 9

def adify(*args)
  @@adify_class_attributes = HashWithIndifferentAccess.new()
  attr_accessor :adify_instance_attributes
  send :include, ::Adify::InstanceMethods

  def self.adify_attributes=(hash)
    @@adify_class_attributes = hash
  end

  def self.adification
    @adification_of_item = self.adify_attributes.update_values{|v| get_adify_value(self,v)}
  end

  def self.adify_attributes
    super.deep_merge(@@adify_class_attributes) rescue @@adify_class_attributes
  end

  private
    def get_adify_value(item,value)
      case value.class
        when Array  then value.each.collect{|v| self.get_adify_value(item,v)}
        when Proc   then (value.call item).to_s
        when Hash   then value.update(value){|k,v| self.get_adify_value(item,v)}
        when Symbol then item.respond_to?(value) ? item.send(value) : value
        else value
      end
    end

  self.adify_attributes = *args
end

#get_adify_value(item, value) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/adify/model.rb', line 27

def get_adify_value(item,value)
  case value.class
    when Array  then value.each.collect{|v| self.get_adify_value(item,v)}
    when Proc   then (value.call item).to_s
    when Hash   then value.update(value){|k,v| self.get_adify_value(item,v)}
    when Symbol then item.respond_to?(value) ? item.send(value) : value
    else value
  end
end