class Setti::Settings
Public Class Methods
new(path)
click to toggle source
# File lib/setti/settings.rb, line 5 def initialize(path) assert File.exist?(path), error: "No such file or directory: #{path}" @settings = Oj.load(IO.read(path), mode: :compat) end
Public Instance Methods
[](name)
click to toggle source
# File lib/setti/settings.rb, line 21 def [](name) with_indifferent_hash lookup_property(name) end
validate(name, *args, &block)
click to toggle source
# File lib/setti/settings.rb, line 10 def validate(name, *args, &block) value = lookup_property(name) case args.first when nil then nil_check!(name, value, block) when :number then number_check!(name, value) when :float then float_check!(name, value) when :integer then int_check!(name, value) when :regex then regex_check!(name, value, args.last) end end
Private Instance Methods
assert(condition, options = {})
click to toggle source
# File lib/setti/settings.rb, line 63 def assert(condition, options = {}) fail options[:error] unless condition end
float_check!(name, value)
click to toggle source
# File lib/setti/settings.rb, line 35 def float_check!(name, value) fail "#{name}=#{value} is not a float" unless value.is_a? Float end
int_check!(name, value)
click to toggle source
# File lib/setti/settings.rb, line 39 def int_check!(name, value) fail "#{name}=#{value} is not an integer" unless [Integer, Fixnum].include? value.class end
lookup_property(name)
click to toggle source
# File lib/setti/settings.rb, line 47 def lookup_property(name) if name.is_a?(String) && name.include?('.') lookup_value_dot_path(name) else IndifferentHash[@settings][name] end end
lookup_value_dot_path(name)
click to toggle source
# File lib/setti/settings.rb, line 55 def lookup_value_dot_path(name) names = name.split('.') names.inject(@settings) do |walker, cur_name| fail "Not found: #{name}" unless walker[cur_name] walker[cur_name] end end
nil_check!(name, value, block)
click to toggle source
# File lib/setti/settings.rb, line 27 def nil_check!(name, value, block) fail "#{name}=#{value} does not comply with the validation" unless block.call(value) end
number_check!(name, value)
click to toggle source
# File lib/setti/settings.rb, line 31 def number_check!(name, value) fail "#{name}=#{value} is not a number" unless [Integer, Fixnum, Float].include? value.class end
regex_check!(name, value, regex)
click to toggle source
# File lib/setti/settings.rb, line 43 def regex_check!(name, value, regex) fail "#{name}=#{value} does not match #{regex}" unless value =~ regex end
with_indifferent_hash(value)
click to toggle source
# File lib/setti/settings.rb, line 67 def with_indifferent_hash(value) value.is_a?(Hash) ? IndifferentHash[value] : value end