Class: Bluepine::Attributes::ObjectAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/bluepine/attributes/object_attribute.rb

Direct Known Subclasses

SchemaAttribute, Endpoints::Params

Constant Summary

Constants included from Bluepine::Assertions

Bluepine::Assertions::Error, Bluepine::Assertions::KeyError, Bluepine::Assertions::SubsetError

Instance Attribute Summary collapse

Attributes inherited from Attribute

#name

Instance Method Summary collapse

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

#validators

Methods included from Validators::Normalizable

#normalize

Methods included from Serializers::Serializable

#serialize

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, options = {}, &block)
  @name       = name
  @options    = 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, options = {}, &block)
  if Attributes.registry.key?(type)
    @attributes[name] = Attributes.create(type, name, options, &block)
  else
    super
  end
end

Instance Attribute Details

#attributesObject (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(options, &block)
  return unless block_given?

  # Use stacks to allow nested conditions
  self.class.stacks << Attribute.options
  Attribute.options = options

  instance_exec(&block)

  # restore options
  Attribute.options = self.class.stacks.pop
end

#keysObject



66
67
68
# File 'lib/bluepine/attributes/object_attribute.rb', line 66

def keys
  @attributes.keys
end

#native_typeObject



18
19
20
# File 'lib/bluepine/attributes/object_attribute.rb', line 18

def native_type
  "object"
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/bluepine/attributes/object_attribute.rb', line 52

def respond_to_missing?(method, *)
  super
end