Class: Confset::Options

Inherits:
OpenStruct
  • Object
show all
Includes:
Validation::Validate, Enumerable
Defined in:
lib/confset/options.rb

Constant Summary collapse

SETTINGS_RESERVED_NAMES =

Some keywords that don’t play nicely with OpenStruct

%w[select collect test count zip min max exit! table].freeze

Instance Method Summary collapse

Methods included from Validation::Validate

#validate!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



144
145
146
147
148
149
# File 'lib/confset/options.rb', line 144

def method_missing(method_name, *args)
  if Confset.fail_on_missing && method_name !~ /.*(?==\z)/m
    raise KeyError, "key not found: #{method_name.inspect}" unless key?(method_name)
  end
  super
end

Instance Method Details

#[](param) ⇒ Object

An alternative mechanism for property access. This let’s you do foo along with foo.bar.



121
122
123
124
# File 'lib/confset/options.rb', line 121

def [](param)
  return super if SETTINGS_RESERVED_NAMES.include?(param)
  send("#{param}")
end

#[]=(param, value) ⇒ Object



126
127
128
# File 'lib/confset/options.rb', line 126

def []=(param, value)
  send("#{param}=", value)
end

#add_source!(source) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/confset/options.rb', line 19

def add_source!(source)
  # handle yaml file paths
  source = (Sources::YAMLSource.new(source)) if source.is_a?(String) || source.is_a?(::Pathname)
  source = (Sources::HashSource.new(source)) if source.is_a?(Hash)

  @config_sources ||= []
  @config_sources << source
end

#as_json(options = nil) ⇒ Object



97
98
99
# File 'lib/confset/options.rb', line 97

def as_json(options = nil)
  to_hash.as_json(options)
end

#each(*args, &block) ⇒ Object



88
89
90
# File 'lib/confset/options.rb', line 88

def each(*args, &block)
  marshal_dump.each(*args, &block)
end

#empty?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/confset/options.rb', line 15

def empty?
  marshal_dump.empty?
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/confset/options.rb', line 140

def has_key?(key)
  @table.has_key?(key)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/confset/options.rb', line 136

def key?(key)
  @table.key?(key)
end

#keysObject



11
12
13
# File 'lib/confset/options.rb', line 11

def keys
  marshal_dump.keys
end

#merge!(hash) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/confset/options.rb', line 101

def merge!(hash)
  current = to_hash
  DeepMerge.deep_merge!(
                        hash.dup,
                        current,
                        preserve_unmergeables: false,
                        knockout_prefix:       Confset.knockout_prefix,
                        overwrite_arrays:      Confset.overwrite_arrays,
                        merge_nil_values:      Confset.merge_nil_values,
                        merge_hash_arrays:     Confset.merge_hash_arrays
                       )
  marshal_load(__convert(current).marshal_dump)
  self
end

#prepend_source!(source) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/confset/options.rb', line 28

def prepend_source!(source)
  source = (Sources::YAMLSource.new(source)) if source.is_a?(String) || source.is_a?(::Pathname)
  source = (Sources::HashSource.new(source)) if source.is_a?(Hash)

  @config_sources ||= []
  @config_sources.unshift(source)
end

#reload!Object Also known as: load!

look through all our sources and rebuild the configuration



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/confset/options.rb', line 37

def reload!
  conf = {}
  @config_sources.each do |source|
    source_conf = source.load

    if conf.empty?
      conf = source_conf
    else
      DeepMerge.deep_merge!(
                            source_conf,
                            conf,
                            preserve_unmergeables: false,
                            knockout_prefix:       Confset.knockout_prefix,
                            overwrite_arrays:      Confset.overwrite_arrays,
                            merge_nil_values:      Confset.merge_nil_values,
                            merge_hash_arrays:     Confset.merge_hash_arrays
                           )
    end
  end

  # swap out the contents of the OStruct with a hash (need to recursively convert)
  marshal_load(__convert(conf).marshal_dump)

  validate!

  self
end

#reload_from_files(*files) ⇒ Object



67
68
69
70
# File 'lib/confset/options.rb', line 67

def reload_from_files(*files)
  Confset.load_and_set_settings(files)
  reload!
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/confset/options.rb', line 151

def respond_to_missing?(*args)
  super
end

#to_hashObject Also known as: to_h



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/confset/options.rb', line 72

def to_hash
  result = {}
  marshal_dump.each do |k, v|
    if v.instance_of? Confset::Options
      result[k] = v.to_hash
    elsif v.instance_of? Array
      result[k] = descend_array(v)
    else
      result[k] = v
    end
  end
  result
end

#to_json(*args) ⇒ Object



92
93
94
95
# File 'lib/confset/options.rb', line 92

def to_json(*args)
  require "json" unless defined?(JSON)
  to_hash.to_json(*args)
end