Class: Economic::EntityProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/economic/proxies/entity_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ EntityProxy

Returns a new instance of EntityProxy.



26
27
28
29
# File 'lib/economic/proxies/entity_proxy.rb', line 26

def initialize(owner)
  @owner = owner
  initialize_items
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



24
25
26
# File 'lib/economic/proxies/entity_proxy.rb', line 24

def owner
  @owner
end

Class Method Details

.entity_classObject

Returns the class this Proxy is a proxy for



9
10
11
# File 'lib/economic/proxies/entity_proxy.rb', line 9

def entity_class
  Economic.const_get(entity_class_name)
end

.entity_class_nameObject



13
14
15
16
# 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

Instance Method Details

#allObject

Fetches all entities from the API.



36
37
38
39
40
41
42
# 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) ⇒ Object Also known as: <<

Adds item to proxy unless item already exists in the proxy



85
86
87
# File 'lib/economic/proxies/entity_proxy.rb', line 85

def append(item)
  items << item unless items.include?(item)
end

#build(properties = {}) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
# 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_classObject

Returns the class this proxy manages



58
59
60
# File 'lib/economic/proxies/entity_proxy.rb', line 58

def entity_class
  self.class.entity_class
end

#entity_class_nameObject

Returns the name of the class this proxy manages



63
64
65
# File 'lib/economic/proxies/entity_proxy.rb', line 63

def entity_class_name
  self.class.entity_class_name
end

#find(handle) ⇒ Object

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



69
70
71
72
73
74
75
# 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) ⇒ Object

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



78
79
80
81
82
# 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

#sessionObject



31
32
33
# File 'lib/economic/proxies/entity_proxy.rb', line 31

def session
  owner.session
end