Class: Ralf::Interpolation

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

Defined Under Namespace

Classes: NotAllInterpolationsSatisfied, VariableMissing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, variables) ⇒ Interpolation

Returns a new instance of Interpolation.



18
19
20
21
22
23
24
# File 'lib/ralf/interpolation.rb', line 18

def initialize(string, variables)
  @variables = variables
  @result = string.dup
  (Ralf::Interpolation.public_instance_methods(false) - ['result']).each do |tag|
    @result.gsub!(/:#{tag}/, self.send( tag )) unless self.send(tag).nil?
  end
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



16
17
18
# File 'lib/ralf/interpolation.rb', line 16

def result
  @result
end

Class Method Details

.interpolate(string, variables, required_variables = []) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/ralf/interpolation.rb', line 7

def self.interpolate(string, variables, required_variables = [])
  required_variables.each do |name|
    raise VariableMissing, ":#{name.to_s} variable missing" unless string.match(/:#{name.to_s}/)
  end
  processor = Ralf::Interpolation.new(string, variables)
  raise NotAllInterpolationsSatisfied, "Not all keys are interpolated: '#{string}'" if processor.result.match(/:/)
  processor.result
end

Instance Method Details

#bucketObject



26
27
28
# File 'lib/ralf/interpolation.rb', line 26

def bucket
  @variables[:bucket]
end

#dayObject



34
35
36
# File 'lib/ralf/interpolation.rb', line 34

def day
  "%02d" % @variables[:date].day if @variables[:date]
end

#monthObject



38
39
40
# File 'lib/ralf/interpolation.rb', line 38

def month
  "%02d" % @variables[:date].month if @variables[:date]
end

#weekObject



30
31
32
# File 'lib/ralf/interpolation.rb', line 30

def week
  "%02d" % @variables[:date].cweek if @variables[:date]
end

#yearObject



42
43
44
# File 'lib/ralf/interpolation.rb', line 42

def year
  "%04d" % @variables[:date].year if @variables[:date]
end