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



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

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

Instance Attribute Details

#orderedObject (readonly)

Returns the value of attribute ordered.



94
95
96
# File 'lib/teapot/substitutions.rb', line 94

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.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/teapot/substitutions.rb', line 241

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

  # The user's current name:
  substitutions['AUTHOR_NAME'] = context..user.name
  substitutions['AUTHOR_EMAIL'] = context..user.email

  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



90
91
92
# File 'lib/teapot/substitutions.rb', line 90

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

#<<(substitution) ⇒ Object



86
87
88
# File 'lib/teapot/substitutions.rb', line 86

def << substitution
  @ordered << substition
end

#[]=(keyword, value) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/teapot/substitutions.rb', line 77

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/teapot/substitutions.rb', line 100

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



96
97
98
# File 'lib/teapot/substitutions.rb', line 96

def call(text)
  apply(text)
end

#freezeObject



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

def freeze
  @ordered.freeze
  
  super
end