Class: Grape::Util::StackableValues

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/util/stackable_values.rb

Overview

A read-only view of one settings scope's stackable registrations.

Grape stores nothing here anymore: the registrations live on Grape::Util::InheritableSetting, one Array per key per scope, and are reached through its semantic accessors (+helpers+, middleware, namespaces, ...). This class survives only because grape-swagger reads InheritableSetting#namespace_stackable directly — including walking the #inherited_values chain and reading each level's own #new_values to recover per-scope registrations — and is expected to be removed once grape-swagger reads those accessors instead.

Instances are built on demand by InheritableSetting#namespace_stackable and are not the backing store: writing to #new_values does not register anything.

Constant Summary collapse

EMPTY =
[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_values, inherited_values) ⇒ StackableValues

Returns a new instance of StackableValues.

Parameters:

  • new_values (Hash, nil)

    the scope's own registrations, one Array per key; nil when the scope never registered anything.

  • inherited_values (StackableValues, Hash)

    the enclosing scope's view, or an empty Hash at the root of the chain.



28
29
30
31
# File 'lib/grape/util/stackable_values.rb', line 28

def initialize(new_values, inherited_values)
  @new_values = new_values
  @inherited_values = inherited_values
end

Instance Attribute Details

#inherited_valuesObject (readonly)

Returns the value of attribute inherited_values.



22
23
24
# File 'lib/grape/util/stackable_values.rb', line 22

def inherited_values
  @inherited_values
end

#new_valuesObject (readonly)

Returns the value of attribute new_values.



22
23
24
# File 'lib/grape/util/stackable_values.rb', line 22

def new_values
  @new_values
end

Instance Method Details

#[](name) ⇒ Object

Outermost scope first. Even if there is no value, an empty (frozen) array will be returned.



35
36
37
38
39
40
41
42
# File 'lib/grape/util/stackable_values.rb', line 35

def [](name)
  inherited_value = @inherited_values[name]
  new_value = @new_values && @new_values[name]

  return new_value || EMPTY unless inherited_value

  concat_values(inherited_value, new_value)
end

#keysObject



44
45
46
47
48
# File 'lib/grape/util/stackable_values.rb', line 44

def keys
  return @inherited_values.keys if @new_values.nil? || @new_values.empty?

  (@inherited_values.keys + @new_values.keys).uniq
end

#to_hashObject



50
51
52
53
54
# File 'lib/grape/util/stackable_values.rb', line 50

def to_hash
  keys.to_h do |key|
    [key, self[key]]
  end
end