Class: StandardId::ConfigSchema::Scope

Inherits:
ActiveSupport::OrderedOptions
  • Object
show all
Defined in:
lib/standard_id/config_schema.rb

Overview

Per-scope OrderedOptions. Validates writes, casts and dups Array/Hash values on read. When ‘resolver` is set (via `Config#register`), reads delegate to the resolver-returned hash for dynamic / multi-tenant configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, scope_name) ⇒ Scope

Returns a new instance of Scope.



117
118
119
120
121
# File 'lib/standard_id/config_schema.rb', line 117

def initialize(schema, scope_name)
  super()
  @schema = schema
  @scope_name = scope_name
end

Instance Attribute Details

#resolverObject

Returns the value of attribute resolver.



115
116
117
# File 'lib/standard_id/config_schema.rb', line 115

def resolver
  @resolver
end

Instance Method Details

#[](key) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/standard_id/config_schema.rb', line 128

def [](key)
  sym = key.to_sym
  validate!(sym) unless key?(sym) || resolver
  raw = if resolver
          hash = resolver.call || {}
          if hash.respond_to?(:key?) && hash.respond_to?(:[])
            hash.key?(sym) ? hash[sym] : hash[sym.to_s]
          end
  elsif key?(sym)
          super(sym)
  else
          @schema.field_for(@scope_name, sym)&.default_value
  end
  cast_read(sym, raw)
end

#[]=(key, value) ⇒ Object



123
124
125
126
# File 'lib/standard_id/config_schema.rb', line 123

def []=(key, value)
  validate!(key)
  super(key.to_sym, value)
end

#write_default(key, value) ⇒ Object



144
145
146
147
# File 'lib/standard_id/config_schema.rb', line 144

def write_default(key, value)
  return if key?(key.to_sym)
  RAW_SET.bind_call(self, key.to_sym, value)
end