Class: Alf::Types::TupleExpression

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_lambdaProc (readonly)


15
16
17
# File 'lib/alf/types/tuple_expression.rb', line 15

def expr_lambda
  @expr_lambda
end

#result_typeClass (readonly)


21
22
23
# File 'lib/alf/types/tuple_expression.rb', line 21

def result_type
  @result_type
end

#sourceString (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)

Raises:

  • (ArgumentError)

    if the coercion fails


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

#hashInteger

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_typeClass

Infers the resulting type.


78
79
80
# File 'lib/alf/types/tuple_expression.rb', line 78

def infer_type
  result_type || Object
end

#to_lispyString

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_literalString

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