Class: ActiveConfig::Suffixes

Inherits:
Object
  • Object
show all
Defined in:
lib/active_config/suffixes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Suffixes

Returns a new instance of Suffixes.



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
61
# File 'lib/active_config/suffixes.rb', line 27

def initialize(*args)
  ac_instance=args.shift
  @symbols=HashWithHooks.new
  @symbols[:hostname]=proc {|sym_table| ENV['ACTIVE_CONFIG_HOSTNAME'] ||
   Socket.gethostname
  } 
  @symbols[:hostname_short]=proc {|sym_table| sym_table[:hostname].call(sym_table).sub(/\..*$/, '').freeze}
  @symbols[:rails_env]=proc { |sym_table| 
    if defined?(RAILS_ENV)
      RAILS_ENV
    elsif defined?(Rails) and Rails.respond_to?(:env)
      Rails.env
    elsif ENV['ACTIVE_CONFIG_FORCE_RAILS_ENV']
      ENV['ACTIVE_CONFIG_FORCE_RAILS_ENV']  
    else
      ENV['RAILS_ENV']
    end
  }
  @symbols[:overlay]=proc { |sym_table| ENV['ACTIVE_CONFIG_OVERLAY']}
  @symbols.add_write_hook do
    ac_instance.flush_cache
  end
  @priority=[
   nil,
   :rails_env,
   [:rails_env,:local],
   :overlay,
   [:overlay,:local],
   [:hostname_short],
   [:hostname_short, :local],
   :hostname,
   [:hostname, :local],
   :local,
  ]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, val = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_config/suffixes.rb', line 62

def method_missing method, val=nil 
  super if method.to_s=~/^_/
  if method.to_s=~/^(.*)=$/
    ac_instance._flush_cache
    return @symbols[$1]=val
  end      
  ret=@symbols[method]
  if ret 
    return ret.call(@symbols) if ret.respond_to?(:call)
    return ret
  end
  super
end

Instance Attribute Details

#ac_instanceObject

Returns the value of attribute ac_instance.



20
21
22
# File 'lib/active_config/suffixes.rb', line 20

def ac_instance
  @ac_instance
end

#priority=(value) ⇒ Object (writeonly)

Sets the attribute priority

Parameters:

  • value

    the value to set the attribute priority to.



19
20
21
# File 'lib/active_config/suffixes.rb', line 19

def priority=(value)
  @priority = value
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



21
22
23
# File 'lib/active_config/suffixes.rb', line 21

def symbols
  @symbols
end

Instance Method Details

#file(fname) ⇒ Object



90
91
92
93
94
# File 'lib/active_config/suffixes.rb', line 90

def file fname
  suffixes.map{|m|m="#{m}" if m
"#{fname}#{m}.yml"
}
end

#for(file) ⇒ Object



75
76
77
# File 'lib/active_config/suffixes.rb', line 75

def for file
  suffixes.map { |this_suffix| [file,*this_suffix].compact.join('_')}.compact.uniq
end

#overlay=(new_overlay) ⇒ Object



23
24
25
26
# File 'lib/active_config/suffixes.rb', line 23

def overlay= new_overlay
  ac_instance._flush_cache
  @symbols[:overlay]=(new_overlay.respond_to?(:upcase) ? new_overlay.upcase : new_overlay)
end

#suffixes(ary = @priority) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_config/suffixes.rb', line 78

def suffixes ary=@priority
  ary.map{|e|
    if Array===e
      t=self.suffixes(e).compact
      t.size > 0 ? t.join('_') : nil
    elsif @symbols[e]
      method_missing(e)
    else
      e && e.to_s
    end
  }
end