Class: Hotdog::Expression::TagExpressionNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, tag_value, separator = nil) ⇒ TagExpressionNode

Returns a new instance of TagExpressionNode.



526
527
528
529
530
531
# File 'lib/hotdog/expression/semantics.rb', line 526

def initialize(tag_name, tag_value, separator=nil)
  @tag_name = tag_name
  @tag_value = tag_value
  @separator = separator
  @fallback = nil
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



534
535
536
# File 'lib/hotdog/expression/semantics.rb', line 534

def separator
  @separator
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



532
533
534
# File 'lib/hotdog/expression/semantics.rb', line 532

def tag_name
  @tag_name
end

#tag_valueObject (readonly)

Returns the value of attribute tag_value.



533
534
535
# File 'lib/hotdog/expression/semantics.rb', line 533

def tag_value
  @tag_value
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  self.class == other.class and @tag_name == other.tag_name and @tag_value == other.tag_value
end

#condition(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


575
576
577
# File 'lib/hotdog/expression/semantics.rb', line 575

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

#condition_tables(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


579
580
581
# File 'lib/hotdog/expression/semantics.rb', line 579

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

#condition_values(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


583
584
585
# File 'lib/hotdog/expression/semantics.rb', line 583

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

#dump(options = {}) ⇒ Object



661
662
663
664
665
666
667
668
# File 'lib/hotdog/expression/semantics.rb', line 661

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

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



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/hotdog/expression/semantics.rb', line 587

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 @fallback
          # avoid optimizing @fallback to prevent infinite recursion
          values = @fallback.evaluate(environment, options.merge(did_fallback: true))
          if values.empty?
            if reload(environment, options)
              evaluate(environment, options).tap do |values|
                if values.empty?
                  environment.logger.info("no result: #{self.dump.inspect}")
                end
              end
            else
              []
            end
          else
            values
          end
        else
          if reload(environment, options)
            evaluate(environment, options).tap do |values|
              if values.empty?
                environment.logger.info("no result: #{self.dump.inspect}")
              end
            end
          else
            []
          end
        end
      end
    else
      values
    end
  else
    []
  end
end

#maybe_fallback(options = {}) ⇒ Object



670
671
672
# File 'lib/hotdog/expression/semantics.rb', line 670

def maybe_fallback(options={})
  nil
end

#maybe_glob(s) ⇒ Object



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

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

#maybe_query(options = {}) ⇒ Object



548
549
550
551
552
553
554
555
# File 'lib/hotdog/expression/semantics.rb', line 548

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



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/hotdog/expression/semantics.rb', line 557

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



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

def optimize(options={})
  # fallback to glob expression
  @fallback = maybe_fallback(options)
  self
end

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



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

def reload(environment, options={})
  $did_reload ||= false
  if $did_reload
    false
  else
    $did_reload = true
    environment.logger.info("force reloading all hosts and tags.")
    environment.reload(force: true)
    true
  end
end

#separator?Boolean

Returns:

  • (Boolean)


544
545
546
# File 'lib/hotdog/expression/semantics.rb', line 544

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

#tag_name?Boolean

Returns:

  • (Boolean)


536
537
538
# File 'lib/hotdog/expression/semantics.rb', line 536

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

#tag_value?Boolean

Returns:

  • (Boolean)


540
541
542
# File 'lib/hotdog/expression/semantics.rb', line 540

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

#to_glob(s) ⇒ Object



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

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