Module: Objectmancy::Objectable::ClassMethods
- Defined in:
- lib/objectmancy/objectable.rb
Overview
Class methods mixed into anything including Objectable. These are where the real power of Objectmancy comes from.
Class Method Summary collapse
-
.extended(base) ⇒ Object
Basic setup of the class-level state.
Instance Method Summary collapse
-
#attribute(name, **opts) ⇒ Object
Defines an attribute usable by Objectmancy to create your object.
-
#multiples(name, **opts) ⇒ Object
Allows the definition of Arrays of items to be turns into objects.
Class Method Details
.extended(base) ⇒ Object
Basic setup of the class-level state.
33 34 35 36 37 |
# File 'lib/objectmancy/objectable.rb', line 33 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.
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 |
#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.
82 83 84 85 86 87 88 |
# File 'lib/objectmancy/objectable.rb', line 82 def multiples(name, **opts) if opts[:type].nil? raise ArgumentError, 'Multiples require the :type option' end attribute(name, opts.merge(multiple: true)) end |