Class: Luoma::LambdaExpr
- Inherits:
-
Object
- Object
- Luoma::LambdaExpr
- Defined in:
- lib/luoma/expression.rb,
sig/luoma/expression.rbs
Instance Attribute Summary collapse
-
#params ⇒ Array[String]
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#broadcast_with_index(enum) ⇒ Array[untyped]
(Enumerable) -> Array.
-
#call(args) ⇒ Object
(Array) -> untyped.
-
#call_with_index(value, index) ⇒ Object
(untyped, Integer) -> untyped.
-
#initialize(params, expr, context) ⇒ LambdaExpr
constructor
(Array, Expression, RenderContext) -> void.
- #to_s ⇒ Object
Constructor Details
#initialize(params, expr, context) ⇒ LambdaExpr
(Array, Expression, RenderContext) -> void
1163 1164 1165 1166 1167 |
# File 'lib/luoma/expression.rb', line 1163 def initialize(params, expr, context) @params = params @expr = expr @context = context end |
Instance Attribute Details
#params ⇒ Array[String] (readonly)
Returns the value of attribute params.
1160 1161 1162 |
# File 'lib/luoma/expression.rb', line 1160 def params @params end |
Instance Method Details
#broadcast_with_index(enum) ⇒ Array[untyped]
(Enumerable) -> Array
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 |
# File 'lib/luoma/expression.rb', line 1182 def broadcast_with_index(enum) scope = {} #: Hash[String, untyped] result = [] #: Array[untyped] if @params.length == 1 param = @params[0] @context.extends(scope) do enum.each do |item| scope[param] = item result << @expr.evaluate(@context) end end else name_param = @params[0] index_param = @params[1] @context.extends(scope) do enum.each_with_index do |item, i| scope[index_param] = i scope[name_param] = item result << @expr.evaluate(@context) end end end result end |
#call(args) ⇒ Object
(Array) -> untyped
1170 1171 1172 1173 1174 1175 |
# File 'lib/luoma/expression.rb', line 1170 def call(args) # zip pads with `nil` and ignores excess args. @context.extends(@params.zip(args).to_h) do @expr.evaluate(@context) end end |
#call_with_index(value, index) ⇒ Object
(untyped, Integer) -> untyped
1216 1217 1218 1219 1220 1221 1222 1223 |
# File 'lib/luoma/expression.rb', line 1216 def call_with_index(value, index) scope = { @params[0] => value } scope[@params[1]] = index if @params.length > 1 @context.extends(scope) do @expr.evaluate(@context) end end |
#to_s ⇒ Object
1226 1227 1228 |
# File 'lib/luoma/expression.rb', line 1226 def to_s "(#{@params.join(", ")}) => #{@expr}" end |