Class: UseTheForce::Base::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/use_the_force/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, target:) ⇒ Relation

Returns a new instance of Relation.



3
4
5
6
7
8
# File 'lib/use_the_force/base.rb', line 3

def initialize(client, target:)
  @client = client
  @filters = {}
  @target = target
  @soql = ""
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object



63
64
65
# File 'lib/use_the_force/base.rb', line 63

def method_missing(meth, *, &)
  collection.send(meth, *, &)
end

Instance Method Details

#collectionObject



59
60
61
# File 'lib/use_the_force/base.rb', line 59

def collection
  @collection ||= @client.query(@soql)
end

#create(**attributes) ⇒ Object



14
15
16
# File 'lib/use_the_force/base.rb', line 14

def create(**attributes)
  @client.create(@target.read_table_name, **attributes)
end

#destroy(id) ⇒ Object



22
23
24
# File 'lib/use_the_force/base.rb', line 22

def destroy(id)
  @client.destroy(@target.read_table_name, id)
end

#find(id) ⇒ Object



55
56
57
# File 'lib/use_the_force/base.rb', line 55

def find(id)
  @client.find(@target.read_table_name, id)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/use_the_force/base.rb', line 67

def respond_to_missing?(method_name, include_private = false)
  collection.respond_to?(method_name, include_private)
end

#to_restforce_collectionObject



10
11
12
# File 'lib/use_the_force/base.rb', line 10

def to_restforce_collection
  collection
end

#update(id, **attributes) ⇒ Object



18
19
20
# File 'lib/use_the_force/base.rb', line 18

def update(id, **attributes)
  @client.update(@target.read_table_name, Id: id, **attributes)
end

#where(**other_filters) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/use_the_force/base.rb', line 26

def where(**other_filters)
  @filters.merge!(other_filters) unless other_filters.empty?

  conditions =
    @filters.map do |attr, value|
      if value.is_a?(String)
        "#{attr} = '#{value}'"
      elsif value.is_a?(Float)
        "#{attr} = #{value}"
      elsif [true, false].include?(value)
        "#{attr} = #{value}"
      else
        "#{attr} = '#{value}'"
      end
    end.join(" AND ")

  where_clause =
    if conditions.empty?
      ""
    else
      conditions.prepend(" WHERE ")
    end

  @collection = nil
  @soql = "SELECT #{@target.read_fields.join(", ")} FROM #{@target.read_table_name} #{where_clause}"
  collection # Right before "self" at the end of the `where` method
  self
end