Class: Hotdog::Expression::TagExpressionNode

Inherits:
ExpressionNode show all
Defined in:
lib/hotdog/expression/semantics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ExpressionNode

#compact

Constructor Details

#initialize(tagname, tagvalue, separator = nil, options = {}) ⇒ TagExpressionNode

Returns a new instance of TagExpressionNode.



628
629
630
631
632
633
# File 'lib/hotdog/expression/semantics.rb', line 628

def initialize(tagname, tagvalue, separator=nil, options={})
  @tagname = tagname
  @tagvalue = tagvalue
  @separator = separator
  @options = options
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



636
637
638
# File 'lib/hotdog/expression/semantics.rb', line 636

def separator
  @separator
end

#tagnameObject (readonly)

Returns the value of attribute tagname.



634
635
636
# File 'lib/hotdog/expression/semantics.rb', line 634

def tagname
  @tagname
end

#tagvalueObject (readonly)

Returns the value of attribute tagvalue.



635
636
637
# File 'lib/hotdog/expression/semantics.rb', line 635

def tagvalue
  @tagvalue
end

Instance Method Details

#==(other) ⇒ Object



712
713
714
# File 'lib/hotdog/expression/semantics.rb', line 712

def ==(other)
  self.class == other.class and @tagname == other.tagname and @tagvalue == other.tagvalue
end

#condition(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


677
678
679
# File 'lib/hotdog/expression/semantics.rb', line 677

def condition(options={})
  raise(NotImplementedError.new("must be overridden"))
end

#condition_tables(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


681
682
683
# File 'lib/hotdog/expression/semantics.rb', line 681

def condition_tables(options={})
  raise(NotImplementedError.new("must be overridden"))
end

#condition_values(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


685
686
687
# File 'lib/hotdog/expression/semantics.rb', line 685

def condition_values(options={})
  raise(NotImplementedError.new("must be overridden"))
end

#dump(options = {}) ⇒ Object



733
734
735
736
737
738
739
740
# File 'lib/hotdog/expression/semantics.rb', line 733

def dump(options={})
  data = {}
  data[:tagname] = tagname.to_s if tagname
  data[:separator] = separator.to_s if separator
  data[:tagvalue] = tagvalue.to_s if tagvalue
  data[:fallback] = @options[:fallback].dump(options) if @options[:fallback]
  data
end

#evaluate(environment, options = {}) ⇒ Object



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/hotdog/expression/semantics.rb', line 689

def evaluate(environment, options={})
  q = maybe_query(options)
  if q
    values = environment.execute(q, condition_values(options)).map { |row| row.first }
    if values.empty?
      if options[:did_fallback]
        []
      else
        if not environment.fixed_string? and @options[:fallback]
          # avoid optimizing @options[:fallback] to prevent infinite recursion
          @options[:fallback].evaluate(environment, options.merge(did_fallback: true))
        else
          []
        end
      end
    else
      values
    end
  else
    []
  end
end

#maybe_fallback(options = {}) ⇒ Object



742
743
744
# File 'lib/hotdog/expression/semantics.rb', line 742

def maybe_fallback(options={})
  nil
end

#maybe_glob(s) ⇒ Object



729
730
731
# File 'lib/hotdog/expression/semantics.rb', line 729

def maybe_glob(s)
  s ? to_glob(s.to_s) : nil
end

#maybe_query(options = {}) ⇒ Object



650
651
652
653
654
655
656
657
# File 'lib/hotdog/expression/semantics.rb', line 650

def maybe_query(options={})
  query_without_condition = maybe_query_without_condition(options)
  if query_without_condition
    query_without_condition.sub(/\s*;\s*\z/, " WHERE #{condition(options)};")
  else
    nil
  end
end

#maybe_query_without_condition(options = {}) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/hotdog/expression/semantics.rb', line 659

def maybe_query_without_condition(options={})
  tables = condition_tables(options)
  if tables.empty?
    nil
  else
    case tables
    when [:hosts]
      "SELECT hosts.id AS host_id FROM hosts;"
    when [:hosts, :tags]
      "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags INNER JOIN hosts ON hosts_tags.host_id = hosts.id INNER JOIN tags ON hosts_tags.tag_id = tags.id;"
    when [:tags]
      "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags INNER JOIN tags ON hosts_tags.tag_id = tags.id;"
    else
      raise(NotImplementedError.new("unknown tables: #{tables.join(", ")}"))
    end
  end
end

#optimize(options = {}) ⇒ Object



716
717
718
719
720
721
722
723
# File 'lib/hotdog/expression/semantics.rb', line 716

def optimize(options={})
  # fallback to glob expression
  self.dup.tap do |o_self|
    o_self.instance_eval {
      @options[:fallback] ||= maybe_fallback(options)
    }
  end
end

#separator?Boolean

Returns:

  • (Boolean)


646
647
648
# File 'lib/hotdog/expression/semantics.rb', line 646

def separator?
  !(separator.nil? or separator.to_s.empty?)
end

#tagname?Boolean

Returns:

  • (Boolean)


638
639
640
# File 'lib/hotdog/expression/semantics.rb', line 638

def tagname?
  !(tagname.nil? or tagname.to_s.empty?)
end

#tagvalue?Boolean

Returns:

  • (Boolean)


642
643
644
# File 'lib/hotdog/expression/semantics.rb', line 642

def tagvalue?
  !(tagvalue.nil? or tagvalue.to_s.empty?)
end

#to_glob(s) ⇒ Object



725
726
727
# File 'lib/hotdog/expression/semantics.rb', line 725

def to_glob(s)
  (s.start_with?("*") ? "" : "*") + s.gsub(/[-.\/_]/, "?") + (s.end_with?("*") ? "" : "*")
end