Class: MindMatch::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mind_match/query_builder.rb

Instance Method Summary collapse

Constructor Details

#initializeQueryBuilder

Returns a new instance of QueryBuilder.



3
4
5
# File 'lib/mind_match/query_builder.rb', line 3

def initialize
  @fields = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)



49
50
51
52
53
54
55
56
57
58
# File 'lib/mind_match/query_builder.rb', line 49

def method_missing(m, *args, &block)
  method = m.to_s
  if method.start_with?('with_')
    key = method.split('with_')[1]
    @fields[key] = args[0]
    self
  else
    super(m, *args, &block)
  end
end

Instance Method Details

#buildObject



7
8
9
10
# File 'lib/mind_match/query_builder.rb', line 7

def build
  @fields = @fields.freeze
  self
end

#to_hObject



12
13
14
15
16
# File 'lib/mind_match/query_builder.rb', line 12

def to_h
  Hash[
    fields.map { |k, _v| [k, []] }
  ]
end

#to_sObject



18
19
20
21
22
23
24
25
26
# File 'lib/mind_match/query_builder.rb', line 18

def to_s
  ''.tap do |s|
    fields.each do |k, v|
      s << "#{k} {"
      s << join(v)
      s << "}\n"
    end
  end
end