class Economic::EntityProxy

Attributes

items[R]
owner[R]

Public Class Methods

entity_class() click to toggle source

Returns the class this Proxy is a proxy for

# File lib/economic/proxies/entity_proxy.rb, line 9
def entity_class
  Economic.const_get(entity_class_name)
end
entity_class_name() click to toggle source
# File lib/economic/proxies/entity_proxy.rb, line 13
def entity_class_name
  proxy_class_name = name.split("::").last
  proxy_class_name.sub(/Proxy$/, "")
end
new(owner) click to toggle source
# File lib/economic/proxies/entity_proxy.rb, line 26
def initialize(owner)
  @owner = owner
  initialize_items
end

Public Instance Methods

<<(item)
Alias for: append
all() click to toggle source

Fetches all entities from the API.

# File lib/economic/proxies/entity_proxy.rb, line 36
def all
  response = request(:get_all)
  handles = response.values.flatten.collect { |handle| Entity::Handle.build(handle) }
  get_data_for_handles(handles)

  self
end
append(item) click to toggle source

Adds item to proxy unless item already exists in the proxy

# File lib/economic/proxies/entity_proxy.rb, line 85
def append(item)
  items << item unless items.include?(item)
end
Also aliased as: <<
build(properties = {}) click to toggle source

Returns a new, unpersisted Economic::Entity that has been added to Proxy

# File lib/economic/proxies/entity_proxy.rb, line 45
def build(properties = {})
  entity = self.class.entity_class.new(:session => session)

  entity.update_properties(properties)
  entity.partial = false

  append(entity)
  initialize_properties_with_values_from_owner(entity)

  entity
end
entity_class() click to toggle source

Returns the class this proxy manages

# File lib/economic/proxies/entity_proxy.rb, line 58
def entity_class
  self.class.entity_class
end
entity_class_name() click to toggle source

Returns the name of the class this proxy manages

# File lib/economic/proxies/entity_proxy.rb, line 63
def entity_class_name
  self.class.entity_class_name
end
find(handle) click to toggle source

Fetches Entity data from API and returns an Entity initialized with that data added to Proxy

# File lib/economic/proxies/entity_proxy.rb, line 69
def find(handle)
  handle = build_handle(handle)
  entity_hash = get_data(handle)
  entity = build(entity_hash)
  entity.persisted = true
  entity
end
get_data(handle) click to toggle source

Gets data for Entity from the API. Returns Hash with the response data

# File lib/economic/proxies/entity_proxy.rb, line 78
def get_data(handle)
  handle = Entity::Handle.new(handle)
  entity_hash = request(:get_data, "entityHandle" => handle.to_hash)
  entity_hash
end
session() click to toggle source
# File lib/economic/proxies/entity_proxy.rb, line 31
def session
  owner.session
end

Protected Instance Methods

build_handle(id_or_hash) click to toggle source

Wraps an Entity::Handle around a potential id_or_hash object as received from the backend.

If id_or_hash is a numeric value it'll be assumed to be an id and used as such in the returned Entity::Handle.

If id_or_hash is a Hash, it's values will be used as the keys and values of the built Entity::Handle.

# File lib/economic/proxies/entity_proxy.rb, line 102
def build_handle(id_or_hash)
  if id_or_hash.respond_to?(:to_i)
    Entity::Handle.new(:id => id_or_hash)
  else
    Entity::Handle.new(id_or_hash)
  end
end
get_data_array(handles) click to toggle source

Fetches all data for the given handles. Returns Array with hashes of entity data

# File lib/economic/proxies/entity_proxy.rb, line 112
def get_data_array(handles)
  return [] unless handles && handles.any?

  entity_class_name_for_soap_request = entity_class.name.split("::").last
  response = request(:get_data_array, "entityHandles" => {"#{entity_class_name_for_soap_request}Handle" => handles.collect(&:to_hash)})
  [response["#{entity_class.key}_data".intern]].flatten
end
get_data_for_handles(handles) click to toggle source
# File lib/economic/proxies/entity_proxy.rb, line 120
def get_data_for_handles(handles)
  if handles.size == 1
    # Fetch data for single entity
    find(handles.first.to_hash)
  elsif handles.size > 1
    # Fetch all data for all the entities
    entity_data = get_data_array(handles)

    # Build Entity objects and add them to the proxy
    entity_data.each do |data|
      entity = build(data)
      entity.persisted = true
    end
  end
end
initialize_items() click to toggle source

Removes all loaded items

# File lib/economic/proxies/entity_proxy.rb, line 137
def initialize_items
  @items = []
end
initialize_properties_with_values_from_owner(entity) click to toggle source

Initialize properties of entity with values from owner. Returns entity

# File lib/economic/proxies/entity_proxy.rb, line 142
def initialize_properties_with_values_from_owner(entity)
  entity
end
request(action, data = nil) click to toggle source

Requests an action from the API endpoint

# File lib/economic/proxies/entity_proxy.rb, line 147
def request(action, data = nil)
  session.request(
    Endpoint.new.soap_action_name(entity_class, action),
    data
  )
end