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, params = {}) ⇒ 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.
- #to_s ⇒ Object
-
#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
Constructor Details
#initialize(expression) ⇒ ExprStub
ExprStub requires a Janino expression decorated with field types. For example: ‘“Found: ” + (x:int + y:int) + “ ” + z:string’. Type names are defined in Cascading::JAVA_TYPE_MAP.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cascading/expr_stub.rb', line 8 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, params = {}) ⇒ Object
Convenience constructor for an ExprStub that optionally performs validation. Takes a string to use as a Janino expression and an optional params hash. By default, the param :validate is set to true (performs expression validation using default actual argument values) and the param :validate_with is set to {} (which doesn’t override any of the default actual argument values used for validation).
34 35 36 37 38 39 40 |
# File 'lib/cascading/expr_stub.rb', line 34 def self.expr(expression, params = {}) params = { :validate => true, :validate_with => {} }.merge(params) expr_stub = expression.kind_of?(ExprStub) ? expression : ExprStub.new(expression).compile expr_stub.validate(params[:validate_with]) if params[:validate] puts "Expression validation is disabled for '#{expression}'" unless params[: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.
44 45 46 47 |
# File 'lib/cascading/expr_stub.rb', line 44 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.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cascading/expr_stub.rb', line 52 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 |
#to_s ⇒ Object
24 25 26 |
# File 'lib/cascading/expr_stub.rb', line 24 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.
67 68 69 |
# File 'lib/cascading/expr_stub.rb', line 67 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.
78 79 80 81 82 83 |
# File 'lib/cascading/expr_stub.rb', line 78 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
71 72 73 |
# File 'lib/cascading/expr_stub.rb', line 71 def validate_scope(scope) validate_fields(scope.values_fields.to_a) end |