Class: FlexibleEnum::SetterMethodConfigurator

Inherits:
AbstractConfigurator show all
Defined in:
lib/flexible_enum/setter_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
# File 'lib/flexible_enum/setter_method_configurator.rb', line 3

def apply
  attribute_name = self.attribute_name

  elements.each do |element_name, element_config|
    bang_method_name = element_config[:setter] || "#{element_name}!"
    attributes = {attribute_name => element_config[:value]}
    timestamp_attribute_name = element_config[:timestamp_attribute] || element_name

    add_instance_method(bang_method_name) do
      time = Time.now.utc
      attributes["#{timestamp_attribute_name}_on".to_sym] = time.to_date if self.class.attribute_method?("#{timestamp_attribute_name}_on")
      attributes["#{timestamp_attribute_name}_at".to_sym] = time if self.class.attribute_method?("#{timestamp_attribute_name}_at")
      update_attributes!(attributes)
    end
  end
end