Class: Luoma::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/luoma/environment.rb,
sig/luoma/environment.rbs

Constant Summary collapse

RE_INTEGER =

Returns:

  • (Regexp)
/\A-?\d+(?:[eE]\+?\d+)?\Z/
RE_DECIMAL =

Returns:

  • (Regexp)
/((?:-?\d+\.\d+(?:[eE][+-]?\d+)?)|(-?\d+[eE]-\d+))/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auto_trim: nil, globals: nil, lexer: UnifiedLexer, loader: nil, max_assign_score_cumulative: nil, max_assign_score: nil, max_context_depth: 30, max_render_score_cumulative: nil, max_render_score: nil, max_render_size: nil, parser: UnifiedParser, strict: false, suppress_blank_control_flow_blocks: true, undefined: UndefinedDrop) ⇒ Environment

Returns a new instance of Environment.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/luoma/environment.rb', line 17

def initialize(
  auto_trim: nil,
  globals: nil,
  lexer: UnifiedLexer,
  loader: nil,
  max_assign_score_cumulative: nil,
  max_assign_score: nil,
  max_context_depth: 30,
  max_render_score_cumulative: nil,
  max_render_score: nil,
  max_render_size: nil,
  parser: UnifiedParser,
  strict: false,
  suppress_blank_control_flow_blocks: true,
  undefined: UndefinedDrop
)
  @auto_trim = auto_trim
  @globals = globals
  @lexer = lexer
  @loader = loader || HashLoader.new({})
  @max_assign_score = max_assign_score
  @max_assign_score_cumulative = max_assign_score_cumulative
  @max_context_depth = max_context_depth
  @max_render_score = max_render_score
  @max_render_score_cumulative = max_render_score_cumulative
  @max_render_size = max_render_size
  @parser = parser
  @strict = strict
  @suppress_blank_control_flow_blocks = suppress_blank_control_flow_blocks
  @undefined = undefined

  @tags = {} #: Hash[String, _Tag]
  @filters = {} #: Hash[String, untyped]
  @predicates = {} #: Hash[String, ^(untyped) -> bool]
  setup_tags_filters_and_predicates

  # Render context registers that persist when copying an instance of
  # `RenderContext`.
  @persistent_registers = Set[:extends_stack] #: Set[Symbol]
end

Instance Attribute Details

#auto_trim'-', ...

Returns the value of attribute auto_trim.

