Class: Cockpit::Settings::Spec
- Inherits:
-
Object
- Object
- Cockpit::Settings::Spec
- Defined in:
- lib/cockpit/core/spec.rb
Overview
settings have one direct definition and many child definitions
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
for “User”.
-
#roots ⇒ Object
readonly
for “User”.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#all_keys ⇒ Object
returns all keys, even the ones defining new scope.
- #definition(key) ⇒ Object
- #each(&block) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(options = {}, &block) ⇒ Spec
constructor
A new instance of Spec.
-
#keys ⇒ Object
only returns keys that aren’t defining a new scope.
- #map(&block) ⇒ Object (also: #collect)
- #to_hash ⇒ Object
- #to_tree ⇒ Object
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( = {}, &block) @name = [:name] @store = [:store] @roots = Cockpit::Settings::Definition.define!(, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
for “User”
5 6 7 |
# File 'lib/cockpit/core/spec.rb', line 5 def name @name end |
#roots ⇒ Object (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_keys ⇒ Object
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
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
25 26 27 |
# File 'lib/cockpit/core/spec.rb', line 25 def has_key?(key) all_keys.include?(key.to_s) end |
#keys ⇒ Object
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_hash ⇒ Object
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_tree ⇒ Object
59 60 61 |
# File 'lib/cockpit/core/spec.rb', line 59 def to_tree roots.map(&:to_tree) end |