Class: SimpleConf::Conf
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- SimpleConf::Conf
- Defined in:
- lib/simpleconf/conf.rb
Instance Attribute Summary collapse
-
#__init_only__ ⇒ Object
Returns the value of attribute init_only.
-
#__instance_block__ ⇒ Object
readonly
Returns the value of attribute instance_block.
-
#__overrides__ ⇒ Object
readonly
Returns the value of attribute __overrides__.
-
#__vars__ ⇒ Object
Returns the value of attribute __vars__.
Instance Method Summary collapse
- #check_and_change_overrides(other) ⇒ Object
-
#initialize(instance_block, opts = {}) ⇒ Conf
constructor
A new instance of Conf.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #method_missing(meth, *args, &blk) ⇒ Object
Constructor Details
#initialize(instance_block, opts = {}) ⇒ Conf
Returns a new instance of Conf.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/simpleconf/conf.rb', line 5 def initialize(instance_block, opts={}) @__opts__ = opts @__vars__ = {} @__init_only__ = false @__instance_block__ = instance_block @__default_config__ = { :override => true } @__overrides__ = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 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 |
# File 'lib/simpleconf/conf.rb', line 48 def method_missing(meth, *args, &blk) fail "#{meth} was not defined in configuration setup" if @__init_only__ = (class << self; self; end) if block_given? nested_opts = @__opts__.merge(args.shift || {}) sub_config = SimpleConf.build(nested_opts, &blk) .class_eval do define_method(meth) do |*args, &blk| @__vars__[meth] end end @__vars__[meth] = sub_config return end value = args.shift opts = @__default_config__.merge(args.shift || {}) @__overrides__[meth] = opts[:override] .class_eval do define_method(meth) do |*args, &blk| local_override = ((args[1].is_a?(Hash) && !args[1][:override].nil?) && @__overrides__[meth]) ? args[1][:override] : @__overrides__[meth] @__overrides__[meth] = local_override if args.first && @__overrides__[meth] @__vars__[meth] = args.first else @__vars__[meth] end end define_method("#{meth}=") do |*args| if args.first && @__overrides__[meth] @__vars__[meth] = args.first end @__vars__[meth] end end @__vars__[meth] = value end |
Instance Attribute Details
#__init_only__ ⇒ Object
Returns the value of attribute init_only.
3 4 5 |
# File 'lib/simpleconf/conf.rb', line 3 def __init_only__ @__init_only__ end |
#__instance_block__ ⇒ Object (readonly)
Returns the value of attribute instance_block.
4 5 6 |
# File 'lib/simpleconf/conf.rb', line 4 def __instance_block__ @__instance_block__ end |
#__overrides__ ⇒ Object (readonly)
Returns the value of attribute __overrides__.
4 5 6 |
# File 'lib/simpleconf/conf.rb', line 4 def __overrides__ @__overrides__ end |
#__vars__ ⇒ Object
Returns the value of attribute __vars__.
3 4 5 |
# File 'lib/simpleconf/conf.rb', line 3 def __vars__ @__vars__ end |
Instance Method Details
#check_and_change_overrides(other) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/simpleconf/conf.rb', line 24 def check_and_change_overrides(other) self.__overrides__.each do |meth, override| if !override other.__overrides__[meth] = false other.__vars__[meth] = __vars__[meth] end end end |
#merge(other) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/simpleconf/conf.rb', line 17 def merge(other) fail "Other needs to be a SimpleConf::Conf" unless other.is_a? Conf copy = SimpleConf.build(@__opts__, &@__instance_block__) copy.merge!(other) copy end |
#merge!(other) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/simpleconf/conf.rb', line 34 def merge!(other) fail "Other needs to be a SimpleConf::Conf" unless other.is_a? Conf check_and_change_overrides(other) self.__init_only__ = other.__init_only__ if other.__instance_block__.is_a?(Proc) self.instance_eval(&other.__instance_block__) else self.instance_eval(other.__instance_block__) end __vars__.rmerge!(other.__vars__) nil end |