Class: Creamerscript::Sweeteners::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/creamerscript/sweeteners/base.rb

Constant Summary collapse

SYMBOL =
/[A-Za-z0-9$_@]+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



8
9
10
# File 'lib/creamerscript/sweeteners/base.rb', line 8

def initialize
  @substitutions = {}
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/creamerscript/sweeteners/base.rb', line 6

def source
  @source
end

#substitutionsObject

Returns the value of attribute substitutions.



6
7
8
# File 'lib/creamerscript/sweeteners/base.rb', line 6

def substitutions
  @substitutions
end

Instance Method Details

#call(id) ⇒ Object



12
13
14
# File 'lib/creamerscript/sweeteners/base.rb', line 12

def call(id)
  @source = substitutions[id]
end

#substitute(source) ⇒ Object



20
21
22
# File 'lib/creamerscript/sweeteners/base.rb', line 20

def substitute(source)
  source.gsub!(pattern) { |match| tokenize(match) }
end

#to_coffeeObject



16
17
18
# File 'lib/creamerscript/sweeteners/base.rb', line 16

def to_coffee
  source
end

#tokenObject



28
29
30
# File 'lib/creamerscript/sweeteners/base.rb', line 28

def token
  "_____CREAMER_#{type.upcase}_#{substitutions.size}_____"
end

#tokenize(substitution) ⇒ Object



24
25
26
# File 'lib/creamerscript/sweeteners/base.rb', line 24

def tokenize(substitution)
  token.tap { substitutions[substitutions.size] = substitution }
end

#typeObject



32
33
34
# File 'lib/creamerscript/sweeteners/base.rb', line 32

def type
  self.class.name.split("::").last.underscore.to_sym
end