Class: LogicalModel
- Inherits:
-
Object
- Object
- LogicalModel
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::MassAssignmentSecurity, ActiveModel::Serializers::JSON, ActiveModel::Validations, ApiKey, Associations, Attributes, Hydra, RESTActions, ResponsesConfiguration, SafeLog, UrlHelper
- Defined in:
- lib/logical_model.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, 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.
- #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.
85 86 87 |
# File 'lib/logical_model.rb', line 85 def json_root @json_root end |
.retries ⇒ Object
Returns the value of attribute retries.
85 86 87 |
# File 'lib/logical_model.rb', line 85 def retries @retries end |
.timeout ⇒ Object
Returns the value of attribute timeout.
85 86 87 |
# File 'lib/logical_model.rb', line 85 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
90 |
# File 'lib/logical_model.rb', line 90 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.
452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/logical_model/rest_actions.rb', line 452 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
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/logical_model.rb', line 92 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
#json_root ⇒ Object
108 109 110 |
# File 'lib/logical_model.rb', line 108 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?
120 121 122 |
# File 'lib/logical_model.rb', line 120 def new_record? !self.persisted? end |
#persisted? ⇒ Boolean
112 113 114 |
# File 'lib/logical_model.rb', line 112 def persisted? false end |