Class: LogicalModel
- Inherits:
-
Object
- Object
- LogicalModel
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Serializers::JSON, ActiveModel::Validations, ApiKey, Associations, Attributes, Hydra, RESTActions, ResponsesConfiguration, SafeLog, UrlHelper
- Defined in:
- lib/logical_model.rb,
lib/logical_model/cache.rb,
lib/logical_model/hydra.rb,
lib/logical_model/api_key.rb,
lib/logical_model/safe_log.rb,
lib/logical_model/attributes.rb,
lib/logical_model/url_helper.rb,
lib/logical_model/associations.rb,
lib/logical_model/rest_actions.rb,
lib/logical_model/associations/belongs_to.rb,
lib/logical_model/responses_configuration.rb,
lib/logical_model/associations/has_many_keys.rb
Overview
Logical Model, not persistant on DB, works through API. (replaces ActiveResource)
Configuration attributes:
host: Host of the WS. eg: "localhost:3000"
resource_path: Path of this resources. eg: "/api/resources"
attribute_keys: Attributes. eg: [:id, :attr_a, :attr_b]
use_ssl: will use https if true, http if false
use_api_key: set to true if api_key is needed to access resource
api_key_name: api key parameter name. eg: app_key
api_key: api_key. eg: "asd32fa4s4pdf35tr"
log_path: Path to log file. Will be ignored if using Rails.
json_root: Used to build parameters. Default: class name underscored
You may use validations such as validates_presence_of, etc.
Usage:
class RemoteResource < LogicalModel
set_resource_url "remote.server", "/api/remote/path"
attribute :id
attribute :attribute_a
attribute :attribute_b
validates_presence_of :id
end
This enables:
RemoteResource.new(params[:remote_resource])
RemoteResource#create
RemoteResource.find(params[:id])
RemoteResource.paginate
RemoteResource#update(params[:remote_resouce])
RemoteResource.delete(params[:id])
RemoteResource#destroy
Defined Under Namespace
Modules: ApiKey, Associations, Attributes, Cache, Hydra, RESTActions, ResponsesConfiguration, SafeLog, UrlHelper
Constant Summary collapse
- DEFAULT_TIMEOUT =
10000
- DEFAULT_RETRIES =
3
Class Attribute Summary collapse
-
.json_root ⇒ Object
Returns the value of attribute json_root.
-
.retries ⇒ Object
Returns the value of attribute retries.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
Instance Attribute Summary collapse
-
#last_response_code ⇒ Object
Returns the value of attribute last_response_code.
Class Method Summary collapse
- .delete_multiple_enabled? ⇒ Boolean
-
.from_json(json_string) ⇒ Object
Will parse JSON string and initialize classes for all hashes in json_string.
- .validates_associated(*associations) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ LogicalModel
constructor
A new instance of LogicalModel.
- #initialize_with_callback(attributes = {}) ⇒ Object
- #json_root ⇒ Object
-
#new_record? ⇒ Boolean
Returns true if a record has not been persisted yet.
- #persisted? ⇒ Boolean
Methods included from Associations
Methods included from SafeLog
Methods included from ApiKey
Methods included from UrlHelper
Methods included from RESTActions
Methods included from Attributes
Methods included from ResponsesConfiguration
Methods included from Hydra
Constructor Details
#initialize(attributes = {}) ⇒ LogicalModel
Returns a new instance of LogicalModel.
80 81 82 |
# File 'lib/logical_model.rb', line 80 def initialize(attributes={}) self.attributes = attributes end |
Class Attribute Details
.json_root ⇒ Object
Returns the value of attribute json_root.
92 93 94 |
# File 'lib/logical_model.rb', line 92 def json_root @json_root end |
.retries ⇒ Object
Returns the value of attribute retries.
92 93 94 |
# File 'lib/logical_model.rb', line 92 def retries @retries end |
.timeout ⇒ Object
Returns the value of attribute timeout.
92 93 94 |
# File 'lib/logical_model.rb', line 92 def timeout @timeout end |
Instance Attribute Details
#last_response_code ⇒ Object
Returns the value of attribute last_response_code.
75 76 77 |
# File 'lib/logical_model.rb', line 75 def last_response_code @last_response_code end |
Class Method Details
.delete_multiple_enabled? ⇒ Boolean
97 |
# File 'lib/logical_model.rb', line 97 def delete_multiple_enabled?; @enable_delete_multiple ||= false; end |
.from_json(json_string) ⇒ Object
Will parse JSON string and initialize classes for all hashes in json_string.
425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/logical_model/rest_actions.rb', line 425 def self.from_json(json_string) parsed_response = ActiveSupport::JSON.decode(json_string) parsed_collection = collection_key.nil?? parsed_response : parsed_response[collection_key] collection = parsed_collection.map{|i| self.new(i)} if total_key {collection: collection, total: parsed_response[total_key].to_i} else { collection: collection } end end |
.validates_associated(*associations) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/logical_model.rb', line 99 def validates_associated(*associations) associations.each do |association| validates_each association do |record, attr, value| unless value.collect{ |r| r.nil? || r.valid? }.all? value.reject { |t| t.valid? }.each do |t| record.errors.add("", "#{t.class.name} #{t.errors..to_sentence}") end end end end end |
Instance Method Details
#initialize_with_callback(attributes = {}) ⇒ Object
84 85 86 87 88 |
# File 'lib/logical_model.rb', line 84 def initialize_with_callback(attributes = {}) run_callbacks :initialize do initialize_without_callback(attributes) end end |
#json_root ⇒ Object
115 116 117 |
# File 'lib/logical_model.rb', line 115 def json_root @json_root ||= self.class.to_s.underscore end |
#new_record? ⇒ Boolean
Returns true if a record has not been persisted yet.
Usage: @person.new_record?
127 128 129 |
# File 'lib/logical_model.rb', line 127 def new_record? !self.persisted? end |
#persisted? ⇒ Boolean
119 120 121 |
# File 'lib/logical_model.rb', line 119 def persisted? false end |