Class: CheapAdvice::Configuration
- Inherits:
-
Object
- Object
- CheapAdvice::Configuration
- Defined in:
- lib/cheap_advice/configuration.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary (collapse)
-
- (Object) advice
Returns the value of attribute advice.
-
- (Object) config
Returns the value of attribute config.
Instance Method Summary (collapse)
- - (Object) configure!
- - (Object) disable!
- - (Object) enable!
- - (Object) get_config
-
- (Configuration) initialize(opts = nil)
constructor
A new instance of Configuration.
- - (Object) merge!(dst, src)
- - (Object) parse_target(x)
Constructor Details
- (Configuration) initialize(opts = nil)
A new instance of Configuration
10 11 12 13 14 15 16 17 |
# File 'lib/cheap_advice/configuration.rb', line 10 def initialize opts = nil opts ||= EMPTY_Hash opts.each do | k, v | send(:#{k}=", v) end @advice ||= { } @targets = [ ] end |
Instance Attribute Details
- (Object) advice
Returns the value of attribute advice
8 9 10 |
# File 'lib/cheap_advice/configuration.rb', line 8 def advice @advice end |
- (Object) config
Returns the value of attribute config
8 9 10 |
# File 'lib/cheap_advice/configuration.rb', line 8 def config @config end |
Instance Method Details
- (Object) configure!
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cheap_advice/configuration.rb', line 19 def configure! disable! # First pass: parse target and defaults. c = [ ] d = { } get_config.each do | target_name, target_config | t = parse_target(target_name) # puts "#{target_name.inspect} => #{t.inspect}" case target_config when true, false target_config = { :enabled => target_config } end t.update(target_config) if target_config t[:advice] = t[:advice].split(/\s+|\s*,\s*/) if String === t[:advice] t[:advice] = t[:advice].inject({}) { | h, k | h[k] = true; h } if Array === t[:advice] t[:advice] ||= { } case when t[:method].nil? && t[:mod].nil? # global default. d[nil] = t when t[:method].nil? # module default. d[t[:mod]] = t else c << t # real target end end d[nil] ||= { } # Second pass: merge defaults with target. @targets = [ ] c.each do | t | x = merge!(d[nil].dup, d[t[:mod]] || EMPTY_Hash) t = merge!(x, t) # $stderr.puts "target = #{t.inspect}" next if t[:enabled] == false @targets << t end enable! self end |
- (Object) disable!
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cheap_advice/configuration.rb', line 62 def disable! @targets.each do | t | (t[:advice] || EMPTY_Hash).each do | advice_name, enabled | advice_name = advice_name.to_sym if advised = (t[:advised] || EMPTY_Hash)[advice_name] advised.disable! end end end self end |
- (Object) enable!
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cheap_advice/configuration.rb', line 74 def enable! @targets.each do | t | (t[:advice] || EMPTY_Hash).each do | advice_name, enabled | next unless enabled advice_name = advice_name.to_sym if advice = @advice[advice_name] = t[:options][nil] = merge!(, t[:options][advice_name]) # puts "#{t.inspect} options => #{options.inspect}" (t[:advised] ||= { })[advice_name] = advice.advise!(t[:mod], t[:method], t[:kind], ) else raise Error, "no advice #{advice_name.inspect} for #{t.inspect}" end end end self end |
- (Object) get_config
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cheap_advice/configuration.rb', line 92 def get_config case @config when Hash @config when Proc @config.call(self) when nil raise Error, "no config" end end |
- (Object) merge!(dst, src)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/cheap_advice/configuration.rb', line 121 def merge! dst, src case dst when nil, Hash case src when Hash dst = dst ? dst.dup : { } src.each do | k, v | dst[k] = merge!(dst[k], v) end else dst = src end else dst = src end dst end |
- (Object) parse_target(x)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cheap_advice/configuration.rb', line 103 def parse_target x case x when nil { } when Hash x when String, Symbol if x.to_s =~ /\A([a-z0-9_:]+)(?:([#\.])([a-z0-9_]+[!?]?))?\Z/i { :mod => $1, :kind => $2 == '.' ? :module : :instance, :method => $3, } else raise Error, "cannot parse #{x.inspect}" end end end |