Class: SupabaseApi::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/supabase_api/record.rb

Direct Known Subclasses

Sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Record

Returns a new instance of Record.



63
64
65
66
67
68
69
# File 'lib/supabase_api/record.rb', line 63

def initialize(params = {})
  params.each do |key,value|
    instance_variable_set("@#{key}", value)
  end

  instance_variables.each {|var| self.class.send(:attr_accessor, var.to_s.delete('@'))}
end

Class Method Details

.allObject



9
10
11
# File 'lib/supabase_api/record.rb', line 9

def self.all
  where()
end

.create(params) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/supabase_api/record.rb', line 41

def self.create(params)
  is_multi_rows = params.kind_of? Array

  client = Client.new
  response = client.create(
    table_name,
    params
  )

  if response.success? && !response.parsed_response.empty?
    if is_multi_rows
      response.parsed_response.map do |data|
        new(data)
      end
    else
      new(response.parsed_response.first)
    end
  else
    nil
  end      
end

.find(id) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/supabase_api/record.rb', line 25

def self.find(id)
  response = Client.new.find(table_name, id)

  if response.success? && !response.parsed_response.empty?
    new(response.parsed_response.first)
  else
    raise RecordNotFound
  end
end

.find_by_id(id) ⇒ Object



35
36
37
38
39
# File 'lib/supabase_api/record.rb', line 35

def self.find_by_id(id)
  find(id)
rescue RecordNotFound
  nil
end

.table_nameObject

Raises:

  • (ArgumentError)


5
6
7
# File 'lib/supabase_api/record.rb', line 5

def self.table_name
  raise ArgumentError
end

.where(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/supabase_api/record.rb', line 13

def self.where(params = {})
  output = []

  response = Client.new.list(table_name, params)

  response.parsed_response.each do |record_hash|
    output << new(record_hash)
  end

  output      
end

Instance Method Details

#assign_attributes(params = {}) ⇒ Object



77
78
79
80
81
# File 'lib/supabase_api/record.rb', line 77

def assign_attributes(params = {})
  params.each do |key,value|
    instance_variable_set("@#{key}", value)
  end      
end

#attributesObject



71
72
73
74
75
# File 'lib/supabase_api/record.rb', line 71

def attributes
  instance_variables.each_with_object({}) do |var, hash|
    hash[var.to_s.delete("@").to_sym] = instance_variable_get(var)
  end
end

#destroyObject

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/supabase_api/record.rb', line 96

def destroy
  raise InvalidRequest if id.nil?

  response = Client.new.destroy(
    self.class.table_name,
    id
  )
  
  return true if response.success?

  raise RecordNotDestroyed
end

#saveObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/supabase_api/record.rb', line 83

def save
  if id.nil?
    self.class.create(attributes)
  else
    response = Client.new.update(
      self.class.table_name,
      id,
      attributes
    )
    self.class.new(response.parsed_response.first)
  end
end