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
-
.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.
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.
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.
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 |