Class: StateMethods::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/state_methods/factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, state_accessor, partition) ⇒ Factory

Returns a new instance of Factory.



7
8
9
10
11
12
13
# File 'lib/state_methods/factory.rb', line 7

def initialize(klass, state_accessor, partition)
  @klass = klass
  @state_accessor = state_accessor
  @partition = partition
  @keys = [state_accessor]
  init
end

Instance Method Details

#check(method_name, force = false) ⇒ Object



18
19
# File 'lib/state_methods/factory.rb', line 18

def check(method_name, force = false)
end

#declare(method_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/state_methods/factory.rb', line 25

def declare(method_name)
  this = self
  ::StateMethods::MethodUtils.define_class_method(@klass, method_name) do |*states, &block|
    factory = this.factory_for(self)
    states.each do |state|
      factory.set(self, method_name, state, &block)
    end
  end
  check(method_name, force=true)

  state_accessor = @state_accessor
  ::StateMethods::MethodUtils.define_instance_method(@klass, method_name) do |*args|
    state = send(state_accessor) || :*
    factory = this.factory_for(self.class)
    begin
      factory.get(self, method_name, state, *args)
    rescue Undefined
      nil
    end
  end

end

#factory_for(klass) ⇒ Object



21
22
23
# File 'lib/state_methods/factory.rb', line 21

def factory_for(klass)
  self
end

#initObject



15
16
# File 'lib/state_methods/factory.rb', line 15

def init
end