lib/maintain/maintainer.rb in maintain-0.1.6 vs lib/maintain/maintainer.rb in maintain-0.1.7
- old
+ new
@@ -1,5 +1,6 @@
+# encoding: UTF-8
module Maintain
class Maintainer
def aggregate(name, options)
if options.is_a?(Hash) && options.has_key?(:as)
options = options[:as]
@@ -36,11 +37,11 @@
def bitmask(value)
@bitmask = !!value
end
def bitmask?
- @bitmask
+ !!@bitmask
end
def default(state)
@default = state
end
@@ -48,17 +49,15 @@
def default?
!!@default
end
def hook(event, state, instance)
- if state && hooks[state.to_sym] && hooks[state.to_sym][event.to_sym]
- hooks[state.to_sym][event.to_sym].each do |method|
- if method.is_a?(Proc)
- instance.instance_eval(&method)
- else
- instance.send(method)
- end
+ 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|
+ next if hook_definition[:if] && !call_method_or_proc_on_instance(hook_definition[:if], instance)
+ next if hook_definition[:unless] && call_method_or_proc_on_instance(hook_definition[:unless], instance)
+ call_method_or_proc_on_instance(hook_definition[:method], instance)
end
end
end
def initialize(maintainee, attribute, active_record = false, options = {})
@@ -72,17 +71,29 @@
def integer(value)
@integer = !!value
end
- def on(event, state, method = nil, &block)
+ def integer?
+ !!@integer
+ end
+
+ def on(*args, &block)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ event, state = args.shift, args.shift
+ method = args.shift
if block_given?
method = block
end
hooks[state.to_sym] ||= {}
hooks[state.to_sym][event.to_sym] ||= []
- hooks[state.to_sym][event.to_sym].push(method) unless hooks[state.to_sym][event.to_sym].include?(method)
+ 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
def state_name_for(value)
if value = states.find {|key, options| options[:compare_value] == value}
value[0]
@@ -96,20 +107,20 @@
end
if options.has_key?(:default)
default(name)
end
@increment ||= 0
- if @bitmask
+ 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}
+ states[name] = {:compare_value => !bitmask? && value.is_a?(Integer) ? value : @increment, :value => value}
@increment += 1
if @active_record && !maintainee.respond_to?(name)
conditions = {:conditions => {@attribute => value.is_a?(Symbol) ? value.to_s : value}}
if defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::STRING >= "3"
maintainee.scope name, conditions
@@ -136,13 +147,13 @@
def states
@states ||= {}
end
def value(initial = nil)
- if @bitmask
+ if bitmask?
BitmaskValue.new(self, initial || @default || 0)
- elsif @integer
+ elsif integer?
IntegerValue.new(self, initial || @default)
else
Value.new(self, initial || @default)
end
end
@@ -162,19 +173,32 @@
if class_method
respond_to = maintainee_class.respond_to?(method_name)
methods = maintainee_class.public_methods + maintainee_class.private_methods + maintainee_class.protected_methods
else
respond_to = false
- methods = maintainee_class.public_instance_methods + maintainee_class.private_instance_methods + maintainee_class.protected_instance_methods
+ methods = maintainee_class.instance_methods
+ # methods = %w(instance_methods public_instance_methods private_instance_methods protected_instance_methods).inject([]) do |methods, method|
+ # methods + maintainee_class.send(method)
+ # end.uniq
end
- !respond_to && !methods.include?(method_name)
+ # Ruby 1.8 returns arrays of strings; ruby 1.9 returns arrays of symbols. "Awesome."
+ !respond_to && !methods.include?(method_name) && !methods.include?(method_name.to_sym)
end
def method_missing(method, *args)
if states.has_key?(method)
states[method][:value]
else
super
+ end
+ end
+
+ private
+ def call_method_or_proc_on_instance(method, instance)
+ if method.is_a?(Proc)
+ instance.instance_eval(&method)
+ else
+ instance.send(method)
end
end
end
end
\ No newline at end of file