Class: Attributor::Struct

Inherits:
Model show all
Defined in:
lib/attributor/types/struct.rb

Constant Summary

Constants inherited from Hash

Hash::CIRCULAR_REFERENCE_MARKER, Hash::MAX_EXAMPLE_DEPTH

Instance Attribute Summary

Attributes inherited from Hash

#contents, #dumping, #validating

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#attributes, check_option!, define_accessors, define_reader, define_writer, #dump, example, generate_subcontext, inherited, #initialize, #method_missing, #respond_to_missing?, #validate

Methods inherited from Hash

#[], #[]=, #_get_attr, attributes, check_option!, #delete, describe, dsl_class, dump, #dump, #each, #empty?, example, example_contents, family, from_hash, generate_subcontext, #generate_subcontext, #get, inherited, #initialize, #key?, #key_attribute, #key_type, keys, #keys, load, #merge, native_type, of, #set, #size, valid_type?, #validate, validate, #value_attribute, #value_type, #values

Methods included from Container

included

Constructor Details

This class inherits a constructor from Attributor::Model

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Attributor::Model

Class Method Details

.construct(attribute_definition, options = {}) ⇒ Object

Construct a new subclass, using attribute_definition to define attributes.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/attributor/types/struct.rb', line 10

def self.construct(attribute_definition, options={})
  # if we're in a subclass of Struct, but not attribute_definition is provided, we're
  # not REALLY trying to define a new struct. more than likely Collection is calling
  # construct on us.
  unless self == Attributor::Struct || attribute_definition.nil?
    raise AttributorException, 'can not construct from already-constructed Struct'
  end

  # TODO: massage the options here to pull out only the relevant ones

  # simply return Struct if we don't specify any sub-attributes....
  return self if attribute_definition.nil?

  if options[:reference]
    options.merge!(options[:reference].options) do |key, oldval, newval|
      oldval
    end
  end

  ::Class.new(self) do
    attributes options, &attribute_definition
  end

end

.constructable?Boolean

Returns:


5
6
7
# File 'lib/attributor/types/struct.rb', line 5

def self.constructable?
  true
end

.definitionObject


36
37
38
39
40
41
42
43
# File 'lib/attributor/types/struct.rb', line 36

def self.definition
  # Could probably do this better, but its use should be memoized in the enclosing Attribute
  if self == Attributor::Struct
    raise AttributorException, "Can not use a pure Struct without defining sub-attributes"
  else
    super
  end
end

Instance Method Details

#==(other_object) ⇒ Object

Two structs are equal if their attributes are equal


47
48
49
50
# File 'lib/attributor/types/struct.rb', line 47

def ==(other_object)
  return false if other_object == nil || !other_object.respond_to?(:attributes)
  self.attributes == other_object.attributes
end