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.



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

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.



559
560
561
# File 'lib/hotdog/expression/semantics.rb', line 559

def separator
  @separator
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



557
558
559
# File 'lib/hotdog/expression/semantics.rb', line 557

def tag_name
  @tag_name
end

#tag_valueObject (readonly)

Returns the value of attribute tag_value.



558
559
560
# File 'lib/hotdog/expression/semantics.rb', line 558

def tag_value
  @tag_value
end

Instance Method Details

#==(other) ⇒ Object



656
657
658
# File 'lib/hotdog/expression/semantics.rb', line 656

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

#condition(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


600
601
602
# File 'lib/hotdog/expression/semantics.rb', line 600

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

#condition_tables(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


604
605
606
# File 'lib/hotdog/expression/semantics.rb', line 604

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

#condition_values(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


608
609
610
# File 'lib/hotdog/expression/semantics.rb', line 608

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

#dump(options = {}) ⇒ Object



686
687
688
689
690
691
692
693
# File 'lib/hotdog/expression/semantics.rb', line 686

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



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/hotdog/expression/semantics.rb', line 612

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



695
696
697
# File 'lib/hotdog/expression/semantics.rb', line 695

def maybe_fallback(options={})
  nil
end

#maybe_glob(s) ⇒ Object



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

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

#maybe_query(options = {}) ⇒ Object



573
574
575
576
577
578
579
580
# File 'lib/hotdog/expression/semantics.rb', line 573

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



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/hotdog/expression/semantics.rb', line 582

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



660
661
662
663
664
# File 'lib/hotdog/expression/semantics.rb', line 660

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

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



674
675
676
677
678
679
680
681
682
683
684
# File 'lib/hotdog/expression/semantics.rb', line 674

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)


569
570
571
# File 'lib/hotdog/expression/semantics.rb', line 569

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

#tag_name?Boolean

Returns:

  • (Boolean)


561
562
563
# File 'lib/hotdog/expression/semantics.rb', line 561

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

#tag_value?Boolean

Returns:

  • (Boolean)


565
566
567
# File 'lib/hotdog/expression/semantics.rb', line 565

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

#to_glob(s) ⇒ Object



666
667
668
# File 'lib/hotdog/expression/semantics.rb', line 666

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