Class: LogicalModel

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::MassAssignmentSecurity, ActiveModel::Serializers::JSON, ActiveModel::Validations, ApiKey, Associations, Attributes, Cache, 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cache

included

Methods included from Associations

included

Methods included from SafeLog

included

Methods included from ApiKey

included

Methods included from UrlHelper

included

Methods included from RESTActions

included

Methods included from Attributes

included

Methods included from ResponsesConfiguration

included

Methods included from Hydra

included

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_rootObject

Returns the value of attribute json_root.



94
95
96
# File 'lib/logical_model.rb', line 94

def json_root
  @json_root
end

.retriesObject

Returns the value of attribute retries.



94
95
96
# File 'lib/logical_model.rb', line 94

def retries
  @retries
end

.timeoutObject

Returns the value of attribute timeout.



94
95
96
# File 'lib/logical_model.rb', line 94

def timeout
  @timeout
end

Instance Attribute Details

#last_response_codeObject

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

Returns:

  • (Boolean)


99
# File 'lib/logical_model.rb', line 99

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.

Parameters:

  • json_string (JSON String)

    This JSON should have format: […], total: X



456
457
458
459
460
461
462
463
464
465
466
# File 'lib/logical_model/rest_actions.rb', line 456

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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/logical_model.rb', line 101

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

#initialize_with_callback(attributes = {}) ⇒ Object



86
87
88
89
90
# File 'lib/logical_model.rb', line 86

def initialize_with_callback(attributes = {})
  run_callbacks :initialize do
    initialize_without_callback(attributes)
  end
end

#json_rootObject



117
118
119
# File 'lib/logical_model.rb', line 117

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?

Returns:

  • (Boolean)


129
130
131
# File 'lib/logical_model.rb', line 129

def new_record?
  !self.persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/logical_model.rb', line 121

def persisted?
  false
end