Module: Aspect::HasAttributes::ClassMethods
- Defined in:
- lib/aspect/has_attributes.rb
Overview
The class methods to extend into the object HasAttributes was included in.
Instance Method Summary collapse
-
#attribute(name, options = {}) {|value, options| ... } ⇒ Object
Define an attribute on the object.
Instance Method Details
#attribute(name, options = {}) {|value, options| ... } ⇒ Object
Define an attribute on the object.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/aspect/has_attributes.rb', line 151 def attribute(name, ={}, &block) # TODO: Should break out into protected methods? name = name.to_sym = .to_h unless .is_a?(Hash) = { getter: true, setter: true }.merge() if [:getter] [:getter] = [:getter] == true ? {} : [:getter].to_h method_name = [:query] ? "#{name}?" : name define_method(method_name) do value = instance_variable_get("@#{name}") [:query] ? !!value : value end end if [:setter] [:setter] = [:setter] == true ? {} : [:setter].to_h define_method("#{name}=") do |value| value = instance_exec(value, , &block) unless block.nil? value = [:query] ? !!value : value instance_variable_set("@#{name}", value) end end self end |