Class: Temple::OptionMap

Inherits:
MutableMap show all
Defined in:
lib/temple/map.rb

Instance Method Summary collapse

Methods inherited from ImmutableMap

#[], #each, #include?, #keys, #to_hash, #values

Constructor Details

#initialize(*map, &block) ⇒ OptionMap

Returns a new instance of OptionMap.



57
58
59
60
61
62
# File 'lib/temple/map.rb', line 57

def initialize(*map, &block)
  super(*map)
  @handler = block
  @valid = {}
  @deprecated = {}
end

Instance Method Details

#[]=(key, value) ⇒ Object



64
65
66
67
# File 'lib/temple/map.rb', line 64

def []=(key, value)
  validate_key!(key)
  super
end

#add_deprecated_keys(*keys) ⇒ Object



83
84
85
# File 'lib/temple/map.rb', line 83

def add_deprecated_keys(*keys)
  keys.flatten.each { |key| @valid[key] = @deprecated[key] = true }
end

#add_valid_keys(*keys) ⇒ Object



79
80
81
# File 'lib/temple/map.rb', line 79

def add_valid_keys(*keys)
  keys.flatten.each { |key| @valid[key] = true }
end

#deprecated_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/temple/map.rb', line 96

def deprecated_key?(key)
  @deprecated.include?(key) ||
    @map.any? {|h| h.deprecated_key?(key) if h.respond_to?(:deprecated_key?) }
end

#update(map) ⇒ Object



69
70
71
72
# File 'lib/temple/map.rb', line 69

def update(map)
  validate_map!(map)
  super
end

#valid_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/temple/map.rb', line 101

def valid_key?(key)
  include?(key) || @valid.include?(key) ||
    @map.any? {|h| h.valid_key?(key) if h.respond_to?(:valid_key?) }
end

#valid_keysObject



74
75
76
77
# File 'lib/temple/map.rb', line 74

def valid_keys
  (keys + @valid.keys +
   @map.map {|h| h.valid_keys if h.respond_to?(:valid_keys) }.compact.flatten).uniq
end

#validate_key!(key) ⇒ Object



91
92
93
94
# File 'lib/temple/map.rb', line 91

def validate_key!(key)
  @handler.call(self, key, :deprecated) if deprecated_key?(key)
  @handler.call(self, key, :invalid) unless valid_key?(key)
end

#validate_map!(map) ⇒ Object



87
88
89
# File 'lib/temple/map.rb', line 87

def validate_map!(map)
  map.to_hash.keys.each {|key| validate_key!(key) }
end