Class: StateMethods::Implementations::Classy

Inherits:
Factory
  • Object
show all
Defined in:
lib/state_methods/implementations/classy.rb

Instance Method Summary collapse

Methods inherited from Factory

#declare, #initialize

Constructor Details

This class inherits a constructor from StateMethods::Factory

Instance Method Details

#check(method_name, force = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/state_methods/implementations/classy.rb', line 44

def check(method_name, force = false)
  ok = begin
    Proxy.new(nil).send(method_name)
    false
  rescue NoMethodError
    true
  rescue
    false
  end
  unless ok
    if force
      @proxy.send(:undef_method, method_name)
    else
      raise ArgumentError, "method '#{method_name}' won't work"
    end
  end
  set(@klass, method_name, :all) { raise Undefined }
end

#class_for(state) ⇒ Object



108
109
110
# File 'lib/state_methods/implementations/classy.rb', line 108

def class_for(state)
  const_for(state).constantize
end

#const_for(state) ⇒ Object



112
113
114
# File 'lib/state_methods/implementations/classy.rb', line 112

def const_for(state)
  [@klass, @state_accessor, state].join('_').sub('*','star').camelize
end

#factory_for(klass) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/state_methods/implementations/classy.rb', line 63

def factory_for(klass)
  if klass == @klass
    self
  else
    klass._state_method_factory_for(*@keys)
  end
end

#get(instance, method_name, state, *args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/state_methods/implementations/classy.rb', line 83

def get(instance, method_name, state, *args)
  # instance.class.should == @klass
  klass = nil
  [state, :*, :all].find do |s|
    begin klass = class_for(s)
    rescue NameError
    end
  end
  if klass
    base = klass.new(instance)
    # if base.respond_to?(method_name)
    base.send(method_name, *args)
    # else
    #   raise Undefined
    # end
  end
end

#initObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/state_methods/implementations/classy.rb', line 20

def init
  proxy_const = [:proxy, @state_accessor].join('_').camelize
  @proxy = if Object.const_defined?(proxy_const)
    proxy_const.constantize
  else
    keys = @keys
    make!(proxy_const, Proxy) do
      self._keys = keys
    end
  end

  @superclass = @klass.superclass
  if @superclass.respond_to?(:_state_method_factory_for)
    begin
      @super_proxy = @superclass._state_method_factory_for(*@keys)
    rescue PartitionNotFound
    end
  end
  @partition.index.each do |state, ancestors|
    superstate = ancestors[1]
    make(state, superstate)
  end
end

#make(state, superstate) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/state_methods/implementations/classy.rb', line 123

def make(state, superstate)
  const = const_for(state)
  unless Object.const_defined?(const)
    if state_superclass = state_superclass_for(state)
      make!(const, state_superclass)
    elsif superstate
      make!(const, @proxy) do
        define_method :method_missing do |method_name, *args, &block|
          klass = base_proxy_for(superstate)
          klass.send(method_name, *args, &block)
        end
        define_method :respond_to_missing? do |name, include_private = false|
          name == method_name or super
        end
      end
    else
      make!(const, @proxy)
    end
  end
  const.constantize
end

#make!(const, state_superclass, &block) ⇒ Object



116
117
118
119
120
121
# File 'lib/state_methods/implementations/classy.rb', line 116

def make!(const, state_superclass, &block)
  new_class = Class.new(state_superclass,&block)
  Object.send(:remove_const, const) if Object.const_defined?(const)
  Object.const_set(const, new_class, true)
  new_class
end

#set(klass, method_name, state, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/state_methods/implementations/classy.rb', line 71

def set(klass, method_name, state, &block)
  # klass.should == @klass
  klass = begin
    class_for(state)
  rescue NameError
    raise UndeclaredState
  end
  ::StateMethods::MethodUtils.define_instance_method(klass, method_name) do |*args|
    @base.instance_exec(*args, &block)
  end
end

#state_superclass_for(state) ⇒ Object



101
102
103
104
105
106
# File 'lib/state_methods/implementations/classy.rb', line 101

def state_superclass_for(state)
  begin
    @super_proxy && @super_proxy.class_for(state)
  rescue NameError
  end
end