Class: GRel::QL::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/grel/ql.rb

Overview

end of QueryContext

Constant Summary collapse

DEFINED_FILTERS =
{
  :$neq => true, :$eq => true, :$lt => true,
  :$lteq => true, :$gt => true, :$gteq => true,
  :$not => true, :$like => true,
  :$or => true, :$and => true, :$in => true
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Filter

Returns a new instance of Filter.



520
521
522
523
524
# File 'lib/grel/ql.rb', line 520

def initialize(context)
  @context = context
  @acum = ""
  @variable = context.fresh_filter_predicate
end

Instance Attribute Details

#acumObject (readonly)

Returns the value of attribute acum.



518
519
520
# File 'lib/grel/ql.rb', line 518

def acum
  @acum
end

#variableObject (readonly)

Returns the value of attribute variable.



518
519
520
# File 'lib/grel/ql.rb', line 518

def variable
  @variable
end

Class Method Details

.filter?(h) ⇒ Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/grel/ql.rb', line 514

def self.filter?(h)
  h.keys.length == 1 && DEFINED_FILTERS[h.keys.first]
end

Instance Method Details

#generate_and(v) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/grel/ql.rb', line 635

def generate_and(v)
  if(v.is_a?(Array))
    @acum += "("
    v.each_with_index do |f,i|
      parse_filter(f)
      @acum += "&&" if i<v.length-1
    end
    @acum += ")"
  else
    raise Exception.new("$or filter must accept an array of conditions")
  end
end

#generate_eq(v) ⇒ Object



551
552
553
554
555
556
557
558
559
# File 'lib/grel/ql.rb', line 551

def generate_eq(v)
  @acum = @acum + "(#{variable} = "
  operator = if(v.is_a?(Hash))
               parse_filter(v)
             else
               QL.to_query(v,@context)
             end
  @acum+="#{operator})"
end

#generate_gt(v) ⇒ Object



581
582
583
584
585
586
587
588
589
# File 'lib/grel/ql.rb', line 581

def generate_gt(v)
  @acum = @acum + "(#{variable} > "
  operator = if(v.is_a?(Hash))
               parse_filter(v)
             else
               QL.to_query(v,@context)
             end
  @acum+="#{operator})"
end

#generate_gteq(v) ⇒ Object



591
592
593
594
595
596
597
598
599
# File 'lib/grel/ql.rb', line 591

def generate_gteq(v)
  @acum = @acum + "(#{variable} <= "
  operator = if(v.is_a?(Hash))
               parse_filter(v)
             else
               QL.to_query(v,@context)
             end
  @acum+="#{operator})"
end

#generate_in(v) ⇒ Object



537
538
539
# File 'lib/grel/ql.rb', line 537

def generate_in(v)
  generate_or v.map{|x| {:$eq => x} }
end

#generate_like(v) ⇒ Object



611
612
613
614
615
616
617
618
619
620
# File 'lib/grel/ql.rb', line 611

def generate_like(v)
  operator = if(v.is_a?(Regexp))
               v.source
             elsif(v.is_a?(String))
               v
             else
               raise Exception.new("Only Regexes and Strings can be used with the $like operator")
             end
  @acum += "(regex(#{variable},\"#{operator}\",\"i\"))"
end

#generate_lt(v) ⇒ Object



561
562
563
564
565
566
567
568
569
# File 'lib/grel/ql.rb', line 561

def generate_lt(v)
  @acum = @acum + "(#{variable} < "
  operator = if(v.is_a?(Hash))
               parse_filter(v)
             else
               QL.to_query(v,@context)
             end
  @acum+="#{operator})"
end

#generate_lteq(v) ⇒ Object



571
572
573
574
575
576
577
578
579
# File 'lib/grel/ql.rb', line 571

def generate_lteq(v)
  @acum = @acum + "(#{variable} <= "
  operator = if(v.is_a?(Hash))
               parse_filter(v)
             else
               QL.to_query(v,@context)
             end
  @acum+="#{operator})"
end

#generate_neq(v) ⇒ Object



541
542
543
544
545
546
547
548
549
# File 'lib/grel/ql.rb', line 541

def generate_neq(v)
  @acum = @acum + "(#{variable} != "
  operator = if(v.is_a?(Hash))
               parse_filter(v)
             else
               QL.to_query(v,@context)
             end
  @acum+="#{operator})"
end

#generate_not(v) ⇒ Object



601
602
603
604
605
606
607
608
609
# File 'lib/grel/ql.rb', line 601

def generate_not(v)
  @acum += "!("
  if(v.is_a?(Hash))
    parse_filter(v)
  else
    @acum += QL.to_query(v,@context)
  end
  @acum += ")"
end

#generate_or(v) ⇒ Object



622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/grel/ql.rb', line 622

def generate_or(v)
  if(v.is_a?(Array))
    @acum += "("
    v.each_with_index do |f,i|
      parse_filter(f)
      @acum += "||" if i<v.length-1
    end
    @acum += ")"
  else
    raise Exception.new("$or filter must accept an array of conditions")
  end
end

#parse(h) ⇒ Object



526
527
528
529
# File 'lib/grel/ql.rb', line 526

def parse(h)
  parse_filter(h)
  @acum = "FILTER("+ @acum + ")"
end

#parse_filter(h) ⇒ Object



531
532
533
534
535
# File 'lib/grel/ql.rb', line 531

def parse_filter(h)
  k = h.keys.first
  v = h.values.first
  self.send "generate_#{k.to_s.split("$").last}".to_sym, v
end