Class: Reference

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, binding) ⇒ Reference

Returns a new instance of Reference.



36
37
38
39
40
41
42
43
# File 'lib/refr.rb', line 36

def initialize (name, binding)
  begin
    @getter = Kernel::eval("lambda { #{name} }", binding)
    @setter = Kernel::eval("lambda { |v| #{name} = v }", binding)
  rescue Exception => e
    Kernel::raise NameError, "#{name} isn't a valid variable name"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



53
54
55
# File 'lib/refr.rb', line 53

def method_missing (id, *args, &block)
  ___get_referenced___.__send__(id, *args, &block)
end

Class Method Details

.[](value, force = false) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/refr.rb', line 27

def [] (value, force=false)
  if (value.___is_a_reference___ rescue false) && !force
    value
  else
    self.local { :value }
  end
end

.local(&block) ⇒ Object



23
24
25
# File 'lib/refr.rb', line 23

def local (&block)
  self.new(block.call, block.binding)
end

.normalize(value) ⇒ Object Also known as: -



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

def normalize (value)
  if (value.___is_a_reference___ rescue false)
    value.___get_referenced___
  else
    value
  end
end

Instance Method Details

#___get_referenced___Object Also known as: ~



45
46
47
# File 'lib/refr.rb', line 45

def ___get_referenced___
  @getter.call
end

#___is_a_reference___Object



57
# File 'lib/refr.rb', line 57

def ___is_a_reference___; true; end

#___set_referenced___(val) ⇒ Object Also known as: =~



49
50
51
# File 'lib/refr.rb', line 49

def ___set_referenced___ (val)
  @setter.call(val)
end