Module: ModelX::Attributes::ClassMethods
- Defined in:
- lib/model_x/attributes.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Array
readonly
An array of defined attribute names.
Instance Method Summary collapse
-
#attribute(*attributes, options = {}) ⇒ Object
DSL method to define attributes.
Instance Attribute Details
#attributes ⇒ Array (readonly)
81 82 83 |
# File 'lib/model_x/attributes.rb', line 81 def attributes @attributes ||= [] end |
Instance Method Details
#attribute(*attributes, options = {}) ⇒ Object
DSL method to define attributes.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/model_x/attributes.rb', line 92 def attribute(*attributes) = attributes. @_model_x_defaults ||= {} @_model_x_types ||= {} attributes.each do |attribute| attribute = attribute.to_sym if instance_methods.include?(attribute) raise AttributeAlreadyDefined, "attribute :#{attribute} is already defined on #{self.name}" end self.attributes << attribute @_model_x_defaults[attribute] = [:default] if .key?(:default) @_model_x_types[attribute] = [:type] class_eval <<-RUBY, __FILE__, __LINE__+1 def #{attribute} value = read_attribute(:#{attribute}) value = self.class.send(:_model_x_default, :#{attribute}) if value.nil? value end def #{attribute}=(value) write_attribute :#{attribute}, value end RUBY if [:type] class_eval <<-RUBY, __FILE__, __LINE__+1 #{[:type]} :#{attribute} RUBY end end end |