Class: Attributor::Struct
- 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
-
.construct(attribute_definition, options = {}) ⇒ Object
Construct a new subclass, using attribute_definition to define attributes.
- .constructable? ⇒ Boolean
- .definition ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two structs are equal if their attributes are equal.
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, slice!, #to_h, valid_type?, validate, #validate, #validate_generic, #validate_keys, #value_attribute, #value_type, #values
Methods included from Dumpable
Methods included from Container
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 32 33 34 35 36 37 38 39 |
# File 'lib/attributor/types/struct.rb', line 9 def self.construct(attribute_definition, = {}) # if we're in a subclass of Struct, but no 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? location_file, location_line = attribute_definition.source_location = "You cannot change an already defined Struct type:\n" += "It seems you are trying to define attributes, using a block, on top of an existing concrete Struct type that already has been fully defined.\n" += "The place where you are trying to define the type is here:\n#{location_file} line #{location_line}\n#{attribute_definition.source}\n" += "If what you meant was to define a brand new Struct or Struct[], please make sure you pass the type in the attribute definition,\n" += "rather than leaving it blank.\n" += "Otherwise, what might be happening is that you have left out the explicit type, and the framework has inferred it from the" += "corresponding :reference type attribute (and hence running into the conflict of trying to redefine an existing type)." raise AttributorException, 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 [:reference] .merge!([:reference].) do |_key, oldval, _newval| oldval end end ::Class.new(self) do attributes **, &attribute_definition end end |
.constructable? ⇒ Boolean
4 5 6 |
# File 'lib/attributor/types/struct.rb', line 4 def self.constructable? true end |
.definition ⇒ Object
41 42 43 44 45 46 |
# File 'lib/attributor/types/struct.rb', line 41 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
49 50 51 52 |
# File 'lib/attributor/types/struct.rb', line 49 def ==(other) return false if other.nil? || !other.respond_to?(:attributes) attributes == other.attributes end |