Class: Variables::Variable

Inherits:
Object
  • Object
show all
Extended by:
AbstractClass
Includes:
Interface
Defined in:
lib/variables/variable.rb

Direct Known Subclasses

ClassVariable, InstanceVariable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interface

#method_prefix, #variable_prefix

Constructor Details

#initialize(owner, name) ⇒ Variable

Returns a new instance of Variable.



12
13
14
15
# File 'lib/variables/variable.rb', line 12

def initialize(owner, name)
  @owner = owner
  @name = normalize(name)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/variables/variable.rb', line 10

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



10
11
12
# File 'lib/variables/variable.rb', line 10

def owner
  @owner
end

Instance Method Details

#defined?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/variables/variable.rb', line 17

def defined?
  variable(:defined?, name)
end

#fetch(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/variables/variable.rb', line 21

def fetch(*args)
  # We have to explicitly reference `self` here
  # since `defined?` is a `Kernel` method
  if self.defined?
    get
  elsif args.any?
    args.first
  elsif block_given?
    yield(name)
  else
    raise UndefinedVariable, "undefined variable #{name.inspect}"
  end
end

#getObject



35
36
37
# File 'lib/variables/variable.rb', line 35

def get
  variable(:get, name)
end

#replace(value, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/variables/variable.rb', line 39

def replace(value, &block)
  if block_given?
    replace_with_block(value, &block)
  else
    replace_without_block(value)
  end
end

#set(value) ⇒ Object



47
48
49
# File 'lib/variables/variable.rb', line 47

def set(value)
  variable(:set, name, value)
end