Class: Cockpit::Settings::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/cockpit/core/spec.rb

Overview

settings have one direct definition and many child definitions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Spec

Returns a new instance of Spec.



7
8
9
10
11
# File 'lib/cockpit/core/spec.rb', line 7

def initialize(options = {}, &block)
  @name     = options[:name]
  @store    = options[:store]
  @roots    = Cockpit::Settings::Definition.define!(options, &block)
end

Instance Attribute Details

#nameObject (readonly)

for “User”



5
6
7
# File 'lib/cockpit/core/spec.rb', line 5

def name
  @name
end

#rootsObject (readonly)

for “User”



5
6
7
# File 'lib/cockpit/core/spec.rb', line 5

def roots
  @roots
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
# File 'lib/cockpit/core/spec.rb', line 38

def [](key)
  definition(key).value rescue nil
end

#all_keysObject

returns all keys, even the ones defining new scope



21
22
23
# File 'lib/cockpit/core/spec.rb', line 21

def all_keys
  @all_keys ||= roots.map(&:all_keys).flatten
end

#definition(key) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
# File 'lib/cockpit/core/spec.rb', line 42

def definition(key)
  key = key.to_s
  return nil if key.empty?
  roots.each do |root|
    value = root.child(key)
    return value unless value.nil?
  end
  raise ArgumentError.new("Settings '#{name}' doesn't have key '#{key}'")
end

#each(&block) ⇒ Object



29
30
31
# File 'lib/cockpit/core/spec.rb', line 29

def each(&block)
  roots.each { |root| root.each(&block) }
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cockpit/core/spec.rb', line 25

def has_key?(key)
  all_keys.include?(key.to_s)
end

#keysObject

only returns keys that aren’t defining a new scope. so site { title “Hello”; pages 10 } would just return [“site.title”, “site.pages”], excluding “site”



16
17
18
# File 'lib/cockpit/core/spec.rb', line 16

def keys
  @keys ||= roots.map(&:keys).flatten
end

#map(&block) ⇒ Object Also known as: collect



33
34
35
# File 'lib/cockpit/core/spec.rb', line 33

def map(&block)
  roots.map { |root| root.map(&block) }
end

#to_hashObject



52
53
54
55
56
57
# File 'lib/cockpit/core/spec.rb', line 52

def to_hash
  keys.inject({}) do |hash, key|
    hash[key] = self[key]
    hash
  end
end

#to_treeObject



59
60
61
# File 'lib/cockpit/core/spec.rb', line 59

def to_tree
  roots.map(&:to_tree)
end