Class: Bzsnippets::Object::EnvAttribute::EnvProxy

Inherits:
Delegator
  • Object
show all
Defined in:
lib/bzsnippets/core_ext/object/env_attribute.rb

Instance Method Summary collapse

Constructor Details

#initialize(value, opts = {}) ⇒ EnvProxy

Returns a new instance of EnvProxy.



13
14
15
16
17
18
# File 'lib/bzsnippets/core_ext/object/env_attribute.rb', line 13

def initialize(value, opts={})
  super(value)
  @delegate_sd_obj = value
  opts[:env] = lambda{opts[:env]} if opts[:env].kind_of? String
  @options = opts
end

Instance Method Details

#__getobj__Object



24
25
26
27
28
# File 'lib/bzsnippets/core_ext/object/env_attribute.rb', line 24

def __getobj__
  env = @options[:env].call
  hsh = all_envs
  (hsh.kind_of?(Hash) && hsh[env]) || nil
end

#__setobj__(obj) ⇒ Object



20
21
22
# File 'lib/bzsnippets/core_ext/object/env_attribute.rb', line 20

def __setobj__(obj)
  @delegate_sd_obj = obj
end

#all_envsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bzsnippets/core_ext/object/env_attribute.rb', line 30

def all_envs
  if @options[:source].kind_of? Proc
    got_by_proc = if @options[:source].arity == 1
      @options[:source].call(@options[:self])
    else
      @options[:source].call
    end
    unless got_by_proc.kind_of? Hash
      @delegate_sd_obj ||= fetch_source(got_by_proc)
    else
      got_by_proc
    end
  else
    @delegate_sd_obj ||= fetch_source(@options[:source])
  end
end