class ComplexConfig::Settings

Attributes

name_prefix[RW]
table[R]

Public Class Methods

[](*a) click to toggle source
# File lib/complex_config/settings.rb, line 10
def [](*a)
  from_hash(*a)
end
build(name, hash) click to toggle source
# File lib/complex_config/settings.rb, line 29
def build(name, hash)
  name.nil? or self.name_prefix = name.to_sym
  from_hash(hash)
ensure
  self.name_prefix = nil
end
from_hash(object) click to toggle source
# File lib/complex_config/settings.rb, line 14
def from_hash(object)
  case
  when object.respond_to?(:to_hash)
    result = new
    object.to_hash.each do |key, value|
      result[key] = from_hash(value)
    end
    result
  when object.respond_to?(:to_ary)
    object.to_ary.map { |a| from_hash(a) }
  else
    object
  end
end
new(hash = nil) click to toggle source
# File lib/complex_config/settings.rb, line 43
def initialize(hash = nil)
  self.name_prefix = self.class.name_prefix
  @table = {}
  if hash
    hash.each_pair do |k, v|
      k = k.to_sym
      @table[k] = v
    end
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/complex_config/settings.rb, line 113
def ==(other)
  other.respond_to?(:to_h) && to_h == other.to_h
end
[](name)
Alias for: attribute_get
[]=(name, value) click to toggle source
# File lib/complex_config/settings.rb, line 210
def []=(name, value)
  @table[name.to_sym] = value
end
attribute_get(name) click to toggle source
# File lib/complex_config/settings.rb, line 190
def attribute_get(name)
  if !attribute_set?(name) and
    value = ::ComplexConfig::Provider.apply_plugins(self, name)
  then
    value
  else
    @table[name.to_sym]
  end
end
Also aliased as: []
attribute_get!(name) click to toggle source
# File lib/complex_config/settings.rb, line 202
def attribute_get!(name)
  if attribute_set?(name)
    attribute_get(name)
  else
    raise ::ComplexConfig::AttributeMissing, "no attribute named #{name.inspect}"
  end
end
attribute_names() click to toggle source
# File lib/complex_config/settings.rb, line 64
def attribute_names
  @table.keys
end
attribute_set?(name) click to toggle source
# File lib/complex_config/settings.rb, line 60
def attribute_set?(name)
  @table.key?(name.to_sym)
end
attribute_values() click to toggle source
# File lib/complex_config/settings.rb, line 68
def attribute_values
  @table.values
end
attributes_update(other) click to toggle source
# File lib/complex_config/settings.rb, line 72
def attributes_update(other)
  unless other.is_a? self.class
    other = self.class.from_hash(other)
  end
  @table.update(other.table)
end
attributes_update_if_nil(other) click to toggle source
# File lib/complex_config/settings.rb, line 79
def attributes_update_if_nil(other)
  unless other.is_a? self.class
    other = self.class.from_hash(other)
  end
  @table.update(other.table) do |key, oldval, newval|
    @table.key?(key) ? oldval : newval
  end
end
deep_freeze() click to toggle source
# File lib/complex_config/settings.rb, line 183
def deep_freeze
  table_enumerator.each do |_, v|
    v.ask_and_send(:deep_freeze) || (v.freeze rescue v)
  end
  freeze
end
each(&block) click to toggle source
# File lib/complex_config/settings.rb, line 214
def each(&block)
  table_enumerator.each(&block)
end
empty?() click to toggle source
# File lib/complex_config/settings.rb, line 129
def empty?
  size == 0
end
freeze() click to toggle source
Calls superclass method
# File lib/complex_config/settings.rb, line 178
def freeze
  @table.freeze
  super
end
initialize_copy(orig) click to toggle source
Calls superclass method
# File lib/complex_config/settings.rb, line 54
def initialize_copy(orig)
  super
  @table = @table.dup
  self
end
inspect(pair_sep: ' = ', path_sep: ?.)
Alias for: to_s
pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {}) click to toggle source
# File lib/complex_config/settings.rb, line 140
def pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {})
  hash.each do |key, value|
    path = prefix.empty? ? key.to_s : "#{prefix}#{path_sep}#{key}"
    case value
    when ::ComplexConfig::Settings
      pathes(
        value,
        path_sep: path_sep,
        prefix:   path,
        result:   result
      )
    when ::Array
      value.each_with_index do |v, i|
        sub_path = path + "[#{i}]"
        if ::ComplexConfig::Settings === v
          pathes(
            v,
            path_sep: path_sep,
            prefix:   sub_path,
            result:   result
          )
        else
          result[sub_path] = v
        end
      end
    else
      result[path] = value
    end
  end
  result
end
pretty_print(q) click to toggle source
# File lib/complex_config/settings.rb, line 174
def pretty_print(q)
  q.text inspect
end
replace_attributes(hash) click to toggle source
# File lib/complex_config/settings.rb, line 88
def replace_attributes(hash)
  @table = self.class.from_hash(hash).table
  self
end
size() click to toggle source
# File lib/complex_config/settings.rb, line 125
def size
  each.count
end
to_h() click to toggle source

def write_config(encrypt: false, store_key: false)

::ComplexConfig::Provider.write_config(
  name_prefix, self, encrypt: encrypt, store_key: store_key
)
self

end

# File lib/complex_config/settings.rb, line 100
def to_h
  table_enumerator.each_with_object({}) do |(k, v), h|
    h[k] =
      if ::Array === v
        v.to_ary.map { |x| (x.ask_and_send(:to_h) rescue x) || x }
      elsif v.respond_to?(:to_h)
        v.ask_and_send(:to_h) rescue v
      else
        v
      end
  end
end
to_json(*a) click to toggle source
# File lib/complex_config/settings.rb, line 121
def to_json(*a)
  to_h.to_json(*a)
end
to_s(pair_sep: ' = ', path_sep: ?.) click to toggle source
# File lib/complex_config/settings.rb, line 133
def to_s(pair_sep: ' = ', path_sep: ?.)
  empty? and return self.class.name
  pathes(path_sep: path_sep).inject('') do |result, (path, value)|
    result + "#{path}#{pair_sep}#{value.inspect}\n"
  end
end
Also aliased as: inspect
to_yaml() click to toggle source
# File lib/complex_config/settings.rb, line 117
def to_yaml
  to_h.to_yaml
end

Private Instance Methods

method_missing(id, *a, &b) click to toggle source
# File lib/complex_config/settings.rb, line 236
def method_missing(id, *a, &b)
  case
  when id =~ /\?\z/
    begin
      public_send $`.to_sym, *a, &b
    rescue ::ComplexConfig::AttributeMissing
      nil
    end
  when id =~ /=\z/
    @table[$`.to_sym] = a.first
  when value = ::ComplexConfig::Provider.apply_plugins(self, id)
    value
  else
    if attribute_set?(id)
      @table[id]
    else
      raise ::ComplexConfig::AttributeMissing, "no attribute named #{id.inspect}"
    end
  end
end
respond_to_missing?(id, include_private = false) click to toggle source
# File lib/complex_config/settings.rb, line 228
def respond_to_missing?(id, include_private = false)
  id =~ /\?\z/ || attribute_set?(id)
end
skip() click to toggle source
# File lib/complex_config/settings.rb, line 232
def skip
  throw :skip
end
table_enumerator() click to toggle source
# File lib/complex_config/settings.rb, line 224
def table_enumerator
  @table.enum_for(:each)
end