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

Instance Method Details

#attribute(name, options = {}) ⇒ Object

Defines an attribute on the model.

Parameters:

  • name (Symbol)

    The name of the attribute, which will also be used to set the accessor methods.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :readonly (Boolean) — default: false

    If true, attribute will be readonly. More specifically, the ‘attribute=` method won’t be defined for it.

  • :default (Object) — default: nil

    Specifies a default value for the attribute.

  • :populate_key (Symbol) — default: attribute name

    Specifies a custom populate key to use for VirtualBox::AbstractModel::Attributable#populate_attributes



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, options = {})
  name = name.to_sym
  attributes[name] = defined?(@__attribute_scope) ? @__attribute_scope : {}
  attributes[name].merge!(options)

  # Create the method for reading this attribute
  define_method("#{name}?") { read_attribute(name) } if options[: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 !options[: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(options, &block)
  @__attribute_scope ||= {}
  old_value = @__attribute_scope
  @__attribute_scope = old_value.merge(options)

  instance_eval(&block)

  @__attribute_scope = old_value
end

#attributesObject

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