Class: Stannum::Schema

Inherits:
Module
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/stannum/schema.rb

Overview

Abstract class for defining attribute methods for a struct.

See Also:

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



15
16
17
18
19
# File 'lib/stannum/schema.rb', line 15

def initialize
  super

  @attributes = {}
end

Instance Method Details

#[](key) ⇒ Stannum::Attribute

Retrieves the named attribute object.

Parameters:

  • key (String, Symbol)

    The name of the requested attribute.

Returns:

Raises:

  • ArgumentError if the key is invalid.

  • KeyError if the attribute is not defined.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stannum/schema.rb', line 29

def [](key)
  tools.assertions.assert_name(key, as: 'key', error_class: ArgumentError)

  str = -key.to_s

  each_ancestor do |ancestor|
    next unless ancestor.own_attributes.key?(str)

    return ancestor.own_attributes[str]
  end

  {}.fetch(str)
end

#define_attribute(name:, options:, type:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines an attribute and adds the attribute to the contract.

This method should not be called directly. Instead, define attributes via the Struct.attribute class method.

See Also:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stannum/schema.rb', line 53

def define_attribute(name:, options:, type:)
  attribute = Stannum::Attribute.new(
    name:    name,
    options: options,
    type:    type
  )

  if @attributes.key?(attribute.name)
    raise ArgumentError, "attribute #{name.inspect} already exists"
  end

  define_reader(attribute.name, attribute.reader_name)
  define_writer(attribute.name, attribute.writer_name, attribute.default)

  @attributes[attribute.name] = attribute
end

#each {|name, attribute| ... } ⇒ Object

Iterates through the the attributes by name and attribute object.

Yield Parameters:

  • name (String)

    The name of the attribute.

  • attribute (Stannum::Attribute)

    The attribute object.



75
76
77
78
79
80
81
# File 'lib/stannum/schema.rb', line 75

def each(&block)
  return enum_for(:each) { size } unless block_given?

  each_ancestor do |ancestor|
    ancestor.own_attributes.each(&block)
  end
end

#each_key {|name| ... } ⇒ Object

Iterates through the the attributes by name.

Yield Parameters:

  • name (String)

    The name of the attribute.



86
87
88
89
90
91
92
# File 'lib/stannum/schema.rb', line 86

def each_key(&block)
  return enum_for(:each_key) { size } unless block_given?

  each_ancestor do |ancestor|
    ancestor.own_attributes.each_key(&block)
  end
end

#each_value {|attribute| ... } ⇒ Object

Iterates through the the attributes by attribute object.

Yield Parameters:



97
98
99
100
101
102
103
# File 'lib/stannum/schema.rb', line 97

def each_value(&block)
  return enum_for(:each_value) { size } unless block_given?

  each_ancestor do |ancestor|
    ancestor.own_attributes.each_value(&block)
  end
end

#key?(key) ⇒ Boolean Also known as: has_key?

Checks if the given attribute is defined.

Parameters:

  • key (String, Symbol)

    the name of the attribute to check.

Returns:

  • (Boolean)

    true if the attribute is defined; otherwise false.



110
111
112
113
114
115
116
# File 'lib/stannum/schema.rb', line 110

def key?(key)
  tools.assertions.assert_name(key, as: 'key', error_class: ArgumentError)

  each_ancestor.any? do |ancestor|
    ancestor.own_attributes.key?(key.to_s)
  end
end

#keysArray<String>

Returns the defined attribute keys.

Returns:

  • (Array<String>)

    the attribute keys.



122
123
124
# File 'lib/stannum/schema.rb', line 122

def keys
  each_key.to_a
end

#own_attributesObject



127
128
129
# File 'lib/stannum/schema.rb', line 127

def own_attributes
  @attributes
end

#sizeInteger Also known as: count

Returns the number of defined attributes.

Returns:

  • (Integer)

    the number of defined attributes.



132
133
134
135
136
# File 'lib/stannum/schema.rb', line 132

def size
  each_ancestor.reduce(0) do |memo, ancestor|
    memo + ancestor.own_attributes.size
  end
end

#valuesArray<Stannum::Attribute>

Returns the defined attribute value.

Returns:



142
143
144
# File 'lib/stannum/schema.rb', line 142

def values
  each_value.to_a
end