Method: Objectmancy::Objectable::ClassMethods#attribute

Defined in:
lib/objectmancy/objectable.rb

#attribute(name, **opts) ⇒ Object

Defines an attribute usable by Objectmancy to create your object. Only attributes defined with this method will be converted to attributes on the final object.

Parameters:

  • name (#to_sym)

    Attribute name

  • opts (Hash)

    Options to be applied

Options Hash (**opts):

  • :type (Symbol, Class)

    The type of object to create. Can be either a Symbol of one of: :datetime; else, a Class

  • :objectable (Symbol, String)

    Method to call on :type. Will default to calling :new. Ignored if :type is one of the known types. Requires :type option.

Raises:

  • (AttributeAlreadyDefinedError)

    Attempt to define two attributes of the same name

  • (ArgumentError)

    Pass in :objectable without :type



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/objectmancy/objectable.rb', line 53

def attribute(name, **opts)
  symbolized_name = name.to_sym

  if registered_attributes.key? symbolized_name
    raise AttributeAlreadyDefinedError, name
  end

  if opts[:objectable] && opts[:type].nil?
    raise ArgumentError, ':objectable option reuqires :type option'
  end

  registered_attributes[symbolized_name] = AttributeOptions.new(opts)

  attr_accessor symbolized_name
end