Class: Alf::Types::TupleExpression
- Inherits:
-
Object
- Object
- Alf::Types::TupleExpression
- Defined in:
- lib/alf/types/tuple_expression.rb
Overview
A tuple expression is a Ruby expression whose evaluates in the scope of a specific tuple.
Example:
expr = TupleExpression["status * 10"]
expr.call(:status => 20)
# => 200
Instance Attribute Summary collapse
-
#expr_lambda ⇒ Proc
readonly
The lambda expression.
-
#result_type ⇒ Class
readonly
The resulting type (Object by default).
-
#source ⇒ String
readonly
The expression source code (may be nil).
Class Method Summary collapse
-
.coerce(arg) ⇒ TupleExpression
(also: [])
Coerces `arg` to a tuple expression.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks equality with another expression.
-
#evaluate(scope = nil) ⇒ Object
Evaluates the expression in the context of a TupleScope.
-
#has_source_code! ⇒ String
Asserts that this expression knows its source code or raises a NotImplementedError.
-
#hash ⇒ Integer
Returns a hash code.
-
#infer_type ⇒ Class
Infers the resulting type.
-
#initialize(expr, source = nil, result_type = Object) ⇒ TupleExpression
constructor
Creates a tuple expression from a Proc object.
-
#to_lispy ⇒ String
Returns a lispy expression.
-
#to_ruby_literal ⇒ String
Returns a ruby literal for this expression.
Constructor Details
#initialize(expr, source = nil, result_type = Object) ⇒ TupleExpression
Creates a tuple expression from a Proc object
27 28 29 30 31 |
# File 'lib/alf/types/tuple_expression.rb', line 27 def initialize(expr, source = nil, result_type = Object) @expr_lambda = expr @source = source @result_type = result_type end |
Instance Attribute Details
#expr_lambda ⇒ Proc (readonly)
15 16 17 |
# File 'lib/alf/types/tuple_expression.rb', line 15 def expr_lambda @expr_lambda end |
#result_type ⇒ Class (readonly)
21 22 23 |
# File 'lib/alf/types/tuple_expression.rb', line 21 def result_type @result_type end |
#source ⇒ String (readonly)
18 19 20 |
# File 'lib/alf/types/tuple_expression.rb', line 18 def source @source end |
Class Method Details
.coerce(arg) ⇒ TupleExpression Also known as: []
Coerces `arg` to a tuple expression.
Implemented coercions are:
-
TupleExpression -> self
-
Proc -> TupleExpression.new(arg, nil)
-
Symbol -> TupleExpression.new(…, arg)
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/alf/types/tuple_expression.rb', line 45 def coerce(arg) case arg when TupleExpression arg when Proc TupleExpression.new(arg) when AttrName TupleExpression.new(eval("->(t){ t.#{arg} }"), "t.#{arg}") else raise ArgumentError, "Invalid argument `#{arg}` for TupleExpression()" end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Checks equality with another expression
93 94 95 96 97 98 99 100 |
# File 'lib/alf/types/tuple_expression.rb', line 93 def ==(other) return false unless other.is_a?(TupleExpression) if source.nil? || other.source.nil? expr_lambda == other.expr_lambda else source == other.source end end |
#evaluate(scope = nil) ⇒ Object
Evaluates the expression in the context of a TupleScope
66 67 68 69 70 71 72 73 |
# File 'lib/alf/types/tuple_expression.rb', line 66 def evaluate(scope = nil) result = if @expr_lambda.arity == 1 @expr_lambda.call(scope) else scope.instance_exec(&@expr_lambda) end looks_an_expression?(result) ? result.to_relation : result end |
#has_source_code! ⇒ String
Asserts that this expression knows its source code or raises a NotImplementedError.
123 124 125 126 127 128 129 |
# File 'lib/alf/types/tuple_expression.rb', line 123 def has_source_code! if source.nil? raise NotImplementedError, "No known source code for this expression" else source end end |
#hash ⇒ Integer
Returns a hash code.
85 86 87 |
# File 'lib/alf/types/tuple_expression.rb', line 85 def hash @source.nil? ? @expr_lambda.hash : @source.hash end |
#infer_type ⇒ Class
Infers the resulting type.
78 79 80 |
# File 'lib/alf/types/tuple_expression.rb', line 78 def infer_type result_type || Object end |
#to_lispy ⇒ String
Returns a lispy expression.
106 107 108 109 110 |
# File 'lib/alf/types/tuple_expression.rb', line 106 def to_lispy "->(t){ #{has_source_code!} }" rescue NotImplementedError => ex "->(t){ [code unavailable] }" end |
#to_ruby_literal ⇒ String
Returns a ruby literal for this expression.
115 116 117 |
# File 'lib/alf/types/tuple_expression.rb', line 115 def to_ruby_literal "Alf::#{Support.class_name(self.class)}[#{Support.to_ruby_literal(has_source_code!)}]" end |