Class: FlexibleEnum::QuestionMethodConfigurator

Inherits:
AbstractConfigurator show all
Defined in:
lib/flexible_enum/question_method_configurator.rb

Instance Attribute Summary

Attributes inherited from AbstractConfigurator

#attribute_name, #elements, #feature_module, #module_for_elements

Instance Method Summary collapse

Methods inherited from AbstractConfigurator

#add_class_method, #add_instance_method

Instance Method Details

#applyObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/flexible_enum/question_method_configurator.rb', line 3

def apply
  elements.each do |element_name, element_config|
    attribute_name = self.attribute_name

    # Define question method
    add_instance_method("#{element_name}?") do
      self.send(attribute_name) == element_config[:value]
    end

    # Define inverse question method (if requested)
    if element_config[:inverse]
      add_instance_method("#{element_config[:inverse]}?") do
        !self.send("#{element_name}?")
      end
    end
  end
end