Class: Cascading::ExprStub

Inherits:
Object
  • Object
show all
Defined in:
lib/cascading/expr_stub.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ ExprStub

ExprStub requires a Janino expression decorated with field types. For example:

expr('"Found: " + (x:int + y:int) + " " + z:string')

Type names are defined in Cascading::JAVA_TYPE_MAP.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cascading/expr_stub.rb', line 9

def initialize(expression)
  @input_expression = expression
  @expression = expression.dup
  @types = {}

  # Simple regexp based parser for types
  JAVA_TYPE_MAP.each do |sym, klass|
    @expression.gsub!(/[A-Za-z0-9_]+:#{sym.to_s}/) do |match|
      name = match.split(/:/).first.gsub(/\s+/, "")
      @types[name] = klass
      match.gsub(/:#{sym.to_s}/, "")
    end
  end
end

Instance Attribute Details

#expressionObject

Returns the value of attribute expression.



3
4
5
# File 'lib/cascading/expr_stub.rb', line 3

def expression
  @expression
end

#input_expressionObject

Returns the value of attribute input_expression.



3
4
5
# File 'lib/cascading/expr_stub.rb', line 3

def input_expression
  @input_expression
end

#typesObject

Returns the value of attribute types.



3
4
5
# File 'lib/cascading/expr_stub.rb', line 3

def types
  @types
end

Class Method Details

.expr(expression, options = {}) ⇒ Object

Convenience constructor for an ExprStub that optionally performs validation. Takes a string to use as a Janino expression and an optional options hash.

The named options are:

validate

A boolean indicating whether expression validation using default actual argument values should be performed. Defaults to true.

validate_with

A hash mapping field names (or symbols) to the value that should be used for validation. Strings default to nil, so if you have previously filtered nulls you might use a marker value like ‘nulls_filtered’. Defaults to {}.

Example:

insert 'x_eq_y' => expr('x:string.equals(y:string)', :validate_with => { :x => 'nulls_filtered' })


51
52
53
54
55
56
57
# File 'lib/cascading/expr_stub.rb', line 51

def self.expr(expression, options = {})
  options = { :validate => true, :validate_with => {} }.merge(options)
  expr_stub = expression.kind_of?(ExprStub) ? expression : ExprStub.new(expression).compile
  expr_stub.validate(options[:validate_with]) if options[:validate]
  puts "Expression validation is disabled for '#{expression}'" unless options[:validate]
  expr_stub
end

Instance Method Details

#compileObject

Scan, parse, and compile expression, then return this ExprStub upon success. Throws an CascadingException upon failure.



61
62
63
64
# File 'lib/cascading/expr_stub.rb', line 61

def compile
  evaluator
  self
end

#eval(actual_args) ⇒ Object

Evaluates this ExprStub given a hash mapping argument names to argument values. Names may be strings or symbols. Throws an CascadingException upon failure.



69
70
71
72
73
74
75
76
77
78
# File 'lib/cascading/expr_stub.rb', line 69

def eval(actual_args)
  actual_args = actual_args.inject({}) do |string_keys, (arg, value)|
    string_keys[arg.to_s] = specific_to_java(value, @types[arg.to_s])
    string_keys
  end
  args, values = split_hash(actual_args)
  unused = validate_fields(args)
  return self.eval(actual_args.reject{ |arg, value| unused.include?(arg) }) unless unused.empty?
  evaluate(values)
end

#names_and_typesObject

Extract Java names and types from @types hash. Cascading constructors often require two separate Java Arrays in this fashion.



26
27
28
29
# File 'lib/cascading/expr_stub.rb', line 26

def names_and_types
  names, types = split_hash(@types)
  [names.to_java(java.lang.String), types.to_java(java.lang.Class)]
end

#to_sObject

Prints the original input expression.



32
33
34
# File 'lib/cascading/expr_stub.rb', line 32

def to_s
  @input_expression
end

#validate(actual_args = {}) ⇒ Object

Evaluates this ExprStub with default values for each actual argument. Values may be overridden with the optional actual_args argument, which accepts a hash like ExprStub#eval. Throws an CascadingException upon failure.



84
85
86
# File 'lib/cascading/expr_stub.rb', line 84

def validate(actual_args = {})
  self.eval(test_values.merge(actual_args))
end

#validate_fields(fields) ⇒ Object

Throws an exception if any arguments required by this ExprStub are missing from fields. Returns those fields which are unused. Throws an ExprArgException upon failure.

Raises:



98
99
100
101
102
103
# File 'lib/cascading/expr_stub.rb', line 98

def validate_fields(fields)
  names = @types.keys.sort
  missing = names - fields
  raise ExprArgException.new("Expression '#{@expression}' is missing these fields: #{missing.inspect}\nRequires: #{names.inspect}, found: #{fields.inspect}") unless missing.empty?
  fields - names
end

#validate_scope(scope) ⇒ Object

Given a scope, validates that the fields required by this ExprStub are available in the values fields of the scope. Returns those values fields which are unused in the expression.



91
92
93
# File 'lib/cascading/expr_stub.rb', line 91

def validate_scope(scope)
  validate_fields(scope.values_fields.to_a)
end