Class: LogicalModel

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Class Attribute Details

.json_rootObject

Returns the value of attribute json_root.



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

def json_root
  @json_root
end

.retriesObject

Returns the value of attribute retries.



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

def retries
  @retries
end

.timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Attribute Details

#last_response_codeObject

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

Returns:

  • (Boolean)


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

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



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



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

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 Also known as: initialize



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_rootObject



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

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)


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

def new_record?
  !self.persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  false
end