Class: Attributor::Struct

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

Constant Summary

Constants inherited from Model

Model::CIRCULAR_REFERENCE_MARKER, Model::MAX_EXAMPLE_DEPTH

Instance Attribute Summary

Attributes inherited from Model

#dumping, #lazy_attributes, #validating

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

attributes, #attributes, check_option!, define_accessors, define_reader, define_writer, describe, dsl_class, dump, #dump, example, from_hash, inherited, #initialize, load, #method_missing, native_type, #respond_to_missing?, validate, #validate

Methods included from Type

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.



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

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

.definitionObject



33
34
35
36
37
38
39
40
# File 'lib/attributor/types/struct.rb', line 33

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



44
45
46
47
# File 'lib/attributor/types/struct.rb', line 44

def ==(other_object)
  return false if other_object == nil
  self.attributes == other_object.attributes
end