Class: Grape::Util::BaseInheritable

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

Overview

Base for classes which need to operate with own values kept in the hash and inherited values kept in a Hash-like object.

Direct Known Subclasses

InheritableValues, StackableValues

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inherited_values = nil) ⇒ BaseInheritable



12
13
14
15
# File 'lib/grape/util/base_inheritable.rb', line 12

def initialize(inherited_values = nil)
  @inherited_values = inherited_values || {}
  @new_values = {}
end

Instance Attribute Details

#inherited_valuesObject

Returns the value of attribute inherited_values.



8
9
10
# File 'lib/grape/util/base_inheritable.rb', line 8

def inherited_values
  @inherited_values
end

#new_valuesObject

Returns the value of attribute new_values.



8
9
10
# File 'lib/grape/util/base_inheritable.rb', line 8

def new_values
  @new_values
end

Instance Method Details

#delete(*keys) ⇒ Object



17
18
19
20
21
22
# File 'lib/grape/util/base_inheritable.rb', line 17

def delete(*keys)
  keys.map do |key|
    # since delete returns the deleted value, seems natural to `map` the result
    new_values.delete key
  end
end

#initialize_copy(other) ⇒ Object



24
25
26
27
28
# File 'lib/grape/util/base_inheritable.rb', line 24

def initialize_copy(other)
  super
  self.inherited_values = other.inherited_values
  self.new_values = other.new_values.dup
end

#key?(name) ⇒ Boolean



41
42
43
# File 'lib/grape/util/base_inheritable.rb', line 41

def key?(name)
  inherited_values.key?(name) || new_values.key?(name)
end

#keysObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/grape/util/base_inheritable.rb', line 30

def keys
  if new_values.any?
    inherited_values.keys.tap do |combined|
      combined.concat(new_values.keys)
      combined.uniq!
    end
  else
    inherited_values.keys
  end
end