Class: PackAPI::Types::BaseType

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/pack_api/types/base_type.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filterable_attributesObject

This method returns a mapping of attribute names to ‘Dry::Type::*` objects that describe the attributes that have been designated as filterable, as in the following example:

attribute :id, Types::String.meta(filterable: true)

NOTE: According to the documentation, ‘Dry::Types::Schema::Key` is part of a private API, but we haven’t found a better way to access an attribute’s metadata or type:



39
40
41
42
43
# File 'lib/pack_api/types/base_type.rb', line 39

def self.filterable_attributes
  schema.keys.each_with_object({}) do |schema_key, result|
    result[schema_key.name] = schema_key.type if schema_key.meta[:filterable]
  end
end

.inherited(subclass) ⇒ Object



7
8
9
10
# File 'lib/pack_api/types/base_type.rb', line 7

def self.inherited(subclass)
  subclass.instance_variable_set(:@optional_attributes, @optional_attributes.dup)
  super
end

.optional_attribute(name, type = Undefined, &block) ⇒ Object



12
13
14
15
# File 'lib/pack_api/types/base_type.rb', line 12

def self.optional_attribute(name, type = Undefined, &block)
  attribute?(name, type.optional, &block)
  @optional_attributes << name
end

.optional_attributesObject



17
18
19
# File 'lib/pack_api/types/base_type.rb', line 17

def self.optional_attributes
  @optional_attributes.to_a
end

Instance Method Details

#merge(**other_attributes) ⇒ Object



26
27
28
# File 'lib/pack_api/types/base_type.rb', line 26

def merge(**other_attributes)
  self.class.new(to_h.merge(other_attributes))
end

#to_data(**other_attributes) ⇒ Object



21
22
23
24
# File 'lib/pack_api/types/base_type.rb', line 21

def to_data(**other_attributes)
  merged_attributes = to_h.merge(other_attributes)
  Data.define(*merged_attributes.keys).new(**merged_attributes)
end