Class: Lurch::Store
- Inherits:
-
Object
- Object
- Lurch::Store
- Defined in:
- lib/lurch/store.rb
Instance Method Summary collapse
-
#add_related(resource, relationship_key, related_resources) ⇒ Object
add resource(s) to a has many relationship.
- #delete(resource, query = {}) ⇒ Object
- #from(type) ⇒ Object (also: #to)
-
#initialize(url, options = {}) ⇒ Store
constructor
A new instance of Store.
- #insert(changeset, query = {}) ⇒ Object
- #load_from_url(url) ⇒ Object
- #peek(type, id) ⇒ Object
- #query ⇒ Object
-
#remove_related(resource, relationship_key, related_resources) ⇒ Object
remove resource(s) from a has many relationship.
- #resource_from_store(type, id) ⇒ Object
- #save(changeset, query = {}) ⇒ Object
-
#update_related(resource, relationship_key, related_resources) ⇒ Object
replace resource(s) for a has many or has one relationship.
Constructor Details
#initialize(url, options = {}) ⇒ Store
Returns a new instance of Store.
3 4 5 6 7 |
# File 'lib/lurch/store.rb', line 3 def initialize(url, = {}) @config = StoreConfiguration.new() @client = Client.new(url, @config) @store = Hash.new { |hash, key| hash[key] = {} } end |
Instance Method Details
#add_related(resource, relationship_key, related_resources) ⇒ Object
add resource(s) to a has many relationship
51 52 53 |
# File 'lib/lurch/store.rb', line 51 def (resource, relationship_key, ) modify_relationship(:post, resource, relationship_key, ) end |
#delete(resource, query = {}) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/lurch/store.rb', line 42 def delete(resource, query = {}) url = uri_builder.resource_uri(resource.type, resource.id, query) @client.delete(url) remove(resource) true end |
#from(type) ⇒ Object Also known as: to
9 10 11 |
# File 'lib/lurch/store.rb', line 9 def from(type) query.type(type) end |
#insert(changeset, query = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lurch/store.rb', line 31 def insert(changeset, query = {}) return save(changeset) unless changeset.id.nil? url = uri_builder.resources_uri(changeset.type, query) document = @client.post(url, payload_builder.build(changeset)) process_document(document) rescue Errors::JSONApiError => err changeset.errors = err.errors raise err end |
#load_from_url(url) ⇒ Object
66 67 68 69 |
# File 'lib/lurch/store.rb', line 66 def load_from_url(url) document = @client.get(url) process_document(document) end |
#peek(type, id) ⇒ Object
14 15 16 17 18 |
# File 'lib/lurch/store.rb', line 14 def peek(type, id) stored_resource = resource_from_store(type, id.to_s) return nil if stored_resource.nil? Resource.new(self, stored_resource.type, stored_resource.id) end |
#query ⇒ Object
78 79 80 |
# File 'lib/lurch/store.rb', line 78 def query Query.new(self, inflector) end |
#remove_related(resource, relationship_key, related_resources) ⇒ Object
remove resource(s) from a has many relationship
56 57 58 |
# File 'lib/lurch/store.rb', line 56 def (resource, relationship_key, ) modify_relationship(:delete, resource, relationship_key, ) end |
#resource_from_store(type, id) ⇒ Object
72 73 74 75 |
# File 'lib/lurch/store.rb', line 72 def resource_from_store(type, id) normalized_type = Inflector.decode_type(type) @store[normalized_type][id] end |
#save(changeset, query = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lurch/store.rb', line 20 def save(changeset, query = {}) return insert(changeset) if changeset.id.nil? url = uri_builder.resource_uri(changeset.type, changeset.id, query) document = @client.patch(url, payload_builder.build(changeset)) process_document(document) rescue Errors::JSONApiError => err changeset.errors = err.errors raise err end |
#update_related(resource, relationship_key, related_resources) ⇒ Object
replace resource(s) for a has many or has one relationship
61 62 63 |
# File 'lib/lurch/store.rb', line 61 def (resource, relationship_key, ) modify_relationship(:patch, resource, relationship_key, ) end |