Class: Bluepine::Attributes::ObjectAttribute
- Defined in:
- lib/bluepine/attributes/object_attribute.rb
Direct Known Subclasses
Constant Summary
Constants included from Bluepine::Assertions
Bluepine::Assertions::Error, Bluepine::Assertions::KeyError, Bluepine::Assertions::SubsetError
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Attributes inherited from Attribute
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, attribute) ⇒ Object
-
#group(options, &block) ⇒ Object
Apply default options to all attributes.
-
#initialize(name, options = {}, &block) ⇒ ObjectAttribute
constructor
A new instance of ObjectAttribute.
- #keys ⇒ Object
-
#method_missing(type, name = nil, options = {}, &block) ⇒ Object
Shortcut for creating attribute (delegate call to Registry.create) This allows us to access newly registered attributes.
- #native_type ⇒ Object
- #respond_to_missing?(method) ⇒ Boolean
Methods inherited from Attribute
#default, #deprecated, #description, #format, #if, #in, #match, #method, #null, #of, #options, #private, #required, #serializable?, #spec, #spec_uri, #type, #unless, #value
Methods included from Validators::Validatable
Methods included from Validators::Normalizable
Methods included from Serializers::Serializable
Methods included from Bluepine::Assertions
#assert, #assert_in, #assert_kind_of, #assert_not, #assert_subset_of, included
Constructor Details
#initialize(name, options = {}, &block) ⇒ ObjectAttribute
Returns a new instance of ObjectAttribute.
11 12 13 14 15 16 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 11 def initialize(name, = {}, &block) @name = name @options = @attributes = {} instance_exec(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, name = nil, options = {}, &block) ⇒ Object
Shortcut for creating attribute (delegate call to Registry.create) This allows us to access newly registered attributes
string :username (or array, number etc)
44 45 46 47 48 49 50 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 44 def method_missing(type, name = nil, = {}, &block) if Attributes.registry.key?(type) @attributes[name] = Attributes.create(type, name, , &block) else super end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 7 def attributes @attributes end |
Instance Method Details
#[](name) ⇒ Object
56 57 58 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 56 def [](name) @attributes[name.to_sym] end |
#[]=(name, attribute) ⇒ Object
60 61 62 63 64 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 60 def []=(name, attribute) assert_kind_of Attribute, attribute @attributes[name.to_sym] = attribute end |
#group(options, &block) ⇒ Object
Apply default options to all attributes
group if: :deleted? { … } group unless: :deleted? { … } group if: ->{ @user.deleted? } { … }
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 27 def group(, &block) return unless block_given? # Use stacks to allow nested conditions self.class.stacks << Attribute. Attribute. = instance_exec(&block) # restore options Attribute. = self.class.stacks.pop end |
#keys ⇒ Object
66 67 68 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 66 def keys @attributes.keys end |
#native_type ⇒ Object
18 19 20 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 18 def native_type "object" end |
#respond_to_missing?(method) ⇒ Boolean
52 53 54 |
# File 'lib/bluepine/attributes/object_attribute.rb', line 52 def respond_to_missing?(method, *) super end |