Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added jquery_query_build_rails_test
Empty file.
1 change: 1 addition & 0 deletions jquery_query_builder-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "railties", ">= 3.1"
spec.add_dependency "json", ">= 1.8.3"
spec.add_dependency "activesupport"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
Expand Down
1 change: 1 addition & 0 deletions lib/jquery_query_builder-rails.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "jquery_query_builder/rails/version"
require "jquery_query_builder/evaluator"
require "jquery_query_builder/generator"

module JqueryQueryBuilder
module Rails
Expand Down
30 changes: 13 additions & 17 deletions lib/jquery_query_builder/evaluator.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/string/inflections'
require 'jquery_query_builder/operator'
require 'jquery_query_builder/rule_group'
require 'jquery_query_builder/rule'
require 'json'
require 'jquery_query_builder/rule'
require 'jquery_query_builder/rule_group'
require 'jquery_query_builder/evaluators/boolean/evaluator'
require 'active_support/core_ext/module/delegation'

module JqueryQueryBuilder
class Evaluator
attr_accessor :parsed_rule_set
def initialize(rule_set)
if rule_set.is_a? String
attr_accessor :parsed_rule_set, :evaluator
def initialize(raw_rule_set, evaluator_class: Evaluators::Boolean::Evaluator)
if raw_rule_set.is_a? String
#assuming the json was passed in
self.parsed_rule_set = JSON.parse(rule_set)
self.parsed_rule_set = JSON.parse(raw_rule_set)
else
self.parsed_rule_set = rule_set
self.parsed_rule_set = raw_rule_set
end
end

def get_matching_objects(objects)
objects.select{|o| object_matches_rules?(o)}
rule_set = RuleGroup.new(parsed_rule_set)
self.evaluator = evaluator_class.new(rule_set)
end

def object_matches_rules?(object)
RuleGroup.new(parsed_rule_set).evaluate(object)
end
# All evaluators must implement
delegate :get_matching_objects, :object_matches_rules?, to: :evaluator
end
end

Expand Down
26 changes: 26 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/evaluator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/string/inflections'
require 'jquery_query_builder/evaluators/boolean/operator'
require 'jquery_query_builder/evaluators/boolean/rule_group_evaluator'

module JqueryQueryBuilder
module Evaluators
module Boolean
class Evaluator
attr_accessor :rule_group
def initialize(rule_group)
self.rule_group = rule_group
end

def get_matching_objects(objects)
objects.select{|o| object_matches_rules?(o)}
end

def object_matches_rules?(object)
RuleGroupEvaluator.new(rule_group).evaluate(object)
end
end
end
end
end
52 changes: 52 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#require all the operators
Dir[File.join(File.dirname(__FILE__) + '/operators', "**/*.rb")].each do |f|
require f
end

module JqueryQueryBuilder
module Evaluators
module Boolean
class Operator
SPECIAL_CASE_MAP = {
}

def self.get_operator_class(operator)
operator_class = SPECIAL_CASE_MAP[operator] || operator.camelize

begin
operator_class = "#{operator_module}::#{operator_class}".constantize
rescue NameError
raise "Unknown operator #{operator} used."
end
end

def self.operator_module
"JqueryQueryBuilder::Evaluators::Boolean::Operators"
end
end
end
end
end

__END__

equal
not_equal
in
not_in
less
less_or_equal
greater
greater_or_equal
between
not_between
begins_with
not_begins_with
contains
not_contains
ends_with
not_ends_with
is_empty
is_not_empty
is_null
is_not_null
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class BeginsWith
def evaluate(left, right)
left.start_with?(right)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/between.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class Between
def evaluate(input, bounds)
input > bounds[0] && input < bounds[1]
end
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/contains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class Contains
def evaluate(left, right)
return false if left.nil?
left.include?(right)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/ends_with.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class EndsWith
def evaluate(left, right)
left.end_with?(right)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/equal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class Equal
def evaluate(left, right)
left == right
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/greater.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class Greater
def evaluate(left, right)
left > right
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class GreaterOrEqual
def evaluate(left, right)
left >= right
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/in.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class In
def evaluate(left, right)
right.include?(left)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/is_empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class IsEmpty
def evaluate(input, value)
input.blank?
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class IsNotEmpty
def evaluate(input, value)
input.present?
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class IsNotNull
def evaluate(input, value)
!input.nil?
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/is_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class IsNull
def evaluate(input, value)
input.nil?
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/jquery_query_builder/evaluators/boolean/operators/less.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class Less
def evaluate(left, right)
left < right
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class LessOrEqual
def evaluate(left, right)
left <= right
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class NotBeginsWith
def evaluate(left, right)
!left.start_with?(right)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class NotBetween
def evaluate(input, bounds)
input <= bounds[0] || input >= bounds[1]
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class NotContains
def evaluate(left, right)
!left.include?(right)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JqueryQueryBuilder
module Evaluators
module Boolean
module Operators
class NotEndsWith
def evaluate(left, right)
!left.end_with?(right)
end
end
end
end
end
end
Loading