Returns:

  • ('-', '~', nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def auto_trim
  @auto_trim
end

#filtersHash[String, untyped]

Returns the value of attribute filters.

Returns:

  • (Hash[String, untyped])


9
10
11
# File 'lib/luoma/environment.rb', line 9

def filters
  @filters
end

#globalst_namespace?

Returns the value of attribute globals.

Returns:

  • (t_namespace, nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def globals
  @globals
end

#lexer_Lexer

Returns the value of attribute lexer.

Returns:



9
10
11
# File 'lib/luoma/environment.rb', line 9

def lexer
  @lexer
end

#loaderTemplateLoader

Returns the value of attribute loader.

Returns:



9
10
11
# File 'lib/luoma/environment.rb', line 9

def loader
  @loader
end

#max_assign_scoreInteger?

Returns the value of attribute max_assign_score.

Returns:

  • (Integer, nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def max_assign_score
  @max_assign_score
end

#max_assign_score_cumulativeInteger?

Returns the value of attribute max_assign_score_cumulative.

Returns:

  • (Integer, nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def max_assign_score_cumulative
  @max_assign_score_cumulative
end

#max_context_depthInteger

Returns the value of attribute max_context_depth.

Returns:

  • (Integer)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def max_context_depth
  @max_context_depth
end

#max_render_scoreInteger?

Returns the value of attribute max_render_score.

Returns:

  • (Integer, nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def max_render_score
  @max_render_score
end

#max_render_score_cumulativeInteger?

Returns the value of attribute max_render_score_cumulative.

Returns:

  • (Integer, nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def max_render_score_cumulative
  @max_render_score_cumulative
end

#max_render_sizeInteger?

Returns the value of attribute max_render_size.

Returns:

  • (Integer, nil)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def max_render_size
  @max_render_size
end

#parser_Parser

Returns the value of attribute parser.

Returns:



9
10
11
# File 'lib/luoma/environment.rb', line 9

def parser
  @parser
end

#persistent_registersSet[Symbol] (readonly)

Returns the value of attribute persistent_registers.

Returns:

  • (Set[Symbol])


7
8
9
# File 'lib/luoma/environment.rb', line 7

def persistent_registers
  @persistent_registers
end

#predicatesHash[String, ^(untyped) -> bool]

Returns the value of attribute predicates.

Returns:

  • (Hash[String, ^(untyped) -> bool])


9
10
11
# File 'lib/luoma/environment.rb', line 9

def predicates
  @predicates
end

#strictBoolean

Returns the value of attribute strict.

Returns:

  • (Boolean)


9
10
11
# File 'lib/luoma/environment.rb', line 9

def strict
  @strict
end

#suppress_blank_control_flow_blocksObject

Returns the value of attribute suppress_blank_control_flow_blocks.



9
10
11
# File 'lib/luoma/environment.rb', line 9

def suppress_blank_control_flow_blocks
  @suppress_blank_control_flow_blocks
end

#tagsHash[String, _Tag]

Returns the value of attribute tags.

Returns:

  • (Hash[String, _Tag])


9
10
11
# File 'lib/luoma/environment.rb', line 9

def tags
  @tags
end

#undefinedsingleton(UndefinedDrop)

Returns the value of attribute undefined.

Returns:



9
10
11
# File 'lib/luoma/environment.rb', line 9

def undefined
  @undefined
end

Instance Method Details

#cmp(left, right, context, token) ⇒ -1, ...

(untyped, untyped, RenderContext, t_token) -> (-1 | 1 | 0 | nil)

Parameters:

  • left (Object)
  • right (Object)
  • context (RenderContext)
  • token (t_token)

Returns:

  • (-1, 1, 0, nil)


265
266
267
268
269
270
271
272
273
274
275
# File 'lib/luoma/environment.rb', line 265

def cmp(left, right, context, token)
  return -1 if lt?(left, right, context, token)
  return  1 if lt?(right, left, context, token)
  return  0 if eq?(left, right, context, token)

  if left.nil? || nothing?(left)
    1
  elsif right.nil? || nothing?(right)
    -1
  end
end

#contains?(left, right, context, token) ⇒ Boolean

(untyped, untyped, RenderContext, t_token) -> bool

Parameters:

  • left (Object)
  • right (Object)
  • context (RenderContext)
  • token (t_token)

Returns:

  • (Boolean)


232
233
234
235
236
237
238
239
240
241
242
# File 'lib/luoma/environment.rb', line 232

def contains?(left, right, context, token)
  return left.contains?(right, context) if left.is_a?(Drop)

  right = right.to_primitive(:data, context) if right.is_a?(Drop)

  if left && right && left.respond_to?(:include?)
    left.include?(left.is_a?(String) ? right.to_s : right)
  else
    false
  end
end

#delete_filter(name) ⇒ void

This method returns an undefined value.

Remove a filter from the filter register.

Signature:

  • (String) -> untyped

Parameters:

  • name (String)


105
106
107
# File 'lib/luoma/environment.rb', line 105

def delete_filter(name)
  @filters.delete(name)
end

#delete_predicate(name) ⇒ Object

Remove a predicate from the predicate register.

Signature:

  • (String) -> (^(untyped) -> bool | nil)

Parameters:

  • name (String)

Returns:

  • (Object)


133
134
135
# File 'lib/luoma/environment.rb', line 133

def delete_predicate(name)
  @predicates.delete(name)
end

#delete_tag(name) ⇒ _Tag?

Remove a tag from the tag register.

Signature:

  • (String) -> (_Tag | nil)

Parameters:

  • name (String)

Returns:



119
120
121
# File 'lib/luoma/environment.rb', line 119

def delete_tag(name)
  @tags.delete(name)
end

#eq?(left, right, context, token) ⇒ Boolean

(untyped, untyped, RenderContext, t_token) -> bool

Parameters:

  • left (Object)
  • right (Object)
  • context (RenderContext)
  • token (t_token)

Returns:

  • (Boolean)


245
246
247
248
249
250
251
# File 'lib/luoma/environment.rb', line 245

def eq?(left, right, context, token)
  # TODO: Don't accepts token
  return left.eq?(right, context) if left.is_a?(Drop)
  return right.eq?(left, context) if right.is_a?(Drop)

  left == right
end

#get_template(name, globals: nil, context: nil) ⇒ Template

(String, ?globals: t_namespace?, ?context: RenderContext?, **untyped) -> Template

Parameters:

  • name (String)
  • globals: (t_namespace, nil) (defaults to: nil)
  • context: (RenderContext, nil) (defaults to: nil)
  • kwargs (Object)

Returns:



83
84
85
# File 'lib/luoma/environment.rb', line 83

def get_template(name, globals: nil, context: nil, **)
  @loader.load(self, name, globals: globals, context: context, **)
end

#lt?(left, right, context, token) ⇒ Boolean?

(untyped, untyped, RenderContext, t_token) -> bool?

Parameters:

  • left (Object)
  • right (Object)
  • context (RenderContext)
  • token (t_token)

Returns:

  • (Boolean, nil)


254
255
256
257
258
259
260
261
262
# File 'lib/luoma/environment.rb', line 254

def lt?(left, right, context, token)
  # TODO: Don't accepts token
  return left.lt?(right, context) if left.is_a?(Drop)
  return right.gt?(left, context) if right.is_a?(Drop)

  left < right
rescue ArgumentError, NoMethodError, TypeError
  nil
end

#make_globals(namespace) ⇒ t_namespace?

(t_namespace?) -> t_namespace?

Parameters:

  • namespace (t_namespace, nil)

Returns:

  • (t_namespace, nil)


457
458
459
460
461
462
# File 'lib/luoma/environment.rb', line 457

def make_globals(namespace)
  namespace_ = {} #: t_namespace
  namespace_.merge!(@globals) if @globals # steep:ignore
  namespace_.merge!(namespace) if namespace
  namespace_
end

#nothing?(obj) ⇒ Boolean

(untyped) -> bool

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


278
279
280
# File 'lib/luoma/environment.rb', line 278

def nothing?(obj)
  obj == :nothing || obj.is_a?(UndefinedDrop)
end

#parse(source, globals: nil, name: nil, overlay: nil, up_to_date: nil) ⇒ Template

(String, ?globals: t_namespace?, ?name: String?, ?overlay: t_namespace?, ?up_to_date: Proc::_Callable?) -> Template

Parameters:

  • source (String)
  • globals: (t_namespace, nil) (defaults to: nil)
  • name: (String, nil) (defaults to: nil)
  • overlay: (t_namespace, nil) (defaults to: nil)
  • up_to_date: (Object, nil) (defaults to: nil)

Returns:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/luoma/environment.rb', line 63

def parse(source, globals: nil, name: nil, overlay: nil, up_to_date: nil)
  Template.new(
    self,
    source,
    @parser.parse(self, source, name || "", @lexer.tokenize(self, source)),
    globals: make_globals(globals),
    name: name,
    overlay: overlay,
    up_to_date: up_to_date
  )
end

#register_filter(name, callable) ⇒ void

This method returns an undefined value.

Add or replace a filter. The same callable can be registered multiple times with different names.

When evaluated as a filter, callable will be called with an instance of FilterContext as the first argument, and the result of the expression to the left of the pipe operator as the second argument.

Remaining positional and keyword arguments are passed through from the filter invocation.

Signature:

  • (String, untyped) -> void

Parameters:

  • name (String)
  • callable (Object)


98
99
100
# File 'lib/luoma/environment.rb', line 98

def register_filter(name, callable)
  @filters[name] = callable
end

#register_predicate(name, callable) ⇒ void

This method returns an undefined value.

Add or replace a predicate function.

Signature:

  • (String, ^(untyped) -> bool) -> void

Parameters:

  • name (String)
  • callable (Object)


126
127
128
# File 'lib/luoma/environment.rb', line 126

def register_predicate(name, callable)
  @predicates[name] = callable
end

#register_tag(name, tag) ⇒ void

This method returns an undefined value.

Add or replace a tag.

Signature:

  • (String, _Tag) -> void

Parameters:

  • name (String)
  • tag (_Tag)


112
113
114
# File 'lib/luoma/environment.rb', line 112

def register_tag(name, tag)
  @tags[name] = tag
end

#render(source, data = nil) ⇒ String

(String, ?t_namespace?) -> String

Parameters:

  • source (String)
  • data (t_namespace, nil) (defaults to: nil)

Returns:

  • (String)


78
79
80
# File 'lib/luoma/environment.rb', line 78

def render(source, data = nil)
  parse(source).render(data)
end

#serialize(obj, context) ⇒ String

(untyped, RenderContext) -> String

Parameters:

Returns:

  • (String)


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/luoma/environment.rb', line 289

def serialize(obj, context)
  case obj
  when Drop
    s = obj.render(context)
    s.nil? ? to_string(obj, context) : s
  when Array
    if obj.all?(String)
      to_string(obj, context)
    elsif obj.all?(BlockDrop)
      obj.map { |i| i.render(context) }.join("\n")
    elsif obj.all? { |i| i.is_a?(String) || i.is_a?(BlockDrop) }
      obj.map { |i| i.is_a?(String) ? i : i.render(context) }.join
    else # rubocop: disable Lint/DuplicateBranch
      to_string(obj, context)
    end
  else
    to_string(obj, context)
  end
end

#setup_tags_filters_and_predicatesvoid

This method returns an undefined value.

() -> void



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/luoma/environment.rb', line 138

def setup_tags_filters_and_predicates
  @tags["assign"] = AssignTag
  @tags["capture"] = CaptureTag
  @tags["case"] = CaseTag
  @tags["define"] = DefineTag
  @tags["for"] = ForTag
  @tags["break"] = BreakTag
  @tags["continue"] = ContinueTag
  @tags["if"] = IfTag
  @tags["import"] = ImportTag
  @tags["include"] = IncludeTag
  @tags["raw"] = RawTag
  @tags["render"] = RenderTag
  @tags["with"] = WithTag

  register_filter("abs", Luoma::Filters.method(:abs))
  register_filter("all", Luoma::Filters.method(:all))
  register_filter("any", Luoma::Filters.method(:any))
  register_filter("append", Luoma::Filters.method(:append))
  register_filter("at_least", Luoma::Filters.method(:at_least))
  register_filter("at_most", Luoma::Filters.method(:at_most))
  register_filter("capitalize", Luoma::Filters.method(:capitalize))
  register_filter("ceil", Luoma::Filters.method(:ceil))
  register_filter("compact", Luoma::Filters.method(:compact))
  register_filter("concat", Luoma::Filters.method(:concat))
  register_filter("date", Luoma::Filters.method(:date))
  register_filter("default", Luoma::Filters.method(:default))
  register_filter("divided_by", Luoma::Filters.method(:divided_by))
  register_filter("downcase", Luoma::Filters.method(:downcase))
  register_filter("escape_once", Luoma::Filters.method(:escape_once))
  register_filter("escape", Luoma::Filters.method(:escape))
  register_filter("find_index", Luoma::Filters.method(:find_index))
  register_filter("find", Luoma::Filters.method(:find))
  register_filter("first", Luoma::Filters.method(:first))
  register_filter("flatten", Luoma::Filters.method(:flatten))
  register_filter("flat_map", Luoma::Filters.method(:flat_map))
  register_filter("floor", Luoma::Filters.method(:floor))
  register_filter("join", Luoma::Filters.method(:join))
  register_filter("json", Luoma::Filters.method(:json))
  register_filter("last", Luoma::Filters.method(:last))
  register_filter("lstrip", Luoma::Filters.method(:lstrip))
  register_filter("map", Luoma::Filters.method(:map))
  register_filter("max", Luoma::Filters.method(:max))
  register_filter("min", Luoma::Filters.method(:min))
  register_filter("minus", Luoma::Filters.method(:minus))
  register_filter("modulo", Luoma::Filters.method(:modulo))
  register_filter("newline_to_br", Luoma::Filters.method(:newline_to_br))
  register_filter("plus", Luoma::Filters.method(:plus))
  register_filter("prepend", Luoma::Filters.method(:prepend_))
  register_filter("squish", Luoma::Filters.method(:squish))
  register_filter("reject", Luoma::Filters.method(:reject))
  register_filter("remove_first", Luoma::Filters.method(:remove_first))
  register_filter("remove_last", Luoma::Filters.method(:remove_last))
  register_filter("remove", Luoma::Filters.method(:remove))
  register_filter("replace_first", Luoma::Filters.method(:replace_first))
  register_filter("replace_last", Luoma::Filters.method(:replace_last))
  register_filter("replace", Luoma::Filters.method(:replace))
  register_filter("reverse", Luoma::Filters.method(:reverse))
  register_filter("round", Luoma::Filters.method(:round))
  register_filter("rstrip", Luoma::Filters.method(:rstrip))
  register_filter("size", Luoma::Filters.method(:size))
  register_filter("slice", Luoma::Filters.method(:slice))
  register_filter("sort", Luoma::Filters.method(:sort))
  register_filter("sort_natural", Luoma::Filters.method(:sort_natural))
  register_filter("sort_numeric", Luoma::Filters.method(:sort_numeric))
  register_filter("split", Luoma::Filters.method(:split))
  register_filter("strip_html", Luoma::Filters.method(:strip_html))
  register_filter("strip_newlines", Luoma::Filters.method(:strip_newlines))
  register_filter("strip", Luoma::Filters.method(:strip))
  register_filter("sum", Luoma::Filters.method(:sum))
  register_filter("take", Luoma::Filters.method(:take))
  register_filter("times", Luoma::Filters.method(:times))
  register_filter("truncate", Luoma::Filters.method(:truncate))
  register_filter("truncatewords", Luoma::Filters.method(:truncatewords))
  register_filter("uniq", Luoma::Filters.method(:uniq))
  register_filter("upcase", Luoma::Filters.method(:upcase))
  register_filter("url_decode", Luoma::Filters.method(:url_decode))
  register_filter("url_encode", Luoma::Filters.method(:url_encode))
  register_filter("where", Luoma::Filters.method(:where))
  register_filter("filter", Luoma::Filters.method(:where))
  register_filter("zip", Luoma::Filters.method(:zip))

  register_predicate("array", Luoma::Predicates.method(:array?))
  register_predicate("blank", Luoma::Predicates.method(:blank?))
  register_predicate("defined", Luoma::Predicates.method(:defined?))
  register_predicate("empty", Luoma::Predicates.method(:empty?))
  register_predicate("null", Luoma::Predicates.method(:null?))
  register_predicate("number", Luoma::Predicates.method(:number?))
  register_predicate("numeric", Luoma::Predicates.method(:numeric?))
  register_predicate("object", Luoma::Predicates.method(:object?))
  register_predicate("string", Luoma::Predicates.method(:string?))
end

#to_a(obj, context) ⇒ Array[untyped]

(untyped, RenderContext) -> Array

Parameters:

Returns:

  • (Array[untyped])


310
311
312
313
314
315
316
317
318
319
320
# File 'lib/luoma/environment.rb', line 310

def to_a(obj, context)
  if obj.is_a?(Array)
    obj
  elsif obj.is_a?(String)
    obj.each_char.to_a
  elsif obj.respond_to?(:to_a)
    obj.to_a
  else
    [obj]
  end
end

#to_data(obj, context) ⇒ Object

(untyped, RenderContext) -> untyped

Parameters:

Returns:

  • (Object)


465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/luoma/environment.rb', line 465

def to_data(obj, context)
  case obj
  when nil, true, false, Numeric, String
    obj
  when Array
    obj.map { |v| to_data(v, context) }
  when Hash
    obj.transform_values { |v| to_data(v, context) }
  when Drop
    obj.to_primitive(:data, context)
  when Symbol
    ""
  end
end

#to_date(obj, context) ⇒ Object

Cast obj to a date and time. Return nil if casting fails. NOTE: This was copied from Shopify/liquid.

Parameters:

Returns:

  • (Object)


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/luoma/environment.rb', line 414

def to_date(obj, context)
  return obj if obj.respond_to?(:strftime)

  if obj.is_a?(String)
    return nil if obj.empty?

    obj = obj.downcase
  end

  case obj
  when "now", "today"
    Time.now
  when /\A\d+\z/, Integer
    Time.at(obj.to_i)
  when String
    Time.parse(obj)
  end
rescue ::ArgumentError
  nil
end

#to_enumerable(obj, context) ⇒ Enumerable[untyped]

(untyped, RenderContext) -> Enumerable

Parameters:

Returns:

  • (Enumerable[untyped])


380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/luoma/environment.rb', line 380

def to_enumerable(obj, context)
  case obj
  when Drop
    obj.each
  when Enumerable
    obj.to_enum
  when String
    obj.each_char
  else
    obj.respond_to?(:each) ? obj.each : [obj]
  end
end

#to_h(obj, context) ⇒ Hash[untyped, untyped]

(untyped, RenderContext) -> Hash[untyped, untyped]

Parameters:

Returns:

  • (Hash[untyped, untyped])


323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/luoma/environment.rb', line 323

def to_h(obj, context)
  if obj.is_a?(Hash)
    obj
  elsif obj.is_a(Drop)
    obj = obj.to_primitive("object", context)
    nothing?(obj) ? {} : obj
  elsif obj.respond_to?(:to_h)
    obj.to_h
  else
    {}
  end
end

#to_i(obj, context, default: :nothing) ⇒ void

This method returns an undefined value.

[X] (untyped, RenderContext, ?default: X) -> (Integer | X)



340
341
342
343
344
345
346
347
348
349
# File 'lib/luoma/environment.rb', line 340

def to_i(obj, context, default: :nothing)
  return obj if obj.is_a?(Integer)
  return obj.to_i if obj.respond_to?(:to_i) && !obj.is_a?(String)

  begin
    Integer(obj.to_s)
  rescue ::ArgumentError
    default
  end
end

#to_numeric(obj, context, default: :nothing) ⇒ void

This method returns an undefined value.

[X] (untyped, RenderContext, ?default: X) -> (Numeric | X)



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/luoma/environment.rb', line 352

def to_numeric(obj, context, default: :nothing)
  case obj
  when ::Float
    BigDecimal(obj)
  when Numeric
    obj
  when ::String
    case obj
    when RE_INTEGER
      obj.to_f.to_i
    when RE_DECIMAL
      BigDecimal(obj)
    else
      default
    end
  when true
    1
  when false
    0
  when Drop
    n = obj.to_primitive(:numeric, context)
    n == :nothing ? default : n
  else
    default
  end
end

#to_string(obj, context) ⇒ String

(untyped, RenderContext) -> String

Parameters:

Returns:

  • (String)


394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/luoma/environment.rb', line 394

def to_string(obj, context)
  case obj
  when String
    obj
  when Hash, Array
    JSON.generate(to_data(obj, context))
  when BigDecimal
    # obj.to_s("F") gives higher precision
    obj.to_f.to_s
  when Drop
    obj.to_primitive(:string, context)
  when Symbol
    ""
  else
    obj.to_s
  end
end

#trim(value, left, right) ⇒ String

(String, String?, String?) -> String

Parameters:

  • value (String)
  • left (String, nil)
  • right (String, nil)

Returns:

  • (String)


436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/luoma/environment.rb', line 436

def trim(value, left, right)
  case left || @auto_trim
  when "-"
    value.lstrip!
  when "~"
    value.sub!(/\A[\r\n]+/, "")
  end

  case right
  when "-"
    value.rstrip!
  when "~"
    value.sub!(/[\r\n]+\Z/, "")
  end

  value
end

#truthy?(obj, context) ⇒ Boolean

(untyped, RenderContext) -> bool

Parameters:

Returns:

  • (Boolean)


283
284
285
286
# File 'lib/luoma/environment.rb', line 283

def truthy?(obj, context)
  obj = obj.to_primitive(:boolean, context) if obj.is_a?(Drop)
  !nothing?(obj) && !!obj
end