Class: Quonfig::Context
- Inherits:
-
Object
- Object
- Quonfig::Context
- Includes:
- Comparable
- Defined in:
- lib/quonfig/context.rb
Overview
Quonfig context: a two-level Hash (named-context → property → value) wrapped for evaluator consumption. The Evaluator accepts either a plain Hash or a Quonfig::Context — this class exists mostly to flatten lookups (‘get`) into the dotted “context-name.property” form criterion rules use.
Defined Under Namespace
Classes: NamedContext
Constant Summary collapse
- BLANK_CONTEXT_NAME =
''
Instance Attribute Summary collapse
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #blank? ⇒ Boolean
- #clear ⇒ Object
- #context(name) ⇒ Object
- #get(property_key, scope: nil) ⇒ Object
- #grouped_key ⇒ Object
-
#initialize(hash = {}) ⇒ Context
constructor
A new instance of Context.
- #set(name, hash) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Context
Returns a new instance of Context.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/quonfig/context.rb', line 35 def initialize(hash = {}) @contexts = {} @flattened = {} raise ArgumentError, 'must be a Hash' unless hash.is_a?(Hash) hash.each do |name, values| unless values.is_a?(Hash) # Legacy shorthand — pre-named-contexts callers passed a flat Hash. values = { name => values } name = BLANK_CONTEXT_NAME end @contexts[name.to_s] = NamedContext.new(name, values) values.each do |key, value| @flattened[name.to_s + '.' + key.to_s] = value end end end |
Instance Attribute Details
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
33 34 35 |
# File 'lib/quonfig/context.rb', line 33 def contexts @contexts end |
Instance Method Details
#<=>(other) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/quonfig/context.rb', line 93 def <=>(other) if other.is_a?(Quonfig::Context) to_h <=> other.to_h else super end end |
#blank? ⇒ Boolean
55 56 57 |
# File 'lib/quonfig/context.rb', line 55 def blank? @contexts.empty? end |
#clear ⇒ Object
79 80 81 82 |
# File 'lib/quonfig/context.rb', line 79 def clear @contexts = {} @flattened = {} end |
#context(name) ⇒ Object
84 85 86 |
# File 'lib/quonfig/context.rb', line 84 def context(name) @contexts[name.to_s] || NamedContext.new(name, {}) end |
#get(property_key, scope: nil) ⇒ Object
66 67 68 69 |
# File 'lib/quonfig/context.rb', line 66 def get(property_key, scope: nil) property_key = BLANK_CONTEXT_NAME + '.' + property_key unless property_key.include?('.') @flattened[property_key] end |
#grouped_key ⇒ Object
88 89 90 |
# File 'lib/quonfig/context.rb', line 88 def grouped_key @contexts.map { |_, ctx| ctx.key }.sort.join('|') end |
#set(name, hash) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/quonfig/context.rb', line 59 def set(name, hash) @contexts[name.to_s] = NamedContext.new(name, hash) hash.each do |key, value| @flattened[name.to_s + '.' + key.to_s] = value end end |
#to_h ⇒ Object
71 72 73 |
# File 'lib/quonfig/context.rb', line 71 def to_h @contexts.transform_values(&:to_h) end |
#to_s ⇒ Object
75 76 77 |
# File 'lib/quonfig/context.rb', line 75 def to_s "#<Quonfig::Context:#{object_id} #{to_h}>" end |