Module: VirtualBox::AbstractModel::Attributable::ClassMethods
- Defined in:
- lib/virtualbox/abstract_model/attributable.rb
Overview
Defines the class methods for the VirtualBox::AbstractModel::Attributable module. For detailed overview documentation, see VirtualBox::AbstractModel::Attributable.
Instance Method Summary collapse
-
#attribute(name, options = {}) ⇒ Object
Defines an attribute on the model.
-
#attribute_scope(options, &block) ⇒ Object
Defines the specified scope for all attributes within the block.
-
#attributes ⇒ Object
Returns the hash of attributes and their associated options.
-
#inherited(subclass) ⇒ Object
Used to propagate attributes to subclasses.
Instance Method Details
#attribute(name, options = {}) ⇒ Object
Defines an attribute on the model.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/virtualbox/abstract_model/attributable.rb', line 154 def attribute(name, = {}) name = name.to_sym attributes[name] = defined?(@__attribute_scope) ? @__attribute_scope : {} attributes[name].merge!() # Create the method for reading this attribute define_method("#{name}?") { read_attribute(name) } if [:boolean] define_method(name) { read_attribute(name) } # Create the writer method for it unless the attribute is readonly, # then remove the method if it exists if ![:readonly] define_method("#{name}=") do |value| write_attribute(name, value) end elsif method_defined?("#{name}=") undef_method("#{name}=") end end |
#attribute_scope(options, &block) ⇒ Object
Defines the specified scope for all attributes within the block. The scope is reset to the previous value once the block ends. Multiple scopes can be nested and they’ll inherit from each other.
177 178 179 180 181 182 183 184 185 |
# File 'lib/virtualbox/abstract_model/attributable.rb', line 177 def attribute_scope(, &block) @__attribute_scope ||= {} old_value = @__attribute_scope @__attribute_scope = old_value.merge() instance_eval(&block) @__attribute_scope = old_value end |
#attributes ⇒ Object
Returns the hash of attributes and their associated options.
188 189 190 |
# File 'lib/virtualbox/abstract_model/attributable.rb', line 188 def attributes @attributes ||= {} end |
#inherited(subclass) ⇒ Object
Used to propagate attributes to subclasses. This method makes sure that subclasses of a class with VirtualBox::AbstractModel::Attributable included will inherit the attributes as well, which would be the expected behaviour.
195 196 197 198 199 200 201 |
# File 'lib/virtualbox/abstract_model/attributable.rb', line 195 def inherited(subclass) super rescue NoMethodError attributes.each do |name, option| subclass.attribute(name, option) end end |