class LogicalModel

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

Constants

DEFAULT_RETRIES
DEFAULT_TIMEOUT

Attributes

json_root[RW]
retries[RW]
timeout[RW]
last_response_code[RW]

Public Class Methods

delete_multiple_enabled?() click to toggle source
# File lib/logical_model.rb, line 98
def delete_multiple_enabled?; @enable_delete_multiple ||= false; end
from_json(json_string) click to toggle source

Will parse JSON string and initialize classes for all hashes in json_string.

@param json_string [JSON String] This JSON should have format: {collection: […], total: X}

# File lib/logical_model/rest_actions.rb, line 423
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
new(attributes={}) click to toggle source
# File lib/logical_model.rb, line 80
def initialize(attributes={})
  self.attributes = attributes
end
validates_associated(*associations) click to toggle source
# 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

Public Instance Methods

initialize_with_callback(attributes = {}) click to toggle source
# File lib/logical_model.rb, line 84
def initialize_with_callback(attributes = {})
  run_callbacks :initialize do
    initialize_without_callback(attributes)
  end
end
Also aliased as: initialize
json_root() click to toggle source
# File lib/logical_model.rb, line 116
def json_root
  @json_root ||= self.class.to_s.underscore
end
new_record?() click to toggle source

Returns true if a record has not been persisted yet.

Usage: @person.new_record?

# File lib/logical_model.rb, line 128
def new_record?
  !self.persisted?
end
persisted?() click to toggle source
# File lib/logical_model.rb, line 120
def persisted?
  false
end