Class: Maintain::Maintainer

Inherits:
Object
  • Object
show all
Defined in:
lib/maintain/maintainer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maintainee, attribute, options = {}) ⇒ Maintainer

Returns a new instance of Maintainer.



99
100
101
102
103
104
105
106
107
108
# File 'lib/maintain/maintainer.rb', line 99

def initialize(maintainee, attribute, options = {})
  @maintainee = maintainee.name
  @attribute = attribute.to_sym
  if back_end = options.delete(:back_end)
    @back_end = Maintain::Backend.build(back_end)
  end
  options.each do |key, value|
    self.send(key, value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



246
247
248
249
250
251
252
# File 'lib/maintain/maintainer.rb', line 246

def method_missing(method, *args)
  if states.has_key?(method)
    states[method][:value]
  else
    super
  end
end

Instance Attribute Details

#back_endObject (readonly)

Returns the value of attribute back_end.



4
5
6
# File 'lib/maintain/maintainer.rb', line 4

def back_end
  @back_end
end

Class Method Details

.call_method_or_proc(method, instance) ⇒ Object



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

def call_method_or_proc(method, instance)
  if method.is_a?(Proc)
    instance.instance_eval(&method)
  else
    instance.send(method)
  end
end

Instance Method Details

#aggregate(name, conditions, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
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
54
# File 'lib/maintain/maintainer.rb', line 16

def aggregate(name, conditions, options = {})
  if conditions.is_a?(Hash) && conditions.has_key?(:as)
    options = conditions
    conditions = options[:as]
  end
  aggregates[name] = conditions

  # Now we're going to add proxies to test for state being in this
  # aggregate. Don't create this method unless it doesn't exist.
  boolean_method = "#{name}?"
  if method_free?(boolean_method)
    # Define it if'n it don't already exist! These are just proxies - so
    # Foo.maintains(:state) { state :awesome } will now have
    # Foo.new.awesome?. But that's really just a proxy for
    # Foo.new.state.awesome?
    # So they're just shortcuts for brevity's sake.
    maintainee.class_eval <<-EOC
      def #{boolean_method}
        #{@attribute}.#{boolean_method}
      end
    EOC
  end

  # Now define the state
  if back_end
    conditions = conditions.select { |value| states.has_key?(value) }
    conditions = conditions.map do |value|
      if states[value][:value].is_a?(Symbol)
        states[value][:value].to_s
      else
        states[value][:value]
      end
    end
    conditions = conditions.compact
    back_end.aggregate(maintainee, name, @attribute, conditions, {
      force: options[:force]
    })
  end
end

#aggregatesObject



56
57
58
# File 'lib/maintain/maintainer.rb', line 56

def aggregates
  @aggregates ||= {}
end

#bitmask(value) ⇒ Object



60
61
62
# File 'lib/maintain/maintainer.rb', line 60

def bitmask(value)
  @bitmask = !!value
end

#bitmask?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/maintain/maintainer.rb', line 64

def bitmask?
  !!@bitmask
end

#default(state = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/maintain/maintainer.rb', line 68

def default(state = nil)
  if state
    if bitmask?
      @default = (@default || 0) | states[state][:value]
    else
      @default = state
    end
  else
    @default
  end
end

#default?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/maintain/maintainer.rb', line 80

def default?
  !!@default
end

#hook(event, state, instance) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/maintain/maintainer.rb', line 84

def hook(event, state, instance)
  if state && state.to_s.strip != '' && hooks[state.to_sym]
    hook_definitions = hooks[state.to_sym][event.to_sym] || []
    hook_definitions.each do |hook_definition|
      if hook_definition[:if]
        next unless call_method_or_proc(hook_definition[:if], instance)
      end
      if hook_definition[:unless]
        next if call_method_or_proc(hook_definition[:unless], instance)
      end
      call_method_or_proc(hook_definition[:method], instance)
    end
  end
end

#integer(value) ⇒ Object



110
111
112
# File 'lib/maintain/maintainer.rb', line 110

def integer(value)
  @integer = !!value
end

#integer?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/maintain/maintainer.rb', line 114

def integer?
  !!@integer
end

#on(*args, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/maintain/maintainer.rb', line 118

def on(*args, &block)
  options = {when: :before}.merge(args.last.is_a?(Hash) ? args.pop : {})
  event, state = args.shift, args.shift
  method = args.shift
  if block_given?
    method = block
  end
  if back_end && back_end.respond_to?(:on)
    back_end.on(maintainee, @attribute, event, state, method, options)
  else
    hooks[state.to_sym] ||= {}
    hooks[state.to_sym][event.to_sym] ||= []
    method_hash = {method: method}.merge(options)
    if old_definition = hooks[state.to_sym][event.to_sym].find{|hook| hook[:method] == method}
      old_definition.merge!(method_hash)
    else
      hooks[state.to_sym][event.to_sym].push(method_hash)
    end
  end
end

#state(name, value = nil, options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/maintain/maintainer.rb', line 145

def state(name, value = nil, options = {})
  if value.is_a?(Hash)
    options = value
    value = nil
  end
  @increment ||= 0
  if bitmask?
    unless value.is_a?(Integer)
      value = @increment
    end
    value = 2 ** value.to_i
  elsif value.is_a?(Integer)
    integer(true)
  end
  value ||= name
  states[name] = {compare_value: !bitmask? && value.is_a?(Integer) ? value : @increment, value: value}
  @increment += 1
  if back_end
    back_end.state maintainee, name, @attribute, value.is_a?(Symbol) ? value.to_s : value, force: options[:force]
  end

  # We need the states hash to contain the compare_value for this guy
  # before we can set defaults on the bitmask, since the default should
  # actually be a bitmask of all possible default states
  if options.has_key?(:default)
    default(name)
  end

  if options.has_key?(:enter)
    on :enter, name.to_sym, options.delete(:enter)
  end

  if options.has_key?(:exit)
    on :exit, name.to_sym, options.delete(:exit)
  end

  # Now we're going tests for state. Shortcuts to these methods only get
  # added if a method of their name doesn't already exist.
  boolean_method = "#{name}?"
  shortcut = options[:force] || method_free?(boolean_method)
  maintainee.class_eval <<-EOC
    def #{@attribute}_#{boolean_method}
      #{@attribute} == #{value.inspect}
    end
    #{"alias :#{boolean_method} :#{@attribute}_#{boolean_method}" if shortcut}
  EOC

  # Last but not least, add bang methods to automatically convert to state.
  # Like boolean methods above, these only get added if they're not already
  # things that are things.
  bang_method = "#{name}!"
  shortcut = options[:force] || method_free?(bang_method)
  maintainee.class_eval <<-EOC
    def #{@attribute}_#{bang_method}
      self.#{@attribute} = #{value.inspect}
    end
    #{"alias :#{bang_method} :#{@attribute}_#{bang_method}" if shortcut}
  EOC
end

#state_name_for(value) ⇒ Object



139
140
141
142
143
# File 'lib/maintain/maintainer.rb', line 139

def state_name_for(value)
  if value = states.find {|key, options| options[:compare_value] == value}
    value[0]
  end
end

#statesObject



205
206
207
# File 'lib/maintain/maintainer.rb', line 205

def states
  @states ||= {}
end

#value(instance, initial = nil) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/maintain/maintainer.rb', line 209

def value(instance, initial = nil)
  if back_end
    initial = back_end.read(instance, @attribute)
  end
  initial ||= initial || @default
  if bitmask?
    BitmaskValue.new(self, initial || 0)
  elsif integer?
    IntegerValue.new(self, initial)
  else
    Value.new(self, initial)
  end
end