Class: Stannum::Schema
- Inherits:
-
Module
- Object
- Module
- Stannum::Schema
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/stannum/schema.rb
Overview
Abstract class for defining attribute methods for a struct.
Instance Method Summary collapse
-
#[](key) ⇒ Stannum::Attribute
Retrieves the named attribute object.
-
#define_attribute(name:, options:, type:) ⇒ Object
private
Defines an attribute and adds the attribute to the contract.
-
#each {|name, attribute| ... } ⇒ Object
Iterates through the the attributes by name and attribute object.
-
#each_key {|name| ... } ⇒ Object
Iterates through the the attributes by name.
-
#each_value {|attribute| ... } ⇒ Object
Iterates through the the attributes by attribute object.
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Checks if the given attribute is defined.
-
#keys ⇒ Array<String>
Returns the defined attribute keys.
- #own_attributes ⇒ Object
-
#size ⇒ Integer
(also: #count)
The number of defined attributes.
-
#values ⇒ Array<Stannum::Attribute>
Returns the defined attribute value.
Constructor Details
#initialize ⇒ Schema
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.
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.
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: , 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.
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.
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.
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.
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 |
#keys ⇒ Array<String>
Returns the defined attribute keys.
122 123 124 |
# File 'lib/stannum/schema.rb', line 122 def keys each_key.to_a end |
#own_attributes ⇒ Object
127 128 129 |
# File 'lib/stannum/schema.rb', line 127 def own_attributes @attributes end |
#size ⇒ Integer Also known as: count
Returns 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 |
#values ⇒ Array<Stannum::Attribute>
Returns the defined attribute value.
142 143 144 |
# File 'lib/stannum/schema.rb', line 142 def values each_value.to_a end |