Class: OracleSqlParser::Ast::Hash

Inherits:
Base show all
Extended by:
Forwardable
Defined in:
lib/oracle-sql-parser/ast/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #ast, deep_dup, #deep_dup, find_different_value, #initialize_copy, #map_ast

Methods included from Util::Parameterizable

#to_parameterized, #to_parameternized

Constructor Details

#initialize(value = {}) ⇒ Hash

Returns a new instance of Hash.



8
9
10
11
# File 'lib/oracle-sql-parser/ast/hash.rb', line 8

def initialize(value = {})
  raise "only ::Hash instance #{value.inspect}" unless value.instance_of? ::Hash
  @ast = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



54
55
56
57
# File 'lib/oracle-sql-parser/ast/hash.rb', line 54

def method_missing(name, *args)
  return @ast.send(:[], name) if @ast.has_key? name
  raise "no method:#{name}, #{@ast.class} in #{self.class}"
end

Class Method Details

.[](value) ⇒ Object



46
47
48
# File 'lib/oracle-sql-parser/ast/hash.rb', line 46

def self.[](value)
  self.new(value)
end

Instance Method Details

#[]=(name, value) ⇒ Object



50
51
52
# File 'lib/oracle-sql-parser/ast/hash.rb', line 50

def []=(name, value)
  @ast[name] = value
end

#inspectObject



30
31
32
33
34
# File 'lib/oracle-sql-parser/ast/hash.rb', line 30

def inspect
  "#<#{self.class.name}\n" +
  @ast.map{|k,v| "#{k.inspect} => #{v.inspect}"}.join(",\n").gsub(/^/, '  ') +
  "}>\n"
end

#map_ast!(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/oracle-sql-parser/ast/hash.rb', line 19

def map_ast!(&block)
  mapped = @ast.class.new
  @ast.each do |k, v|
    if v.is_a? OracleSqlParser::Ast::Base
      v.map_ast!(&block)
    end
    mapped[k] = block.call(v)
  end
  @ast = mapped
end

#remove_nil_values!Object



13
14
15
16
17
# File 'lib/oracle-sql-parser/ast/hash.rb', line 13

def remove_nil_values!
  @ast.delete_if{|k, v| v.nil?}
  @ast.each {|k, v| v.remove_nil_values! if v.respond_to? :remove_nil_values!}
  self
end

#to_sql(options = {:separator => ' '}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/oracle-sql-parser/ast/hash.rb', line 36

def to_sql(options = {:separator => ' '})
  @ast.map do |k,v|
    if v.respond_to? :to_sql
      v.to_sql
    else
      v.to_s
    end
  end.compact.join(options[:separator])
end