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 property methods for an entity.
Instance Attribute Summary collapse
-
#property_class ⇒ Class
readonly
The class representing the elements of the schema.
-
#property_name ⇒ String
readonly
The name of the schema elements.
Instance Method Summary collapse
-
#[](key) ⇒ Stannum::Attribute
Retrieves the named property object.
-
#define(name:, options:, type:, definition_class: nil) ⇒ Object
private
Defines an property and adds the property to the contract.
-
#each {|name, property| ... } ⇒ Object
Iterates through the the properties by name and property object.
-
#each_key {|name| ... } ⇒ Object
Iterates through the the properties by name.
-
#each_value {|property| ... } ⇒ Object
Iterates through the the properties by property object.
-
#initialize(property_class:, property_name:) ⇒ Schema
constructor
A new instance of Schema.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Checks if the given property is defined.
-
#keys ⇒ Array<String>
Returns the defined property keys.
- #own_properties ⇒ Object
-
#size ⇒ Integer
(also: #count)
The number of defined properties.
-
#values ⇒ Array<Stannum::Attribute>
Returns the defined property value.
Constructor Details
#initialize(property_class:, property_name:) ⇒ Schema
Returns a new instance of Schema.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/stannum/schema.rb', line 16 def initialize(property_class:, property_name:) super() tools.assertions.validate_class(property_class, as: 'property class') tools.assertions.validate_name(property_name, as: 'property name') @properties = {} @property_class = property_class @property_name = property_name.to_s end |
Instance Attribute Details
#property_class ⇒ Class (readonly)
Returns the class representing the elements of the schema.
28 29 30 |
# File 'lib/stannum/schema.rb', line 28 def property_class @property_class end |
#property_name ⇒ String (readonly)
Returns the name of the schema elements.
31 32 33 |
# File 'lib/stannum/schema.rb', line 31 def property_name @property_name end |
Instance Method Details
#[](key) ⇒ Stannum::Attribute
Retrieves the named property object.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/stannum/schema.rb', line 41 def [](key) tools.assertions.assert_name(key, as: 'key', error_class: ArgumentError) str = -key.to_s each_ancestor do |ancestor| next unless ancestor.own_properties.key?(str) return ancestor.own_properties[str] end {}.fetch(str) end |
#define(name:, options:, type:, definition_class: nil) ⇒ 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 property and adds the property to the contract.
This method should not be called directly. Instead, define properties via the Struct.property class method.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/stannum/schema.rb', line 63 def define(name:, options:, type:, definition_class: nil) definition_class ||= property_class property = definition_class.new(name:, options:, type:) if @properties.key?(property.name) = "#{tools.str.singularize(property_name)} #{name.inspect} " \ 'already exists' raise ArgumentError, end definition_class::Builder.new(self).call(property) @properties[property.name] = property end |
#each {|name, property| ... } ⇒ Object
Iterates through the the properties by name and property object.
85 86 87 88 89 90 91 |
# File 'lib/stannum/schema.rb', line 85 def each(&block) return enum_for(:each) { size } unless block_given? each_ancestor do |ancestor| ancestor.own_properties.each(&block) end end |
#each_key {|name| ... } ⇒ Object
Iterates through the the properties by name.
96 97 98 99 100 101 102 |
# File 'lib/stannum/schema.rb', line 96 def each_key(&block) return enum_for(:each_key) { size } unless block_given? each_ancestor do |ancestor| ancestor.own_properties.each_key(&block) end end |
#each_value {|property| ... } ⇒ Object
Iterates through the the properties by property object.
107 108 109 110 111 112 113 |
# File 'lib/stannum/schema.rb', line 107 def each_value(&block) return enum_for(:each_value) { size } unless block_given? each_ancestor do |ancestor| ancestor.own_properties.each_value(&block) end end |
#key?(key) ⇒ Boolean Also known as: has_key?
Checks if the given property is defined.
120 121 122 123 124 125 126 |
# File 'lib/stannum/schema.rb', line 120 def key?(key) tools.assertions.assert_name(key, as: 'key', error_class: ArgumentError) each_ancestor.any? do |ancestor| ancestor.own_properties.key?(key.to_s) end end |
#keys ⇒ Array<String>
Returns the defined property keys.
132 133 134 |
# File 'lib/stannum/schema.rb', line 132 def keys each_key.to_a end |
#own_properties ⇒ Object
137 138 139 |
# File 'lib/stannum/schema.rb', line 137 def own_properties @properties end |
#size ⇒ Integer Also known as: count
Returns the number of defined properties.
142 143 144 145 146 |
# File 'lib/stannum/schema.rb', line 142 def size each_ancestor.reduce(0) do |memo, ancestor| memo + ancestor.own_properties.size end end |
#values ⇒ Array<Stannum::Attribute>
Returns the defined property value.
152 153 154 |
# File 'lib/stannum/schema.rb', line 152 def values each_value.to_a end |