class Hotdog::Expression::TagExpressionNode

Attributes

separator[R]
tagname[R]
tagvalue[R]

Public Class Methods

new(tagname, tagvalue, separator=nil, options={}) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 628
def initialize(tagname, tagvalue, separator=nil, options={})
  @tagname = tagname
  @tagvalue = tagvalue
  @separator = separator
  @options = options
end

Public Instance Methods

==(other) click to toggle source
# 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={}) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 677
def condition(options={})
  raise(NotImplementedError.new("must be overridden"))
end
condition_tables(options={}) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 681
def condition_tables(options={})
  raise(NotImplementedError.new("must be overridden"))
end
condition_values(options={}) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 685
def condition_values(options={})
  raise(NotImplementedError.new("must be overridden"))
end
dump(options={}) click to toggle source
# 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={}) click to toggle source
# 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 environment.use_fallback? 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={}) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 742
def maybe_fallback(options={})
  nil
end
maybe_glob(s) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 729
def maybe_glob(s)
  s ? to_glob(s.to_s) : nil
end
maybe_query(options={}) click to toggle source
# 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={}) click to toggle source
# 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={}) click to toggle source
# 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?() click to toggle source
# File lib/hotdog/expression/semantics.rb, line 646
def separator?
  !(separator.nil? or separator.to_s.empty?)
end
tagname?() click to toggle source
# File lib/hotdog/expression/semantics.rb, line 638
def tagname?
  !(tagname.nil? or tagname.to_s.empty?)
end
tagvalue?() click to toggle source
# File lib/hotdog/expression/semantics.rb, line 642
def tagvalue?
  !(tagvalue.nil? or tagvalue.to_s.empty?)
end
to_glob(s) click to toggle source
# File lib/hotdog/expression/semantics.rb, line 725
def to_glob(s)
  (s.start_with?("*") ? "" : "*") + s.gsub(/[-.\/_]/, "?") + (s.end_with?("*") ? "" : "*")
end