Class: SupabaseApi::Record
- Inherits:
-
Object
- Object
- SupabaseApi::Record
- Defined in:
- lib/supabase_api/record.rb
Direct Known Subclasses
Class Method Summary collapse
- .all ⇒ Object
- .create(params) ⇒ Object
- .find(id) ⇒ Object
- .find_by_id(id) ⇒ Object
- .table_name ⇒ Object
- .where(params = {}) ⇒ Object
Instance Method Summary collapse
- #assign_attributes(params = {}) ⇒ Object
- #attributes ⇒ Object
- #destroy ⇒ Object
-
#initialize(params = {}) ⇒ Record
constructor
A new instance of Record.
- #save ⇒ Object
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
.all ⇒ Object
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_name ⇒ Object
5 6 7 |
# File 'lib/supabase_api/record.rb', line 5 def self.table_name raise ArgumentError 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 |
#attributes ⇒ Object
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 |
#destroy ⇒ Object
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 |