Sha256: 781641c1ec7df091df1a3fa4df0e7b28bbf5c008df6bd77d46ba9b03d045f158

Contents?: true

Size: 1.48 KB

Versions: 11

Compression:

Stored size: 1.48 KB

Contents

module Attributor
  class Struct < Attributor::Model
    def self.constructable?
      true
    end

    # Construct a new subclass, using attribute_definition to define attributes.
    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

    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

    # Two structs are equal if their attributes are equal
    def ==(other)
      return false if other.nil? || !other.respond_to?(:attributes)
      attributes == other.attributes
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
attributor-6.5 lib/attributor/types/struct.rb
attributor-6.4 lib/attributor/types/struct.rb
attributor-6.3 lib/attributor/types/struct.rb
attributor-6.2 lib/attributor/types/struct.rb
attributor-6.1 lib/attributor/types/struct.rb
attributor-6.0 lib/attributor/types/struct.rb
attributor-5.7 lib/attributor/types/struct.rb
attributor-5.6 lib/attributor/types/struct.rb
attributor-5.5 lib/attributor/types/struct.rb
attributor-5.4 lib/attributor/types/struct.rb
attributor-5.3 lib/attributor/types/struct.rb