Class: RubyJsonApiClient::Base
- Inherits:
-
Object
- Object
- RubyJsonApiClient::Base
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Model
- 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
- .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
Instance Attribute Details
#__origin__ ⇒ Object
Returns the value of attribute __origin__.
31 32 33 |
# File 'lib/ruby_json_api_client/base.rb', line 31 def __origin__ @__origin__ end |
#meta ⇒ Object
Returns the value of attribute meta.
30 31 32 |
# File 'lib/ruby_json_api_client/base.rb', line 30 def end |
Class Method Details
._identifier ⇒ Object
25 26 27 |
# File 'lib/ruby_json_api_client/base.rb', line 25 def self._identifier @_identifier || superclass._identifier end |
.all ⇒ Object
77 78 79 |
# File 'lib/ruby_json_api_client/base.rb', line 77 def self.all where({}) end |
.field(name, type = :string) ⇒ Object
6 7 8 9 10 |
# File 'lib/ruby_json_api_client/base.rb', line 6 def self.field(name, type = :string) @_fields ||= [] @_fields << name attr_accessor name end |
.fields ⇒ Object
12 13 14 |
# File 'lib/ruby_json_api_client/base.rb', line 12 def self.fields @_fields || [] end |
.find(id) ⇒ Object
73 74 75 |
# File 'lib/ruby_json_api_client/base.rb', line 73 def self.find(id) RubyJsonApiClient::Store.instance.find(self, id) end |
.has_field?(name) ⇒ Boolean
16 17 18 |
# File 'lib/ruby_json_api_client/base.rb', line 16 def self.has_field?(name) (@_fields | [_identifier]).include?(name) end |
.has_many(name, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_json_api_client/base.rb', line 33 def self.has_many(name, = {}) @_has_many_relationships ||= [] @_has_many_relationships << name define_method(name) do # make cachable RubyJsonApiClient::Store .instance .find_many_relationship(self, name, ) end end |
.has_many_relationships ⇒ Object
44 45 46 |
# File 'lib/ruby_json_api_client/base.rb', line 44 def self.has_many_relationships @_has_many_relationships end |
.has_one(name, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_json_api_client/base.rb', line 48 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 end |
.identifier(name) ⇒ Object
20 21 22 23 |
# File 'lib/ruby_json_api_client/base.rb', line 20 def self.identifier(name) @_identifier = name field(name, :number) end |
.where(params) ⇒ Object
81 82 83 |
# File 'lib/ruby_json_api_client/base.rb', line 81 def self.where(params) RubyJsonApiClient::Store.instance.query(self, params) end |
Instance Method Details
#links ⇒ Object
93 94 95 |
# File 'lib/ruby_json_api_client/base.rb', line 93 def links store.find(self.class._identifier).__data__ end |
#loaded_has_ones ⇒ Object
69 70 71 |
# File 'lib/ruby_json_api_client/base.rb', line 69 def loaded_has_ones @_loaded_has_ones || {} end |
#persisted? ⇒ Boolean
85 86 87 |
# File 'lib/ruby_json_api_client/base.rb', line 85 def persisted? !!send(self.class._identifier) end |
#reload ⇒ Object
89 90 91 |
# File 'lib/ruby_json_api_client/base.rb', line 89 def reload RubyJsonApiClient::Store.instance.reload(self) end |