Class: DynamicVariable

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

Constant Summary collapse

@@un =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(*pairs, &block) ⇒ DynamicVariable

Returns a new instance of DynamicVariable.



4
5
6
7
8
9
10
11
# File 'lib/dynamic_variable.rb', line 4

def initialize(*pairs, &block)
  @bindings = []
  if block_given?
    with(*pairs, &block)
  else
    push_pairs(pairs) unless pairs.empty?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(variable, *args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dynamic_variable.rb', line 66

def method_missing(variable, *args)    
  if args.empty?
    value = self[variable]
    instance_eval %Q{def #{variable}; self[:#{variable}] end}
  else
    writer = variable
    unless args.size == 1 and writer.to_s =~ /(.*)=/
      raise NoMethodError, "undefined method `#{writer}' for #{self.inspect}"
    end
    variable = $1.to_sym
    self[variable] = args[0]
    instance_eval %Q{def #{writer}(v); self[:#{variable}] = v end}
  end
  value
end

Instance Method Details

#[](variable) ⇒ Object



58
59
60
# File 'lib/dynamic_variable.rb', line 58

def [](variable)
  find_binding(variable)[1]
end

#[]=(variable, value) ⇒ Object



62
63
64
# File 'lib/dynamic_variable.rb', line 62

def []=(variable, value)
  find_binding(variable)[1] = value
end

#bindings(variable = @@un) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/dynamic_variable.rb', line 92

def bindings(variable = @@un)
  if variable == @@un
    @bindings.map{|pair| pair.clone}
  else
    @bindings.select{|var, value| var == variable}.map!{|var, value| value}
  end
end

#bindings=(bindings) ⇒ Object



100
101
102
# File 'lib/dynamic_variable.rb', line 100

def bindings=(bindings)
  set_bindings(bindings)
end

#pop(variable) ⇒ Object



19
20
21
22
# File 'lib/dynamic_variable.rb', line 19

def pop(variable)
  index = rindex(variable)
  @bindings.slice!(index)[1] if index
end

#push(variable, value) ⇒ Object



13
14
15
16
17
# File 'lib/dynamic_variable.rb', line 13

def push(variable, value)
  pair = [variable, value]
  @bindings << pair
  pair
end

#set_bindings(variable, bindings = @@un) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dynamic_variable.rb', line 104

def set_bindings(variable, bindings = @@un)
  if bindings == @@un
    bindings = variable
    @bindings = bindings.map do |pair|
      unless pair.is_a? Array and pair.size == 2
        raise ArgumentError,
          "expected [variable, value] pair, got #{pair.inspect}"
      end
      pair.clone
    end
  else
    unless bindings.is_a? Array
      raise ArgumentError,
        "expected bindings to be Array, got a #{bindings.class}"
    end
    index = 0
    old_bindings = @bindings
    @bindings = []
    old_bindings.each do |var, value|
      if var == variable
        unless index < bindings.size
          next
        end
        value = bindings[index]
        index += 1
      end
      push(var, value)
    end
    while index < bindings.size
      value = bindings[index]
      push(variable, value)
      index += 1
    end
  end
  bindings
end

#variablesObject



82
83
84
85
86
# File 'lib/dynamic_variable.rb', line 82

def variables
  variables = {}
  @bindings.each{|variable, value| variables[variable] = value}
  variables
end

#variables=(variables) ⇒ Object



88
89
90
# File 'lib/dynamic_variable.rb', line 88

def variables=(variables)
  variables.each{|variable, value| self[variable] = value}
end

#with(*pairs) ⇒ Object

with {}

varible defaults to :value
value defaults to nil

with(value) { block }

variable defaults to :value

with(variable, value) { block }

with(variable_1, value_1, …, variable_n_1, value_n_1, value_n) { block }

variable_n defaults to :value

with(variable_1, value_1, …, variable_n, value_n) { block }



39
40
41
42
43
44
45
46
47
# File 'lib/dynamic_variable.rb', line 39

def with(*pairs)
  pairs = [nil] if pairs.empty?
  push_pairs(pairs)
  begin
    yield(self)
  ensure
    pop_pairs(pairs)
  end
end

#with_moduleObject



49
50
51
52
53
54
55
56
# File 'lib/dynamic_variable.rb', line 49

def with_module
  this = self
  mod = Module.new
  mod.send(:define_method, :with) do |*args, &block|
    this.with(*args, &block)
  end
  mod
end