class Economic::Entity
Attributes
Internal accessors
Internal accessors
Internal accessors
Public Class Methods
Returns the default values for properties
# File lib/economic/entity.rb, line 19 def default_values @default_values || {} end
Sets default property values that an entity should be initialized with
# File lib/economic/entity.rb, line 14 def defaults(default_values) @default_values = default_values end
Create a setter method for property that converts its input to a Handle
# 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
# 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
Returns a symbol based on the name of the entity. Used to request and read data responses.
Entity.key #=> :entity CurrentInvoice.key #=> :current_invoice
# 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
# File lib/economic/entity.rb, line 103 def initialize(properties = {}) initialize_defaults update_properties(properties) @persisted = false @partial = true end
# File lib/economic/entity.rb, line 71 def properties @properties || [] end
# File lib/economic/entity.rb, line 31 def properties_not_triggering_full_load [:id, :number, :handle] end
Create a property getter that loads the full Entity
from the API if necessary
# 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
Create a property setter for property
# 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
Returns the class used to instantiate a proxy for Entity
# 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
Public Instance Methods
# File lib/economic/entity.rb, line 180 def ==(other) return false if other.nil? handle == other.handle && other.is_a?(self.class) end
Deletes entity permanently from E-conomic.
# 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 default Entity
with its number from the API
# File lib/economic/entity.rb, line 119 def get proxy.get end
Updates Entity
with its data from the API
# 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
# File lib/economic/entity.rb, line 95 def handle @handle || Handle.build(:number => @number, :id => @id) end
# File lib/economic/entity.rb, line 99 def handle=(handle) @handle = Handle.build(handle) end
# 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
Returns true if Entity
has not been fully loaded from API yet
# File lib/economic/entity.rb, line 137 def partial? # TODO: Can this be introspected somehow? !!@partial end
Returns true if CurrentInvoiceLine has been persisted in e-conomic
# File lib/economic/entity.rb, line 132 def persisted? !!@persisted end
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.
# File lib/economic/entity.rb, line 145 def proxy self.class.proxy.new(session) end
Persist the Entity
to the API
# File lib/economic/entity.rb, line 155 def save create_or_update end
Updates properties of Entity
with the values from hash
# 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
Protected Instance Methods
Returns Hash with the data structure to send to the API
# File lib/economic/entity.rb, line 225 def build_soap_data Entity::Mapper.new(self, fields).to_hash end
# File lib/economic/entity.rb, line 241 def class_name self.class.to_s.split("::").last end
# File lib/economic/entity.rb, line 195 def create response = request(:create_from_data, "data" => build_soap_data) if response @number = response[:number] @id = response[:id] @id1 = response[:id1] @id2 = response[:id2] end @persisted = true @partial = false response end
# File lib/economic/entity.rb, line 187 def create_or_update if persisted? update else create end end
# File lib/economic/entity.rb, line 211 def defaults self.class.default_values end
# File lib/economic/entity.rb, line 229 def fields raise NotImplementedError, "Subclasses of Economic::Entity must implement `fields`" end
# File lib/economic/entity.rb, line 245 def initialize_defaults defaults.each do |property_name, default_value| send("#{property_name}=", default_value) end end
Requests an action from the API endpoint
# File lib/economic/entity.rb, line 234 def request(action, data = nil) session.request( Endpoint.new.soap_action_name(self.class, action), data ) end
# File lib/economic/entity.rb, line 215 def update response = request(:update_from_data, "data" => build_soap_data) @persisted = true @partial = false response end