Class: ClassMattr::Proxy

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

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Proxy

Returns a new instance of Proxy.



6
7
8
# File 'lib/lib/proxy.rb', line 6

def initialize host
  @host = host
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, value = nil) ⇒ Object



39
40
41
# File 'lib/lib/proxy.rb', line 39

def method_missing name, value=nil
  @@mattrs.push [@host.to_s, name.to_s, value]
end

Instance Method Details

#_get(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/lib/proxy.rb', line 22

def _get name
  for klass in @host.ancestors.map(&:to_s)
    return {} if klass == 'ClassMattr'

    hash = MATTRS.dig(klass, name.to_sym)
    return hash if hash
  end

  raise 'ClassMattr not included?'
end

#_set(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lib/proxy.rb', line 10

def _set name
  while el = @@mattrs.shift
    klass, trait, opts = el[0], el[1].to_sym, el[2]

    MATTRS[klass] ||= {}
    el = MATTRS[klass][name.to_sym] ||= {}

    # default value if true if no argument provided
    el[trait] = opts.nil? ? true : opts
  end
end

#get_hashObject



33
34
35
36
37
# File 'lib/lib/proxy.rb', line 33

def get_hash
  name = :__mattr_tmp
  _set name
  MATTRS[@host.to_s].delete(name)
end