Class: Cascading::ExprStub
- Inherits:
-
Object
- Object
- Cascading::ExprStub
- Defined in:
- lib/cascading/expr_stub.rb
Instance Attribute Summary collapse
-
#expression ⇒ Object
Returns the value of attribute expression.
-
#input_expression ⇒ Object
Returns the value of attribute input_expression.
-
#types ⇒ Object
Returns the value of attribute types.
Class Method Summary collapse
-
.expr(expression, options = {}) ⇒ Object
Convenience constructor for an ExprStub that optionally performs validation.
Instance Method Summary collapse
-
#compile ⇒ Object
Scan, parse, and compile expression, then return this ExprStub upon success.
-
#eval(actual_args) ⇒ Object
Evaluates this ExprStub given a hash mapping argument names to argument values.
-
#initialize(expression) ⇒ ExprStub
constructor
ExprStub requires a Janino expression decorated with field types.
-
#names_and_types ⇒ Object
Extract Java names and types from @types hash.
-
#to_s ⇒ Object
Prints the original input expression.
-
#validate(actual_args = {}) ⇒ Object
Evaluates this ExprStub with default values for each actual argument.
-
#validate_fields(fields) ⇒ Object
Throws an exception if any arguments required by this ExprStub are missing from fields.
-
#validate_scope(scope) ⇒ Object
Given a scope, validates that the fields required by this ExprStub are available in the values fields of the scope.
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
#expression ⇒ Object
Returns the value of attribute expression.
3 4 5 |
# File 'lib/cascading/expr_stub.rb', line 3 def expression @expression end |
#input_expression ⇒ Object
Returns the value of attribute input_expression.
3 4 5 |
# File 'lib/cascading/expr_stub.rb', line 3 def input_expression @input_expression end |
#types ⇒ Object
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, = {}) = { :validate => true, :validate_with => {} }.merge() expr_stub = expression.kind_of?(ExprStub) ? expression : ExprStub.new(expression).compile expr_stub.validate([:validate_with]) if [:validate] puts "Expression validation is disabled for '#{expression}'" unless [:validate] expr_stub end |
Instance Method Details
#compile ⇒ Object
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_types ⇒ Object
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_s ⇒ Object
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.
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 |