Class: Levels::ConfiguredGroup

Inherits:
Object
  • Object
show all
Includes:
MethodMissing
Defined in:
lib/levels/configured_group.rb

Overview

A ConfiguredGroup is the union of one or more Groups. When a key is read, the value returned is the value from the last Level that defines that key.

Instance Method Summary collapse

Methods included from MethodMissing

#method_missing

Constructor Details

#initialize(levels, group_key, group_observer) ⇒ ConfiguredGroup

Internal: Initialize a configured group.



9
10
11
12
13
# File 'lib/levels/configured_group.rb', line 9

def initialize(levels, group_key, group_observer)
  @levels = levels
  @group_key = group_key
  @group_observer = group_observer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Levels::MethodMissing

Instance Method Details

#[](value_key) ⇒ Object

Public: Retrieve a value.

value_key - Symbol name of the value.

Returns the value stored at that key. Raises Levels::UnknownKey if the key is not defined.

Raises:



21
22
23
24
25
# File 'lib/levels/configured_group.rb', line 21

def [](value_key)
  raise UnknownKey unless self.defined?(value_key)
  values = @group_observer.observe_values(@levels, @group_key, value_key)
  values.final_value
end

#defined?(value_key) ⇒ Boolean

Public: Determine if a key is defined.

value_key - Symbol name of the key.

Returns a Boolean.

Returns:

  • (Boolean)


32
33
34
# File 'lib/levels/configured_group.rb', line 32

def defined?(value_key)
  groups.any? { |group| group.defined?(value_key) }
end

#to_enumObject

Returns an Enumerator which yields [key, value].



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/levels/configured_group.rb', line 41

def to_enum
  Enumerator.new do |y|
    value_keys = Set.new
    groups.each do |group|
      group.to_enum.each do |key, value|
        value_keys << key
      end
    end
    value_keys.each do |key|
      y << [key, self[key]]
    end
  end
end

#to_sObject



36
37
38
# File 'lib/levels/configured_group.rb', line 36

def to_s
  "<Levels::ConfiguredGroup #{@group_key}>"
end