Class: RubyJsonApiClient::Base
- Inherits:
-
Object
- Object
- RubyJsonApiClient::Base
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::SerializerSupport, ActiveModel::Validations
- 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
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
- #loaded_has_ones ⇒ Object
- #persisted? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #update_attributes(data) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
18 19 20 21 22 23 24 |
# File 'lib/ruby_json_api_client/base.rb', line 18 def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params super() end |
Instance Attribute Details
#__origin__ ⇒ Object
Returns the value of attribute __origin__.
61 62 63 |
# File 'lib/ruby_json_api_client/base.rb', line 61 def __origin__ @__origin__ end |
#meta ⇒ Object
Returns the value of attribute meta.
60 61 62 |
# File 'lib/ruby_json_api_client/base.rb', line 60 def @meta end |
Class Method Details
._identifier ⇒ Object
55 56 57 |
# File 'lib/ruby_json_api_client/base.rb', line 55 def self._identifier @_identifier || superclass._identifier end |
.all ⇒ Object
122 123 124 |
# File 'lib/ruby_json_api_client/base.rb', line 122 def self.all where({}) end |
.attributes ⇒ Object
35 36 37 38 39 40 |
# File 'lib/ruby_json_api_client/base.rb', line 35 def self.attributes fields.reduce({}) do |attributes, field| attributes[field] = nil attributes end end |
.create(params) ⇒ Object
130 131 132 |
# File 'lib/ruby_json_api_client/base.rb', line 130 def self.create(params) new(params).tap(&:save) end |
.field(name, type = :string) ⇒ Object
26 27 28 29 |
# File 'lib/ruby_json_api_client/base.rb', line 26 def self.field(name, type = :string) fields << name attr_accessor name end |
.fields ⇒ Object
31 32 33 |
# File 'lib/ruby_json_api_client/base.rb', line 31 def self.fields @_fields ||= Set.new [_identifier] end |
.find(id) ⇒ Object
118 119 120 |
# File 'lib/ruby_json_api_client/base.rb', line 118 def self.find(id) RubyJsonApiClient::Store.instance.find(self, id) end |
.has_field?(name) ⇒ Boolean
46 47 48 |
# File 'lib/ruby_json_api_client/base.rb', line 46 def self.has_field?(name) fields.include?(name) end |
.has_many(name, options = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby_json_api_client/base.rb', line 63 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
82 83 84 |
# File 'lib/ruby_json_api_client/base.rb', line 82 def self.has_many_relationships @_has_many_relationships end |
.has_one(name, options = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ruby_json_api_client/base.rb', line 86 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
50 51 52 53 |
# File 'lib/ruby_json_api_client/base.rb', line 50 def self.identifier(name) @_identifier = name field(name, :number) end |
.where(params) ⇒ Object
126 127 128 |
# File 'lib/ruby_json_api_client/base.rb', line 126 def self.where(params) RubyJsonApiClient::Store.instance.query(self, params) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?, equal?
157 158 159 160 161 162 |
# File 'lib/ruby_json_api_client/base.rb', line 157 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
42 43 44 |
# File 'lib/ruby_json_api_client/base.rb', line 42 def attributes @attrs ||= self.class.attributes end |
#destroy ⇒ Object
153 154 155 |
# File 'lib/ruby_json_api_client/base.rb', line 153 def destroy RubyJsonApiClient::Store.instance.delete(self) end |
#loaded_has_ones ⇒ Object
114 115 116 |
# File 'lib/ruby_json_api_client/base.rb', line 114 def loaded_has_ones @_loaded_has_ones || {} end |
#persisted? ⇒ Boolean
134 135 136 |
# File 'lib/ruby_json_api_client/base.rb', line 134 def persisted? !!send(self.class._identifier) end |
#reload ⇒ Object
138 139 140 |
# File 'lib/ruby_json_api_client/base.rb', line 138 def reload RubyJsonApiClient::Store.instance.reload(self) end |
#save ⇒ Object
142 143 144 |
# File 'lib/ruby_json_api_client/base.rb', line 142 def save RubyJsonApiClient::Store.instance.save(self) end |
#update_attributes(data) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/ruby_json_api_client/base.rb', line 146 def update_attributes(data) data.each do |(key, value)| send("#{key}=", value) end save end |