Class: LogicalModel
- Inherits:
-
Object
- Object
- LogicalModel
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::MassAssignmentSecurity, ActiveModel::Serializers::JSON, ActiveModel::Validations, ApiKey, Attributes, BelongsTo, HasManyKeys, 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/belongs_to.rb,
lib/logical_model/url_helper.rb,
lib/logical_model/rest_actions.rb,
lib/logical_model/has_many_keys.rb,
lib/logical_model/responses_configuration.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, Attributes, BelongsTo, HasManyKeys, 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 BelongsTo
Methods included from HasManyKeys
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.
82 83 84 |
# File 'lib/logical_model.rb', line 82 def initialize(attributes={}) self.attributes = attributes end |
Class Attribute Details
.json_root ⇒ Object
Returns the value of attribute json_root.
87 88 89 |
# File 'lib/logical_model.rb', line 87 def json_root @json_root end |
.retries ⇒ Object
Returns the value of attribute retries.
87 88 89 |
# File 'lib/logical_model.rb', line 87 def retries @retries end |
.timeout ⇒ Object
Returns the value of attribute timeout.
87 88 89 |
# File 'lib/logical_model.rb', line 87 def timeout @timeout end |
Instance Attribute Details
#last_response_code ⇒ Object
Returns the value of attribute last_response_code.
77 78 79 |
# File 'lib/logical_model.rb', line 77 def last_response_code @last_response_code end |
Class Method Details
.delete_multiple_enabled? ⇒ Boolean
92 |
# File 'lib/logical_model.rb', line 92 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.
441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/logical_model/rest_actions.rb', line 441 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
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/logical_model.rb', line 94 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.full_messages.to_sentence}") end end end end end |
Instance Method Details
#json_root ⇒ Object
110 111 112 |
# File 'lib/logical_model.rb', line 110 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?
122 123 124 |
# File 'lib/logical_model.rb', line 122 def new_record? !self.persisted? end |
#persisted? ⇒ Boolean
114 115 116 |
# File 'lib/logical_model.rb', line 114 def persisted? false end |