Module: Attributable::ClassMethods

Defined in:
app/models/attributable.rb

Overview

Class methods for attribute configuration

Instance Method Summary collapse

Instance Method Details

#association(name, instance_method, options = {}) ⇒ Object

Defines an association with the name.



103
104
105
106
107
# File 'app/models/attributable.rb', line 103

def association(name, instance_method, options = {})
  association = Association.new(self, name, instance_method, options)
  association.configure(self)
  self.association_details += [association]
end

#attribute_details_for(attribute_name) ⇒ Attributable::Attribute

Looks up the Attribute in a attribute_details Array

Parameters:

  • attribute_name (String)

    The name of the attribute to lookup

Returns:



127
128
129
130
# File 'app/models/attributable.rb', line 127

def attribute_details_for(attribute_name)
  attribute_details.detect { |d| d.name.to_sym == attribute_name.to_sym } ||
    raise(StandardError, "Unknown attribute #{attribute_name}")
end

#attribute_namesArray<String>

Returns An array of all attribute names.

Returns:

  • (Array<String>)

    An array of all attribute names



118
119
120
# File 'app/models/attributable.rb', line 118

def attribute_names
  attribute_details.map(&:name)
end

#custom_attribute(name, options = {}, override_previous = false) ⇒ void

Note:

Heavy on meta-programming here. There behaviour could also be tidied up and simplified significantly.

This method returns an undefined value.

Define a custom attribute with the provided name. Will automatically generate: - Validations - Form helpers - Accessioning tie-ins - Convert blank attributes to nil

Parameters:

  • name (Symbol, String)

    The name of the attribute to generate

  • override_previous (defaults to: false)

    = false [type] Override any attributes of the same name on parent classes. (UNUSED)

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

    a customizable set of options

Options Hash (options):

  • :default (Object)

    A default value for the option. (Not a proc/lambda)

  • :required (Boolean) — default: false

    Whether the option is required or not

  • :validator (Boolean) — default: false

    Set to true to defer validation to the #validator_for method

  • :integer (Boolean) — default: false

    The attribute should be an integer

  • :positive_float (Boolean) — default: false

    The attribute should be a float, greater than 0

  • :boolean (Boolean) — default: false

    The attribute should be true or false. WARNING! Currently just tests presence of the key, not actual value. Thus false=true.

  • :in (Array) — default: nil

    The attribute is a selection that must be included in the array.

  • :selection (Boolean) — default: false

    The attribute is a selection generated dynamically from

    validator_for

  • :minimum (Numeric) — default: 0

    The minimum value for an integer. WARNING! Inconsistently implemented for floats

  • :with (Regexp) — default: nil

    Regexp for validating the attribute

  • :if (Symbol) — default: nil

    Passed through to the rails validator and will also switch persistence of the attribute based on the condition.

  • :on (Symbol) — default: nil

    Passed through to the rails validator. (eg. on: :create)

  • :save_blank (Boolean) — default: false

    set to true to disabling setting blank attributes to nil. (UNUSED)



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/attributable.rb', line 90

def custom_attribute(name, options = {}, override_previous = false)
  attribute = Attribute.new(self, name, options)
  attribute.configure(self)

  if override_previous
    self.attribute_details = attribute_details.reject { |a| a.name == name }
    self.attribute_details += [attribute]
  elsif self.attribute_details.detect { |a| a.name == name }.nil?
    self.attribute_details += [attribute]
  end
end

#defaultsHash<String,Object>

Returns a hash of default attribute values

Returns:

  • (Hash<String,Object>)

    Hash of each attribute and its default



112
113
114
115
# File 'app/models/attributable.rb', line 112

def defaults
  @defaults ||=
    attribute_details.each_with_object({}) { |attribute, hash| hash[attribute.name] = attribute.default }
end