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?, #to_hash, #validate

Methods inherited from Hash

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

Methods included from Dumpable

#dump

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.



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

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:



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

def self.constructable?
  true
end

.definitionObject



33
34
35
36
37
38
# 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
  raise AttributorException, 'Can not use a pure Struct without defining sub-attributes' if self == Attributor::Struct

  super
end

Instance Method Details

#==(other) ⇒ Object

Two structs are equal if their attributes are equal



41
42
43
44
# File 'lib/attributor/types/struct.rb', line 41

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