lib/billomat/models/base.rb in billomat-0.2.0 vs lib/billomat/models/base.rb in billomat-0.3.0
- old
+ new
@@ -2,54 +2,53 @@
require 'ostruct'
module Billomat
module Models
- ##
# This class is the base for all other models (resources).
# It handles the communication with the gateway to talk to the API.
class Base
attr_accessor :data
- ##
- # Tries to find the resource for the given id
+ # Tries to find the resource for the given id.
#
- # @param [String] id The resource id
- # @return [Billomat::Models::Base, nil] The found resource or nil
+ # @param id [String] the resource id
+ # @return [Billomat::Models::Base, nil] the found resource or nil
def self.find(id)
return nil if id.nil?
+
resp = Billomat::Gateway.new(:get, "#{base_path}/#{id}").run
new(resp[resource_name])
end
- ##
- # Allows to query for a record
+ # Allows to query for a record.
#
- # @param [Hash] hash The query parameters
- # @return [Array<Billomat::Models::Base>] The found records
+ # @param hash [Hash] the query parameters
+ # @return [Array<Billomat::Models::Base>] the found records
def self.where(hash = {})
Billomat::Search.new(self, hash).run
end
##
- # Initializes a new model
+ # Initializes a new model.
#
- # @param [Hash] data The attributes of the object
- # @return [Billomat::Models::Base] The record as an object
+ # @param data [Hash] the attributes of the object
+ # @return [Billomat::Models::Base] the record as an object
def initialize(data = {})
@data = OpenStruct.new(data)
end
- ##
# Persists the current object in the API.
# When record is new it calls create, otherwise it saves the object.
#
# @return [TrueClass]
def save
return create if id.nil?
+
update
end
+ alias save! save
# @return [TrueClass]
def create
resp = Billomat::Gateway.new(
:post, self.class.base_path, wrapped_data
@@ -57,72 +56,72 @@
@data = OpenStruct.new(resp[self.class.resource_name])
true
end
+ alias create! create
# @return [TrueClass]
def update
path = "#{self.class.base_path}/#{id}"
resp = Billomat::Gateway.new(:put, path, wrapped_data).run
@data = resp[self.class.resource_name]
true
end
+ alias update! update
# @return [TrueClass]
def delete
path = "#{self.class.base_path}/#{id}"
Billomat::Gateway.new(:delete, path).run
true
end
+ alias delete! delete
- # @return [String, nil] The object's ID
+ # @return [String, nil] the object's ID
def id
@data['id'] || nil
end
- ##
- # Wraps the data so the API accepts the request
+ # Wraps the data so the API accepts the request.
#
# @example
# some_invoice.wrapped_data
# #=> { "invoice" => { "id" => "12345" } }
#
- # @return [Hash] The wrapped data
+ # @return [Hash] the wrapped data
def wrapped_data
{ self.class.resource_name => @data.to_h }
end
- ##
- # Returns the object with the right JSON structure
+ # Returns the object with the right JSON structure.
#
- # @return [Hash] The objects data
+ # @return [Hash] the objects data
def as_json(_options = nil)
@data.to_h
end
- ##
- # All values in the @data hash can be accessed like a 'normal' method
+ # All values in the @data hash can be accessed like a 'normal' method.
#
# @example
# invoice = Billomat::Models::Invoice.new(invoice_number: '123')
# invoice.invoice_number
# #=> '123'
def method_missing(method, *args, &block)
- return @data[method] if @data.to_h.keys.include?(method)
+ return @data[method] if @data.to_h.key?(method)
+
super
end
- ##
- # Necessary for method_missing
+ # Necessary for method_missing.
#
# @param [Symbol] method The method name
# @param [TrueClass, FalseClass] include_privat
# @return [TrueClass, FalseClass]
def respond_to_missing?(method, include_privat = false)
- @data.to_h.keys.include?(method.to_s) || super
+ @data.to_h.key?(method.to_s) || super
end
end
end
end