Class: RubyJsonApiClient::Base
- Inherits:
-
Object
- Object
- RubyJsonApiClient::Base
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Model, ActiveModel::Serialization, ActiveModel::SerializerSupport
- Defined in:
- lib/ruby_json_api_client/base.rb
Instance Attribute Summary collapse
-
#__origin__ ⇒ Object
Returns the value of attribute __origin__.
-
#meta ⇒ Object
Returns the value of attribute meta.
Class Method Summary collapse
- ._identifier ⇒ Object
- .all ⇒ Object
- .attributes ⇒ Object
- .create(params) ⇒ Object
- .field(name, type = :string) ⇒ Object
- .fields ⇒ Object
- .find(id) ⇒ Object
- .has_field?(name) ⇒ Boolean
- .has_many(name, options = {}) ⇒ Object
- .has_many_relationships ⇒ Object
- .has_one(name, options = {}) ⇒ Object
- .identifier(name) ⇒ Object
- .where(params) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?, #equal?)
- #attributes ⇒ Object
- #destroy ⇒ Object
- #loaded_has_ones ⇒ Object
- #persisted? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #update_attributes(data) ⇒ Object
Instance Attribute Details
#__origin__ ⇒ Object
Returns the value of attribute __origin__.
49 50 51 |
# File 'lib/ruby_json_api_client/base.rb', line 49 def __origin__ @__origin__ end |
#meta ⇒ Object
Returns the value of attribute meta.
48 49 50 |
# File 'lib/ruby_json_api_client/base.rb', line 48 def @meta end |
Class Method Details
._identifier ⇒ Object
43 44 45 |
# File 'lib/ruby_json_api_client/base.rb', line 43 def self._identifier @_identifier || superclass._identifier end |
.all ⇒ Object
110 111 112 |
# File 'lib/ruby_json_api_client/base.rb', line 110 def self.all where({}) end |
.attributes ⇒ Object
23 24 25 26 27 28 |
# File 'lib/ruby_json_api_client/base.rb', line 23 def self.attributes fields.reduce({}) do |attributes, field| attributes[field] = nil attributes end end |
.create(params) ⇒ Object
118 119 120 |
# File 'lib/ruby_json_api_client/base.rb', line 118 def self.create(params) new(params).tap(&:save) end |
.field(name, type = :string) ⇒ Object
14 15 16 17 |
# File 'lib/ruby_json_api_client/base.rb', line 14 def self.field(name, type = :string) fields << name attr_accessor name end |
.fields ⇒ Object
19 20 21 |
# File 'lib/ruby_json_api_client/base.rb', line 19 def self.fields @_fields ||= Set.new [_identifier] end |
.find(id) ⇒ Object
106 107 108 |
# File 'lib/ruby_json_api_client/base.rb', line 106 def self.find(id) RubyJsonApiClient::Store.instance.find(self, id) end |
.has_field?(name) ⇒ Boolean
34 35 36 |
# File 'lib/ruby_json_api_client/base.rb', line 34 def self.has_field?(name) fields.include?(name) end |
.has_many(name, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby_json_api_client/base.rb', line 51 def self.has_many(name, = {}) @_has_many_relationships ||= [] @_has_many_relationships << name define_method(name) do @_loaded_has_manys ||= {} if @_loaded_has_manys[name].nil? result = RubyJsonApiClient::Store .instance .find_many_relationship(self, name, ) @_loaded_has_manys[name] = result end @_loaded_has_manys[name] end end |
.has_many_relationships ⇒ Object
70 71 72 |
# File 'lib/ruby_json_api_client/base.rb', line 70 def self.has_many_relationships @_has_many_relationships end |
.has_one(name, options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ruby_json_api_client/base.rb', line 74 def self.has_one(name, = {}) define_method(name) do @_loaded_has_ones ||= {} if @_loaded_has_ones[name].nil? result = RubyJsonApiClient::Store .instance .find_single_relationship(self, name, ) @_loaded_has_ones[name] = result end @_loaded_has_ones[name] end define_method("#{name}=".to_sym) do || @_loaded_has_ones ||= {} @_loaded_has_ones[name] = end define_method("#{name}_id=".to_sym) do || klass_name = [:class_name] || ActiveSupport::Inflector.classify(name) klass = ActiveSupport::Inflector.constantize(klass_name) @_loaded_has_ones ||= {} @_loaded_has_ones[name] = klass.new(id: ) end end |
.identifier(name) ⇒ Object
38 39 40 41 |
# File 'lib/ruby_json_api_client/base.rb', line 38 def self.identifier(name) @_identifier = name field(name, :number) end |
.where(params) ⇒ Object
114 115 116 |
# File 'lib/ruby_json_api_client/base.rb', line 114 def self.where(params) RubyJsonApiClient::Store.instance.query(self, params) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?, equal?
145 146 147 148 149 150 |
# File 'lib/ruby_json_api_client/base.rb', line 145 def ==(other) klass_match = (self.class == other.class) ids_match = (send(self.class._identifier) == other.send(other.class._identifier)) klass_match && ids_match end |
#attributes ⇒ Object
30 31 32 |
# File 'lib/ruby_json_api_client/base.rb', line 30 def attributes @attrs ||= self.class.attributes end |
#destroy ⇒ Object
141 142 143 |
# File 'lib/ruby_json_api_client/base.rb', line 141 def destroy RubyJsonApiClient::Store.instance.delete(self) end |
#loaded_has_ones ⇒ Object
102 103 104 |
# File 'lib/ruby_json_api_client/base.rb', line 102 def loaded_has_ones @_loaded_has_ones || {} end |
#persisted? ⇒ Boolean
122 123 124 |
# File 'lib/ruby_json_api_client/base.rb', line 122 def persisted? !!send(self.class._identifier) end |
#reload ⇒ Object
126 127 128 |
# File 'lib/ruby_json_api_client/base.rb', line 126 def reload RubyJsonApiClient::Store.instance.reload(self) end |
#save ⇒ Object
130 131 132 |
# File 'lib/ruby_json_api_client/base.rb', line 130 def save RubyJsonApiClient::Store.instance.save(self) end |
#update_attributes(data) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/ruby_json_api_client/base.rb', line 134 def update_attributes(data) data.each do |(key, value)| send("#{key}=", value) end save end |