Class: DynamicVariable

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

Direct Known Subclasses

Mixin::MixedDynamicVariable

Defined Under Namespace

Modules: Mixin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*pairs, &block) ⇒ DynamicVariable

Returns a new instance of DynamicVariable.



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

def initialize(*pairs, &block)
  self.default_variable = :value
  @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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dynamic_variable.rb', line 58

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 Attribute Details

#default_variableObject

Returns the value of attribute default_variable.



2
3
4
# File 'lib/dynamic_variable.rb', line 2

def default_variable
  @default_variable
end

Instance Method Details

#[](variable) ⇒ Object



50
51
52
# File 'lib/dynamic_variable.rb', line 50

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

#[]=(variable, value) ⇒ Object



54
55
56
# File 'lib/dynamic_variable.rb', line 54

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

#bindings(variable = (un = true)) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/dynamic_variable.rb', line 85

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

#bindings=(bindings) ⇒ Object



93
94
95
# File 'lib/dynamic_variable.rb', line 93

def bindings=(bindings)
  set_bindings(bindings)
end

#pop(variable) ⇒ Object



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

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

#push(variable, value) ⇒ Object



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

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

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



97
98
99
100
101
102
103
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
# File 'lib/dynamic_variable.rb', line 97

def set_bindings(variable, bindings = (un = true))
  if 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



75
76
77
78
79
# File 'lib/dynamic_variable.rb', line 75

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

#variables=(variables) ⇒ Object



81
82
83
# File 'lib/dynamic_variable.rb', line 81

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

#with(*pairs) ⇒ Object

with {}

varible defaults to default_variable
value defaults to nil

with(value) { block }

variable defaults to default_variable

with(variable, value) { block }

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

variable_n defaults to default_variable

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



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

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