Class: Greased::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/greased/setting.rb

Constant Summary collapse

DEFAULT_OPERATOR =
"="
OPERATIONS =
{
  "<<"    => MethodCaller.new("<<"),
  "+="    => MethodCaller.new("+", :reassign => true),
  "-="    => MethodCaller.new("-", :reassign => true),
  "*="    => MethodCaller.new("*", :reassign => true),
  "="     => MethodCaller.new("="),
  "call"  => MethodCaller.new,
  "send"  => MethodCaller.new
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, target, name, value, env, parent_methods = []) ⇒ Setting

Returns a new instance of Setting.



17
18
19
20
21
22
23
24
25
# File 'lib/greased/setting.rb', line 17

def initialize(app, target, name, value, env, parent_methods = [])
  @name, @operator = name.split(/\s+/, 2)
  @value          = value
  @operator       ||= DEFAULT_OPERATOR
  @target         = target
  @app            = app
  @env            = env
  @parent_methods = parent_methods
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



15
16
17
# File 'lib/greased/setting.rb', line 15

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



15
16
17
# File 'lib/greased/setting.rb', line 15

def env
  @env
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/greased/setting.rb', line 15

def name
  @name
end

#operatorObject (readonly)

Returns the value of attribute operator.



15
16
17
# File 'lib/greased/setting.rb', line 15

def operator
  @operator
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/greased/setting.rb', line 15

def value
  @value
end

Class Method Details

.from_config(app, property_name, property_values, env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/greased/setting.rb', line 41

def from_config(app, property_name, property_values, env)
  app_target  = app.send(property_name.to_sym)
  
  property_values.map do |setting_name, setting_values|
    parents = Array.wrap(property_name)
    
    begin
      if (target = app_target.send(setting_name.to_sym)).is_a? ActiveSupport::OrderedOptions
        parents << setting_name
      else
        setting_values  = { setting_name => setting_values }
        target          = app_target
      end
      
      setting_values.map{ |key, value| new(app, target, key, value, env, parents) }
    rescue NoMethodError => ex
      puts "WARNING: Configuration section #{setting_name} doesn't exist: #{ex}"
    end
  end.flatten.compact
end

Instance Method Details

#apply!Object



32
33
34
35
36
37
# File 'lib/greased/setting.rb', line 32

def apply!
  raise "Unknown operator (#{operator}) for setting option: #{name}" unless OPERATIONS.keys.include? operator
  OPERATIONS[operator].call(@target, name, value)
  
  self
end

#serializeObject



27
28
29
30
# File 'lib/greased/setting.rb', line 27

def serialize
  #"#{(@parent_methods + [name]).join('.')} #{operator} #{PP.pp(value, '').sub(/^([\[\{])/, "\1\n").sub(/([\]\}])$/, "\n\1")})}"
  "#{(@parent_methods + [name]).join('.')} #{operator} #{PP.pp(value, '').sub(/^([\[\{])/, "\\1\n ").sub(/([\]\}])$/, "\n\\1")}"
end