Class: PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler::LazyLiteralEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb

Overview

Lazily evaluates a Pops object, ignoring any objects that cannot be converted to a literal value. Based on the Puppet Literal Evaluator Ref - github.com/puppetlabs/puppet/blob/ba4d1a1aba0095d3c70b98fea5c67434a4876a61/lib/puppet/pops/evaluator/literal_evaluator.rb

Literal values for: String (not containing interpolation) Numbers Booleans Undef (produces nil) Array Hash QualifiedName Default (produced :default) Regular Expression (produces ruby regular expression) QualifiedReference e.g. File, FooBar AccessExpression

Anything else is ignored

Instance Method Summary collapse

Constructor Details

#initializeLazyLiteralEvaluator

Returns a new instance of LazyLiteralEvaluator.



138
139
140
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 138

def initialize
  @literal_visitor = ::Puppet::Pops::Visitor.new(self, "literal", 0, 0)
end

Instance Method Details

#literal(ast) ⇒ Object



142
143
144
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 142

def literal(ast)
  @literal_visitor.visit_this_0(self, ast)
end

#literal_AccessExpression(o) ⇒ Object



151
152
153
154
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 151

def literal_AccessExpression(o)
  # Extract the raw text of the Access Expression
  ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
end

#literal_ConcatenatedString(o) ⇒ Object



202
203
204
205
206
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 202

def literal_ConcatenatedString(o)
  # use double quoted string value if there is no interpolation
  throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)
  o.segments[0].value
end

#literal_Factory(o) ⇒ Object

—– The following methods are the same as the original Literal_evaluator



162
163
164
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 162

def literal_Factory(o)
  literal(o.model)
end

#literal_LiteralBoolean(o) ⇒ Object



186
187
188
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 186

def literal_LiteralBoolean(o)
  o.value
end

#literal_LiteralDefault(o) ⇒ Object



194
195
196
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 194

def literal_LiteralDefault(o)
  :default
end

#literal_LiteralHash(o) ⇒ Object



212
213
214
215
216
217
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 212

def literal_LiteralHash(o)
  o.entries.reduce({}) do |result, entry|
    result[literal(entry.key)] = literal(entry.value)
    result
  end
end

#literal_LiteralList(o) ⇒ Object



208
209
210
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 208

def literal_LiteralList(o)
  o.values.map { |v| literal(v) }
end

#literal_LiteralNumber(o) ⇒ Object



178
179
180
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 178

def literal_LiteralNumber(o)
  o.value
end

#literal_LiteralRegularExpression(o) ⇒ Object



198
199
200
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 198

def literal_LiteralRegularExpression(o)
  o.value
end

#literal_LiteralString(o) ⇒ Object



170
171
172
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 170

def literal_LiteralString(o)
  o.value
end

#literal_LiteralUndef(o) ⇒ Object



190
191
192
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 190

def literal_LiteralUndef(o)
  nil
end

#literal_Object(o) ⇒ Object

—– The following methods are different/additions from the original Literal_evaluator



147
148
149
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 147

def literal_Object(o)
  # Ignore any other object types
end

#literal_Program(o) ⇒ Object



166
167
168
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 166

def literal_Program(o)
  literal(o.body)
end

#literal_QualifiedName(o) ⇒ Object



174
175
176
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 174

def literal_QualifiedName(o)
  o.value
end

#literal_QualifiedReference(o) ⇒ Object



156
157
158
159
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 156

def literal_QualifiedReference(o)
  # Extract the raw text of the Qualified Reference
  ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
end

#literal_UnaryMinusExpression(o) ⇒ Object



182
183
184
# File 'lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb', line 182

def literal_UnaryMinusExpression(o)
  -1 * literal(o.expr)
end