Module: SmartEnv

Defined in:
lib/smart_env.rb,
lib/smart_env/version.rb,
lib/smart_env/uri_proxy.rb

Defined Under Namespace

Classes: UriProxy

Constant Summary collapse

VERSION =
"2.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#registryObject

Returns the value of attribute registry.



5
6
7
# File 'lib/smart_env.rb', line 5

def registry
  @registry
end

Class Method Details

.extended(base) ⇒ Object

Raises:

  • (RuntimeError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smart_env.rb', line 33

def self.extended(base)
  raise RuntimeError.new("#{base.inspect} doesn't respond to #[]") \
    unless base.respond_to? :[]

  class << base
    alias_method :__get, :[]  

    def [](key)
      value = __get(key)
      registry.each do |klass, condition|
        result = condition.call(key, value) rescue false
        value  = klass.new(key, value) if value && result
      end
      value
    end
  end
end

Instance Method Details

#clear_registryObject



7
8
9
# File 'lib/smart_env.rb', line 7

def clear_registry
  @registry = empty_registry
end

#empty_registryObject



11
12
13
# File 'lib/smart_env.rb', line 11

def empty_registry
  []
end

#use(klass) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/smart_env.rb', line 15

def use(klass)
  @class = klass
  if @class.respond_to? :when
    registry << [@class, lambda { |k,v| @class.when(k,v)  }]
  end
  self
end

#when(&block) ⇒ Object



23
24
25
26
27
# File 'lib/smart_env.rb', line 23

def when(&block)
  raise "Block must take 0 or 2 arguments: key and value" unless (block.arity < 1 || block.arity == 2)
  registry << [@class, block]
  self
end