Class: Scheming::Attribute::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scheming/attribute/list.rb

Overview

Attribute Collection

Defined Under Namespace

Classes: MissingAttribute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = []) ⇒ List

Returns a new instance of List.

Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scheming/attribute/list.rb', line 13

def initialize(attributes = [])
  @attributes = attributes.uniq(&:field_name).freeze

  # @type [Hash<Symbol, Scheming::Attribute>]
  @lookup =
    @attributes
    .each_with_object({}) do |attr, lookup|
      lookup[attr.field_name] = attr
    end
    .freeze
  freeze
end

Class Method Details

.emptyScheming::Attribute::List



10
# File 'lib/scheming/attribute/list.rb', line 10

def self.empty = @empty ||= new([])

Instance Method Details

#attr(key) ⇒ Scheming::Attribute Also known as: []

Parameters:

  • key (Symbol)

Returns:



28
29
30
31
32
33
34
35
36
# File 'lib/scheming/attribute/list.rb', line 28

def attr(key)
  @lookup.fetch(key) do |attr_key|
    raise MissingAttribute, "      Missing Attribute [\#{attr_key}]\n      Available Fields:\n        \#{@attributes.map(&:field_name).join(\"\\n  \")}\n    MSG\n  end\nend\n".strip!

#each {|| ... } ⇒ Object

Interface for Enumerable

Yield Parameters:



46
47
48
49
50
# File 'lib/scheming/attribute/list.rb', line 46

def each(&)
  return enum_for(:each) unless block_given?

  @attributes.each(&)
end

#requiredObject



39
# File 'lib/scheming/attribute/list.rb', line 39

def required = each.select(&:is_required)

#to_hHash<Symbol, Scheming::Attribute>

Returns:



42
# File 'lib/scheming/attribute/list.rb', line 42

def to_h = @lookup