Module: Objectmancy::CommonClassMethods

Defined in:
lib/objectmancy/common_class_methods.rb

Overview

Class methods mixed into anything including one of the mixins for Objectmancy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Basic setup of the class-level state.



11
12
13
14
15
# File 'lib/objectmancy/common_class_methods.rb', line 11

def self.extended(base)
  base.instance_variable_set(:@registered_attributes, {})
  base.class.send(:attr_reader, :registered_attributes)
  base.send(:private_class_method, :attribute, :multiples)
end

Instance Method Details

#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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/objectmancy/common_class_methods.rb', line 31

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

#multiples(name, **opts) ⇒ Object

Allows the definition of Arrays of items to be turns into objects. Bear in mind that Arrays of basic objects (Strings, numbers, anything else that doesn’t need special initialization) are handled by .attribute.

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.

Raises:

  • (AttributeAlreadyDefinedError)

    Attempt to define two attributes of the same name

  • (ArgumentError)

    Calling .multiples without :type option for non-Hashable objects.



61
62
63
64
65
66
67
# File 'lib/objectmancy/common_class_methods.rb', line 61

def multiples(name, **opts)
  if !ancestors.include?(Objectmancy::Hashable) && opts[:type].nil?
    raise ArgumentError, 'Multiples require the :type option'
  end

  attribute(name, opts.merge(multiple: true))
end