Class: Luoma::Environment
- Inherits:
-
Object
- Object
- Luoma::Environment
- Defined in:
- lib/luoma/environment.rb,
sig/luoma/environment.rbs
Overview
Luoma template parsing and render configuration.
Constant Summary collapse
- RE_INTEGER =
/\A-?\d+(?:[eE]\+?\d+)?\Z/- RE_DECIMAL =
/((?:-?\d+\.\d+(?:[eE][+-]?\d+)?)|(-?\d+[eE]-\d+))/
Instance Attribute Summary collapse
-
#auto_trim ⇒ '-', ...
Returns the value of attribute auto_trim.
-
#filters ⇒ Hash[String, untyped]
Returns the value of attribute filters.
-
#globals ⇒ t_namespace?
Returns the value of attribute globals.
-
#lexer ⇒ _Lexer
Returns the value of attribute lexer.
-
#loader ⇒ TemplateLoader
Returns the value of attribute loader.
-
#max_assign_score ⇒ Integer?
Returns the value of attribute max_assign_score.
-
#max_assign_score_cumulative ⇒ Integer?
Returns the value of attribute max_assign_score_cumulative.
-
#max_context_depth ⇒ Integer
Returns the value of attribute max_context_depth.
-
#max_render_score ⇒ Integer?
Returns the value of attribute max_render_score.
-
#max_render_score_cumulative ⇒ Integer?
Returns the value of attribute max_render_score_cumulative.
-
#max_render_size ⇒ Integer?
Returns the value of attribute max_render_size.
-
#parser ⇒ _Parser
Returns the value of attribute parser.
-
#persistent_registers ⇒ Set[Symbol]
readonly
Returns the value of attribute persistent_registers.
-
#predicates ⇒ Hash[String, ^(untyped) -> bool]
Returns the value of attribute predicates.
-
#strict ⇒ Boolean
Returns the value of attribute strict.
-
#suppress_blank_control_flow_blocks ⇒ Object
Returns the value of attribute suppress_blank_control_flow_blocks.
-
#tags ⇒ Hash[String, _Tag]
Returns the value of attribute tags.
-
#undefined ⇒ singleton(UndefinedDrop)
Returns the value of attribute undefined.
Instance Method Summary collapse
-
#cmp(left, right, context) ⇒ -1, ...
(untyped, untyped, RenderContext) -> (-1 | 1 | 0 | nil).
-
#contains?(left, right, context) ⇒ Boolean
(untyped, untyped, RenderContext) -> bool.
-
#delete_filter(name) ⇒ void
Remove a filter from the filter register.
-
#delete_predicate(name) ⇒ Object
Remove a predicate from the predicate register.
-
#delete_tag(name) ⇒ _Tag?
Remove a tag from the tag register.
-
#eq?(left, right, context) ⇒ Boolean
(untyped, untyped, RenderContext) -> bool.
-
#get_template(name, globals: nil, context: nil) ⇒ Template
(String, ?globals: t_namespace?, ?context: RenderContext?, **untyped) -> Template.
-
#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
constructor
A new instance of Environment.
-
#lt?(left, right, context) ⇒ Boolean?
(untyped, untyped, RenderContext) -> bool?.
-
#make_globals(namespace) ⇒ t_namespace?
(t_namespace?) -> t_namespace?.
-
#nothing?(obj) ⇒ Boolean
(untyped) -> bool.
-
#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.
-
#register_filter(name, callable) ⇒ void
Add or replace a filter.
-
#register_predicate(name, callable) ⇒ void
Add or replace a predicate function.
-
#register_tag(name, tag) ⇒ void
Add or replace a tag.
-
#render(source, data = nil) ⇒ String
(String, ?t_namespace?) -> String.
-
#serialize(obj, context) ⇒ String
(untyped, RenderContext) -> String.
-
#setup_tags_filters_and_predicates ⇒ void
() -> void.
-
#to_a(obj, context) ⇒ Array[untyped]
(untyped, RenderContext) -> Array.
-
#to_data(obj, context) ⇒ Object
(untyped, RenderContext) -> untyped.
-
#to_date(obj, context) ⇒ Object
Cast obj to a date and time.
-
#to_enumerable(obj, context) ⇒ Enumerable[untyped]
(untyped, RenderContext) -> Enumerable.
-
#to_h(obj, context) ⇒ Hash[untyped, untyped]
(untyped, RenderContext) -> Hash[untyped, untyped].
-
#to_i(obj, context, default: :nothing) ⇒ void
[X] (untyped, RenderContext, ?default: X) -> (Integer | X).
-
#to_numeric(obj, context, default: :nothing) ⇒ void
[X] (untyped, RenderContext, ?default: X) -> (Numeric | X).
-
#to_string(obj, context) ⇒ String
(untyped, RenderContext) -> String.
-
#trim(value, left, right) ⇒ String
(String, String?, String?) -> String.
-
#truthy?(obj, context) ⇒ Boolean
(untyped, RenderContext) -> bool.
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.
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 57 |
# File 'lib/luoma/environment.rb', line 18 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] # 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.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def auto_trim @auto_trim end |
#filters ⇒ Hash[String, untyped]
Returns the value of attribute filters.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def filters @filters end |
#globals ⇒ t_namespace?
Returns the value of attribute globals.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def globals @globals end |
#lexer ⇒ _Lexer
Returns the value of attribute lexer.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def lexer @lexer end |
#loader ⇒ TemplateLoader
Returns the value of attribute loader.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def loader @loader end |
#max_assign_score ⇒ Integer?
Returns the value of attribute max_assign_score.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def max_assign_score @max_assign_score end |
#max_assign_score_cumulative ⇒ Integer?
Returns the value of attribute max_assign_score_cumulative.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def max_assign_score_cumulative @max_assign_score_cumulative end |
#max_context_depth ⇒ Integer
Returns the value of attribute max_context_depth.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def max_context_depth @max_context_depth end |
#max_render_score ⇒ Integer?
Returns the value of attribute max_render_score.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def max_render_score @max_render_score end |
#max_render_score_cumulative ⇒ Integer?
Returns the value of attribute max_render_score_cumulative.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def max_render_score_cumulative @max_render_score_cumulative end |
#max_render_size ⇒ Integer?
Returns the value of attribute max_render_size.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def max_render_size @max_render_size end |
#parser ⇒ _Parser
Returns the value of attribute parser.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def parser @parser end |
#persistent_registers ⇒ Set[Symbol] (readonly)
Returns the value of attribute persistent_registers.
8 9 10 |
# File 'lib/luoma/environment.rb', line 8 def persistent_registers @persistent_registers end |
#predicates ⇒ Hash[String, ^(untyped) -> bool]
Returns the value of attribute predicates.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def predicates @predicates end |
#strict ⇒ Boolean
Returns the value of attribute strict.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def strict @strict end |
#suppress_blank_control_flow_blocks ⇒ Object
Returns the value of attribute suppress_blank_control_flow_blocks.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def suppress_blank_control_flow_blocks @suppress_blank_control_flow_blocks end |
#tags ⇒ Hash[String, _Tag]
Returns the value of attribute tags.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def @tags end |
#undefined ⇒ singleton(UndefinedDrop)
Returns the value of attribute undefined.
10 11 12 |
# File 'lib/luoma/environment.rb', line 10 def undefined @undefined end |
Instance Method Details
#cmp(left, right, context) ⇒ -1, ...
(untyped, untyped, RenderContext) -> (-1 | 1 | 0 | nil)
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/luoma/environment.rb', line 264 def cmp(left, right, context) return -1 if lt?(left, right, context) return 1 if lt?(right, left, context) return 0 if eq?(left, right, context) if left.nil? || nothing?(left) 1 elsif right.nil? || nothing?(right) -1 end end |
#contains?(left, right, context) ⇒ Boolean
(untyped, untyped, RenderContext) -> bool
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/luoma/environment.rb', line 233 def contains?(left, right, context) 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.
106 107 108 |
# File 'lib/luoma/environment.rb', line 106 def delete_filter(name) @filters.delete(name) end |
#delete_predicate(name) ⇒ Object
Remove a predicate from the predicate register.
134 135 136 |
# File 'lib/luoma/environment.rb', line 134 def delete_predicate(name) @predicates.delete(name) end |
#delete_tag(name) ⇒ _Tag?
Remove a tag from the tag register.
120 121 122 |
# File 'lib/luoma/environment.rb', line 120 def delete_tag(name) @tags.delete(name) end |
#eq?(left, right, context) ⇒ Boolean
(untyped, untyped, RenderContext) -> bool
246 247 248 249 250 251 |
# File 'lib/luoma/environment.rb', line 246 def eq?(left, right, context) 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
84 85 86 |
# File 'lib/luoma/environment.rb', line 84 def get_template(name, globals: nil, context: nil, **) @loader.load(self, name, globals: globals, context: context, **) end |
#lt?(left, right, context) ⇒ Boolean?
(untyped, untyped, RenderContext) -> bool?
254 255 256 257 258 259 260 261 |
# File 'lib/luoma/environment.rb', line 254 def lt?(left, right, context) 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?
456 457 458 459 460 461 |
# File 'lib/luoma/environment.rb', line 456 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
277 278 279 |
# File 'lib/luoma/environment.rb', line 277 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
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/luoma/environment.rb', line 64 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: , 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.
99 100 101 |
# File 'lib/luoma/environment.rb', line 99 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.
127 128 129 |
# File 'lib/luoma/environment.rb', line 127 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.
113 114 115 |
# File 'lib/luoma/environment.rb', line 113 def register_tag(name, tag) @tags[name] = tag end |
#render(source, data = nil) ⇒ String
(String, ?t_namespace?) -> String
79 80 81 |
# File 'lib/luoma/environment.rb', line 79 def render(source, data = nil) parse(source).render(data) end |
#serialize(obj, context) ⇒ String
(untyped, RenderContext) -> String
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/luoma/environment.rb', line 288 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_predicates ⇒ void
This method returns an undefined value.
() -> void
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 230 |
# File 'lib/luoma/environment.rb', line 139 def @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_js", Luoma::Filters.method(:escape_js)) 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_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
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/luoma/environment.rb', line 309 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
464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/luoma/environment.rb', line 464 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.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/luoma/environment.rb', line 413 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
379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/luoma/environment.rb', line 379 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]
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/luoma/environment.rb', line 322 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)
339 340 341 342 343 344 345 346 347 348 |
# File 'lib/luoma/environment.rb', line 339 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)
351 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 |
# File 'lib/luoma/environment.rb', line 351 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
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/luoma/environment.rb', line 393 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
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/luoma/environment.rb', line 435 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
282 283 284 285 |
# File 'lib/luoma/environment.rb', line 282 def truthy?(obj, context) obj = obj.to_primitive(:boolean, context) if obj.is_a?(Drop) !nothing?(obj) && !!obj end |