Class: Teapot::Substitutions

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

Defined Under Namespace

Classes: NestedSubstitution, SymbolicSubstitution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ordered = []) ⇒ Substitutions

Returns a new instance of Substitutions.



55
56
57
# File 'lib/teapot/substitutions.rb', line 55

def initialize(ordered = [])
	@ordered = ordered
end

Instance Attribute Details

#orderedObject (readonly)

Returns the value of attribute ordered.



76
77
78
# File 'lib/teapot/substitutions.rb', line 76

def ordered
  @ordered
end

Class Method Details

.for_context(context) ⇒ Object

Create a set of substitutions from the given context which includes a set of useful defaults.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/teapot/substitutions.rb', line 207

def self.for_context(context)
	substitutions = self.new

	# The user's current name:
	substitutions['AUTHOR_NAME'] = `git config --global user.name`.chomp

	substitutions['PROJECT_NAME'] = context.project.name
	substitutions['LICENSE'] = context.project.license

	current_date = Time.new
	substitutions['DATE'] = current_date.strftime("%-d/%-m/%Y")
	substitutions['YEAR'] = current_date.strftime("%Y")

	return substitutions
end

Instance Method Details

#+(other) ⇒ Object



72
73
74
# File 'lib/teapot/substitutions.rb', line 72

def + other
	Substitutions.new(@ordered + other.ordered)
end

#<<(substitution) ⇒ Object



68
69
70
# File 'lib/teapot/substitutions.rb', line 68

def << substitution
	@ordered << substition
end

#[]=(keyword, value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/teapot/substitutions.rb', line 59

def []= keyword, value
	if Array === value
		open, close = *value.each_slice(value.length / 2)
		@ordered << NestedSubstitution.new(keyword, open, close)
	else
		@ordered << SymbolicSubstitution.new('$' + keyword, value.to_s)
	end
end

#apply(text) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/teapot/substitutions.rb', line 82

def apply(text)
	return text unless @ordered.count > 0
	
	grouped = [[@ordered.first]]
	
	@ordered.drop(1).each do |substitution|
		if grouped.last[0].class == substitution.class
			grouped.last << substitution
		else
			grouped << [substitution]
		end
	end
	
	grouped.each do |group|
		text = group.first.class.apply(text, group)
	end
	
	return text
end

#call(text) ⇒ Object



78
79
80
# File 'lib/teapot/substitutions.rb', line 78

def call(text)
	apply(text)
end