Class: Stannum::Schema

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

Overview

Abstract class for defining property methods for an entity.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property_class:, property_name:) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • property_class (Class)

    the class representing the elements of the schema.

  • property_name (String, Symbol)

    the name of the schema elements.



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_classClass (readonly)

Returns the class representing the elements of the schema.

Returns:

  • (Class)

    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_nameString (readonly)

Returns the name of the schema elements.

Returns:

  • (String)

    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.

Parameters:

  • key (String, Symbol)

    The name of the requested property.

Returns:

Raises:

  • ArgumentError if the key is invalid.

  • KeyError if the property is not defined.



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.

See Also:



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)
    message =
      "#{tools.str.singularize(property_name)} #{name.inspect} " \
      'already exists'

    raise ArgumentError, message
  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.

Yield Parameters:

  • name (String)

    The name of the property.

  • property (Stannum::Attribute)

    The 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.

Yield Parameters:

  • name (String)

    The name of the property.



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.

Yield Parameters:



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.

Parameters:

  • key (String, Symbol)

    the name of the property to check.

Returns:

  • (Boolean)

    true if the property is defined; otherwise false.



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

#keysArray<String>

Returns the defined property keys.

Returns:

  • (Array<String>)

    the property keys.



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

def keys
  each_key.to_a
end

#own_propertiesObject



137
138
139
# File 'lib/stannum/schema.rb', line 137

def own_properties
  @properties
end

#sizeInteger Also known as: count

Returns the number of defined properties.

Returns:

  • (Integer)

    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

#valuesArray<Stannum::Attribute>

Returns the defined property value.

Returns:



152
153
154
# File 'lib/stannum/schema.rb', line 152

def values
  each_value.to_a
end