module FluentConditions class AccessorDefinerFactory def self.make_for(options) if options.include?(:values) TextValueAccessorDefiner.new(options) elsif options.include?(:if) CustomAccessorDefiner.new(options) else BooleanAccessorDefiner.new(options) end end end class FluentAccessorMethod def initialize(name) @name = name end def positive_check @name end def negative_check "not_#{@name}" end def positive_check_with_result "#{@name}?" end def negative_check_with_result "not_#{@name}?" end end class TextValueAccessorDefiner def initialize(options) @options = options end def define(builder, field) @options[:values].each do |value| accessor_method = FluentAccessorMethod.new(value) builder.class_eval do define_method(accessor_method.positive_check) do update_and_continue(instance_variable_get(:@object).send(field) == value) end define_method(accessor_method.positive_check_with_result) do update_and_finish(instance_variable_get(:@object).send(field) == value) end define_method(accessor_method.negative_check) do update_and_continue(instance_variable_get(:@object).send(field) != value) end define_method(accessor_method.negative_check_with_result) do update_and_finish(instance_variable_get(:@object).send(field) != value) end end end end end class CustomAccessorDefiner def initialize(options) @options = options end def define(builder, field) field_name = @options[:as] condition_check = @options[:if] accessor_method = FluentAccessorMethod.new(field_name) builder.class_eval do define_method(accessor_method.positive_check) do update_and_continue(condition_check.call(instance_variable_get(:@object).send(field))) end define_method(accessor_method.positive_check_with_result) do update_and_finish(condition_check.call(instance_variable_get(:@object).send(field))) end define_method(accessor_method.negative_check) do update_and_continue(!condition_check.call(instance_variable_get(:@object).send(field))) end define_method(accessor_method.negative_check_with_result) do update_and_finish(!condition_check.call(instance_variable_get(:@object).send(field))) end end end end class BooleanAccessorDefiner def initialize(options) @options = options end def define(builder, field) accessor_method = FluentAccessorMethod.new(field) builder.class_eval do define_method(accessor_method.positive_check) do update_and_continue(instance_variable_get(:@object).send(field)) end define_method(accessor_method.positive_check_with_result) do update_and_finish(instance_variable_get(:@object).send(field)) end define_method(accessor_method.negative_check) do update_and_continue(!instance_variable_get(:@object).send(field)) end define_method(accessor_method.negative_check_with_result) do update_and_finish(!instance_variable_get(:@object).send(field)) end end end end end