Class: ComplexConfig::Settings

Inherits:
JSON::GenericObject
  • Object
show all
Extended by:
Tins::ThreadLocal
Defined in:
lib/complex_config/settings.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Settings

Returns a new instance of Settings.



25
26
27
28
# File 'lib/complex_config/settings.rb', line 25

def initialize(*)
  self.name_prefix = self.class.name_prefix
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *a, &b) ⇒ Object (private)



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/complex_config/settings.rb', line 121

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/
    super
  when value = ComplexConfig::Provider.apply_plugins(self, id)
    value
  else
    if attribute_set?(id)
      super
    elsif table.respond_to?(id)
      table.__send__(id, *a, &b)
    else
      raise ComplexConfig::AttributeMissing, "no attribute named #{id.inspect}"
    end
  end
end

Instance Attribute Details

#name_prefix ⇒ Object

Returns the value of attribute name_prefix.



16
17
18
# File 'lib/complex_config/settings.rb', line 16

def name_prefix
  @name_prefix
end

Class Method Details

.[](*a) ⇒ Object



6
7
8
# File 'lib/complex_config/settings.rb', line 6

def self.[](*a)
  from_hash *a
end

.build(name, hash) ⇒ Object



18
19
20
21
22
23
# File 'lib/complex_config/settings.rb', line 18

def self.build(name, hash)
  name.nil? or self.name_prefix = name.to_sym
  self[hash]
ensure
  self.name_prefix = nil
end

Instance Method Details

#attribute_names ⇒ Object



34
35
36
# File 'lib/complex_config/settings.rb', line 34

def attribute_names
  table.keys
end

#attribute_set?(name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/complex_config/settings.rb', line 30

def attribute_set?(name)
  table.key?(name.to_sym)
end

#attribute_values ⇒ Object



38
39
40
# File 'lib/complex_config/settings.rb', line 38

def attribute_values
  table.values
end

#deep_freeze ⇒ Object



100
101
102
103
104
105
# File 'lib/complex_config/settings.rb', line 100

def deep_freeze
  table_enumerator.each do |_, v|
    v.ask_and_send(:deep_freeze) || (v.freeze rescue v)
  end
  freeze
end

#pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/complex_config/settings.rb', line 61

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

#to_ary(*a, &b) ⇒ Object



94
95
96
# File 'lib/complex_config/settings.rb', line 94

def to_ary(*a, &b)
  table_enumerator.__send__(:to_a, *a, &b)
end

#to_h ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/complex_config/settings.rb', line 42

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_s(pair_sep: ' = ', path_sep: ?.) ⇒ Object Also known as: inspect



55
56
57
58
59
# File 'lib/complex_config/settings.rb', line 55

def to_s(pair_sep: ' = ', path_sep: ?.)
  pathes(path_sep: path_sep).inject('') do |result, (path, value)|
    result << "#{path}#{pair_sep}#{value.inspect}\n"
  end
end