class Economic::Entity

Attributes

id[R]

Returns the id of Entity. This does not trigger a load from the API even if Entity is partial

number[R]

Returns the number of Entity. This does not trigger a load from the API even if Entity is partial

partial[RW]

Internal accessors

persisted[RW]

Internal accessors

session[RW]

Internal accessors

Public Class Methods

default_values() click to toggle source

Returns the default values for properties

# File lib/economic/entity.rb, line 19
def default_values
  @default_values || {}
end
defaults(default_values) click to toggle source

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
handle_writer(property) click to toggle source

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
has_properties(*properties) click to toggle source
# 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() click to toggle source

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
new(properties = {}) click to toggle source
# File lib/economic/entity.rb, line 103
def initialize(properties = {})
  initialize_defaults
  update_properties(properties)
  @persisted = false
  @partial = true
end
properties() click to toggle source
# File lib/economic/entity.rb, line 71
def properties
  @properties || []
end
properties_not_triggering_full_load() click to toggle source
# File lib/economic/entity.rb, line 31
def properties_not_triggering_full_load
  [:id, :number, :handle]
end
property_reader(property) click to toggle source

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
property_writer(property) click to toggle source

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
proxy() click to toggle source

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

==(other) click to toggle source
# File lib/economic/entity.rb, line 180
def ==(other)
  return false if other.nil?
  handle == other.handle && other.is_a?(self.class)
end
destroy() click to toggle source

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() click to toggle source

Get default Entity with its number from the API

# File lib/economic/entity.rb, line 119
def get
  proxy.get
end
get_data() click to toggle source

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
handle() click to toggle source
# File lib/economic/entity.rb, line 95
def handle
  @handle || Handle.build(:number => @number, :id => @id)
end
handle=(handle) click to toggle source
# File lib/economic/entity.rb, line 99
def handle=(handle)
  @handle = Handle.build(handle)
end
inspect() click to toggle source
# 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?() click to toggle source

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
persisted?() click to toggle source

Returns true if CurrentInvoiceLine has been persisted in e-conomic

# File lib/economic/entity.rb, line 132
def persisted?
  !!@persisted
end
proxy() click to toggle source

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
save() click to toggle source

Persist the Entity to the API

# File lib/economic/entity.rb, line 155
def save
  create_or_update
end
update_properties(hash) click to toggle source

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

build_soap_data() click to toggle source

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
class_name() click to toggle source
# File lib/economic/entity.rb, line 241
def class_name
  self.class.to_s.split("::").last
end
create() click to toggle source
# 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
create_or_update() click to toggle source
# File lib/economic/entity.rb, line 187
def create_or_update
  if persisted?
    update
  else
    create
  end
end
defaults() click to toggle source
# File lib/economic/entity.rb, line 211
def defaults
  self.class.default_values
end
fields() click to toggle source
# File lib/economic/entity.rb, line 229
def fields
  raise NotImplementedError, "Subclasses of Economic::Entity must implement `fields`"
end
initialize_defaults() click to toggle source
# File lib/economic/entity.rb, line 245
def initialize_defaults
  defaults.each do |property_name, default_value|
    send("#{property_name}=", default_value)
  end
end
request(action, data = nil) click to toggle source

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
update() click to toggle source
# File lib/economic/entity.rb, line 215
def update
  response = request(:update_from_data, "data" => build_soap_data)

  @persisted = true
  @partial = false

  response
end