Class: Economic::Entity
- Inherits:
-
Object
- Object
- Economic::Entity
- Defined in:
- lib/economic/entity.rb,
lib/economic/entity/handle.rb
Direct Known Subclasses
Account, CashBook, CashBookEntry, Company, Creditor, CreditorContact, CreditorEntry, CurrentInvoice, CurrentInvoiceLine, Debtor, DebtorContact, DebtorEntry, Entry, Invoice, Order, OrderLine, Product
Defined Under Namespace
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the id of Entity.
-
#number ⇒ Object
readonly
Returns the number of Entity.
-
#partial ⇒ Object
Internal accessors.
-
#persisted ⇒ Object
Internal accessors.
-
#session ⇒ Object
Internal accessors.
Class Method Summary collapse
-
.default_values ⇒ Object
Returns the default values for properties.
-
.defaults(default_values) ⇒ Object
Sets default property values that an entity should be initialized with.
-
.handle_writer(property) ⇒ Object
Create a setter method for property that converts its input to a Handle.
- .has_properties(*properties) ⇒ Object
-
.key ⇒ Object
Returns a symbol based on the name of the entity.
- .properties ⇒ Object
- .properties_not_triggering_full_load ⇒ Object
-
.property_reader(property) ⇒ Object
Create a property getter that loads the full Entity from the API if necessary.
-
.property_writer(property) ⇒ Object
Create a property setter for property.
-
.proxy ⇒ Object
Returns the class used to instantiate a proxy for Entity.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#destroy ⇒ Object
Deletes entity permanently from E-conomic.
-
#get ⇒ Object
Get default Entity with its number from the API.
-
#get_data ⇒ Object
Updates Entity with its data from the API.
- #handle ⇒ Object
- #handle=(handle) ⇒ Object
-
#initialize(properties = {}) ⇒ Entity
constructor
A new instance of Entity.
- #inspect ⇒ Object
-
#partial? ⇒ Boolean
Returns true if Entity has not been fully loaded from API yet.
-
#persisted? ⇒ Boolean
Returns true if CurrentInvoiceLine has been persisted in e-conomic.
-
#proxy ⇒ Object
Returns a proxy for entities of the current class.
-
#save ⇒ Object
Persist the Entity to the API.
-
#update_properties(hash) ⇒ Object
Updates properties of Entity with the values from hash.
Constructor Details
#initialize(properties = {}) ⇒ Entity
Returns a new instance of Entity.
103 104 105 106 107 108 |
# File 'lib/economic/entity.rb', line 103 def initialize(properties = {}) initialize_defaults update_properties(properties) @persisted = false @partial = true end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the id of Entity. This does not trigger a load from the API even if Entity is partial
129 130 131 |
# File 'lib/economic/entity.rb', line 129 def id @id end |
#number ⇒ Object (readonly)
Returns the number of Entity. This does not trigger a load from the API even if Entity is partial
125 126 127 |
# File 'lib/economic/entity.rb', line 125 def number @number end |
#partial ⇒ Object
Internal accessors
10 11 12 |
# File 'lib/economic/entity.rb', line 10 def partial @partial end |
#persisted ⇒ Object
Internal accessors
10 11 12 |
# File 'lib/economic/entity.rb', line 10 def persisted @persisted end |
#session ⇒ Object
Internal accessors
10 11 12 |
# File 'lib/economic/entity.rb', line 10 def session @session end |
Class Method Details
.default_values ⇒ Object
Returns the default values for properties
19 20 21 |
# File 'lib/economic/entity.rb', line 19 def default_values @default_values || {} end |
.defaults(default_values) ⇒ Object
Sets default property values that an entity should be initialized with
14 15 16 |
# File 'lib/economic/entity.rb', line 14 def defaults(default_values) @default_values = default_values end |
.handle_writer(property) ⇒ Object
Create a setter method for property that converts its input to a Handle
24 25 26 27 28 29 |
# File 'lib/economic/entity.rb', line 24 def handle_writer(property) define_method "#{property}=" do |value| value = Economic::Entity::Handle.new(value) if value instance_variable_set("@#{property}", value) end end |
.has_properties(*properties) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/economic/entity.rb', line 58 def has_properties(*properties) @properties = properties properties.each do |property| # Create a getter for property unless properties_not_triggering_full_load.include?(property) property_reader property end # Create a setter for property property_writer property end end |
.key ⇒ Object
Returns a symbol based on the name of the entity. Used to request and read data responses.
Entity.key #=> :entity
CurrentInvoice.key #=> :current_invoice
87 88 89 90 91 92 |
# File 'lib/economic/entity.rb', line 87 def key key = name key = Economic::Support::String.demodulize(key) key = Economic::Support::String.underscore(key) key.intern end |
.properties ⇒ Object
71 72 73 |
# File 'lib/economic/entity.rb', line 71 def properties @properties || [] end |
.properties_not_triggering_full_load ⇒ Object
31 32 33 |
# File 'lib/economic/entity.rb', line 31 def properties_not_triggering_full_load [:id, :number, :handle] end |
.property_reader(property) ⇒ Object
Create a property getter that loads the full Entity from the API if necessary
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/economic/entity.rb', line 37 def property_reader(property) define_method property.to_s do value = instance_variable_get("@#{property}") if value.nil? && partial? && persisted? instance_variable_get("@#{property}") else value end end end |
.property_writer(property) ⇒ Object
Create a property setter for property
49 50 51 52 53 54 55 56 |
# File 'lib/economic/entity.rb', line 49 def property_writer(property) if property.to_s.end_with?("_handle") handle_writer property else # Just use regular writers attr_writer property end end |
.proxy ⇒ Object
Returns the class used to instantiate a proxy for Entity
76 77 78 79 80 |
# File 'lib/economic/entity.rb', line 76 def proxy class_name = name.split("::").last proxy_class_name = "#{class_name}Proxy" Economic.const_get(proxy_class_name) end |
Instance Method Details
#==(other) ⇒ Object
180 181 182 183 |
# File 'lib/economic/entity.rb', line 180 def ==(other) return false if other.nil? handle == other.handle && other.is_a?(self.class) end |
#destroy ⇒ Object
Deletes entity permanently from E-conomic.
160 161 162 163 164 165 166 167 168 |
# File 'lib/economic/entity.rb', line 160 def destroy handleKey = "#{Support::String.camel_back(class_name)}Handle" response = request(:delete, handleKey => handle.to_hash) @persisted = false @partial = true response end |
#get ⇒ Object
Get default Entity with its number from the API
119 120 121 |
# File 'lib/economic/entity.rb', line 119 def get proxy.get end |
#get_data ⇒ Object
Updates Entity with its data from the API
111 112 113 114 115 116 |
# File 'lib/economic/entity.rb', line 111 def get_data response = proxy.get_data(handle) update_properties(response) self.partial = false self.persisted = true end |
#handle ⇒ Object
95 96 97 |
# File 'lib/economic/entity.rb', line 95 def handle @handle || Handle.build(:number => @number, :id => @id) end |
#handle=(handle) ⇒ Object
99 100 101 |
# File 'lib/economic/entity.rb', line 99 def handle=(handle) @handle = Handle.build(handle) end |
#inspect ⇒ Object
149 150 151 152 |
# File 'lib/economic/entity.rb', line 149 def inspect props = self.class.properties.collect { |p| "#{p}=#{send(p).inspect}" } "#<#{self.class}:#{object_id} partial=#{partial?}, persisted=#{persisted?}, #{props.join(', ')}>" end |
#partial? ⇒ Boolean
Returns true if Entity has not been fully loaded from API yet
137 138 139 140 |
# File 'lib/economic/entity.rb', line 137 def partial? # TODO: Can this be introspected somehow? !!@partial end |
#persisted? ⇒ Boolean
Returns true if CurrentInvoiceLine has been persisted in e-conomic
132 133 134 |
# File 'lib/economic/entity.rb', line 132 def persisted? !!@persisted end |
#proxy ⇒ Object
Returns a proxy for entities of the current class. For example if called on an Economic::Debtor it returns an instance of Economic::DebtorProxy with the Debtors session as owner.
145 146 147 |
# File 'lib/economic/entity.rb', line 145 def proxy self.class.proxy.new(session) end |
#save ⇒ Object
Persist the Entity to the API
155 156 157 |
# File 'lib/economic/entity.rb', line 155 def save create_or_update end |
#update_properties(hash) ⇒ Object
Updates properties of Entity with the values from hash
171 172 173 174 175 176 177 178 |
# File 'lib/economic/entity.rb', line 171 def update_properties(hash) hash.each do |key, value| setter_method = "#{key}=" if respond_to?(setter_method) send(setter_method, value) end end end |