Module: StoreMethod::ClassMethods

Defined in:
lib/store_method.rb

Instance Method Summary collapse

Instance Method Details

#method_added(name) ⇒ Object



17
18
19
20
21
22
# File 'lib/store_method.rb', line 17

def method_added name
  super
  return if @methods_to_store.nil? || @methods_to_store[name].nil?
  @methods_to_store.delete(name)
  store_method_create(name)
end

#store_method(*names) ⇒ Object Also known as: store_methods



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/store_method.rb', line 3

def store_method *names
  @methods_to_store ||= {}
  
  names.each do |name|
    if instance_methods.include?(name.to_sym)
      store_method_create(name)
    else
      @methods_to_store[name.to_sym] = true
    end
  end
end

#store_method_create(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/store_method.rb', line 24

def store_method_create name
  alias_method "#{name}_orig", name

  define_method(name) do |*args|
    if args.empty?
      val = attributes[name.to_s]
      unless val
        val = send("#{name}_orig")
        if new_record?
          assign_attributes({name.to_sym => val})
        else
          update_column(name.to_sym, val) if val
        end
      end
      return val
    else
      return send("#{name}_orig", *args)
    end
  end
  
  define_method("refresh_#{name}") do |*args|
    val = send("#{name}_orig")
    if new_record?
      assign_attributes({name.to_sym => val})
    else
      update_column(name.to_sym, val)
    end
    return val
  end
end