module EventAttribute::ClassMethods
Public Instance Methods
event_attribute(column, options = {})
click to toggle source
Configuration options¶ ↑
-
attribute
- name of the attribute that will be created in the model that returns true/false (default: column name minus ‘_at’ or ‘_on’) -
nil_equals
- whether or not the attribute should return true or false if the column is nil (default: false)
# File lib/event_attribute.rb, line 60 def event_attribute(column, options = {}) unless respond_to?(:event_attribute_attrs) class_attribute :event_attributes class_attribute :event_attribute_attrs self.event_attributes, self.event_attribute_attrs = {}, {} end attribute = (options[:attribute] || (column.to_s =~ /_at|_on/ ? column.to_s[0...-3] : raise("Unable to create default attribute name"))).to_sym nil_equals = options[:nil_equals] || false self.event_attribute_attrs[attribute] = column self.event_attributes[column] = nil_equals create_attribute_accessors(attribute, column, nil_equals) end
Private Instance Methods
create_attribute_accessors(attribute, column, nil_equals)
click to toggle source
# File lib/event_attribute.rb, line 79 def create_attribute_accessors(attribute, column, nil_equals) define_method(attribute) { (nil_equals ? self[column].nil? : !self[column].nil?) } alias_method :"#{attribute.to_s}?", attribute # define the method to set the field value define_method(:"#{attribute.to_s}=") do |value| if [true, "1", 1, "t", "true"].include? value send("#{column}=", nil_equals ? nil : DateTime.now) elsif [false, "0", 0, "f", "false"].include? value send("#{column}=", nil_equals ? DateTime.now : nil) end end end