Class: Economic::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/economic/entity.rb

Direct Known Subclasses

CurrentInvoice, CurrentInvoiceLine, Debtor, DebtorContact

Defined Under Namespace

Classes: Handle

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Entity) initialize(properties = {})

Returns a new instance of Entity



80
81
82
83
84
85
# File 'lib/economic/entity.rb', line 80

def initialize(properties = {})
  initialize_defaults
  update_properties(properties)
  @persisted = false
  @partial = true
end

Instance Attribute Details

- (Object) partial

Internal accessors



30
31
32
# File 'lib/economic/entity.rb', line 30

def partial
  @partial
end

- (Object) persisted

Internal accessors



30
31
32
# File 'lib/economic/entity.rb', line 30

def persisted
  @persisted
end

- (Object) session

Internal accessors



30
31
32
# File 'lib/economic/entity.rb', line 30

def session
  @session
end

Class Method Details

+ (Object) has_properties(*properties)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/economic/entity.rb', line 37

def has_properties(*properties)
  @properties = properties
  properties.each do |property|
    unless properties_not_triggering_full_load.include?(property) || instance_methods.collect(&:to_s).include?(property.to_s)
      # Create property accessors that loads the full Entity from the API if necessary
      define_method "#{property}" do
        value = instance_variable_get("@#{property}")
        if value.nil? && partial? && persisted?
          instance_variable_get("@#{property}")
        else
          value
        end
      end
    end

    # Just use regular writers
    attr_writer property
  end
end

+ (Object) properties



57
58
59
# File 'lib/economic/entity.rb', line 57

def properties
  @properties || []
end

+ (Object) properties_not_triggering_full_load



33
34
35
# File 'lib/economic/entity.rb', line 33

def properties_not_triggering_full_load
  [:id, :number, :handle]
end

+ (Object) proxy

Returns the class used to instantiate a proxy for Entity



62
63
64
65
66
# File 'lib/economic/entity.rb', line 62

def proxy
  class_name = name.split('::').last
  proxy_class_name = "#{class_name}Proxy"
  Economic.const_get(proxy_class_name)
end

+ (Object) soap_action(action)

Returns the E-conomic API action name to call



69
70
71
72
73
# File 'lib/economic/entity.rb', line 69

def soap_action(action)
  class_name = self.name
  class_name_without_modules = class_name.split('::').last
  "#{class_name_without_modules.snakecase}_#{action.to_s.snakecase}".intern
end

Instance Method Details

- (Object) get_data

Updates Entity with its data from the API



92
93
94
95
96
97
# File 'lib/economic/entity.rb', line 92

def get_data
  response = proxy.get_data(number)
  self.update_properties(response)
  self.partial = false
  self.persisted = true
end

- (Object) handle



76
77
78
79
80
81
# File 'lib/economic/entity.rb', line 76

def handle
  handle = {}
  handle[:id] = id unless id.blank?
  handle[:number] = number unless number.blank?
  handle
end

- (Object) id

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



105
106
107
# File 'lib/economic/entity.rb', line 105

def id
  @id
end

- (Object) initialize_defaults



87
88
89
# File 'lib/economic/entity.rb', line 87

def initialize_defaults
  nil
end

- (Object) inspect



134
135
136
137
# File 'lib/economic/entity.rb', line 134

def inspect
  props = self.class.properties.collect { |p| "#{p}=#{self.send(p).inspect}" }
  "#<#{self.class}:#{self.object_id} partial=#{partial?}, persisted=#{persisted?}, #{props.join(', ')}>"
end

- (Object) number

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



100
101
102
# File 'lib/economic/entity.rb', line 100

def number
  @number
end

- (Boolean) partial?

Returns true if Entity has not been fully loaded from API yet

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/economic/entity.rb', line 122

def partial?
  # TODO: Can this be introspected somehow?
  !!@partial
end

- (Boolean) persisted?

Returns true if CurrentInvoiceLine has been persisted in e-conomic

Returns:

  • (Boolean)


117
118
119
# File 'lib/economic/entity.rb', line 117

def persisted?
  !!@persisted
end

- (Object) proxy

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.



130
131
132
# File 'lib/economic/entity.rb', line 130

def proxy
  self.class.proxy.new(session)
end

- (Object) save

Persist the Entity to the API



140
141
142
# File 'lib/economic/entity.rb', line 140

def save
  create_or_update
end

- (Object) update_properties(hash)

Updates properties of Entity with the values from hash



145
146
147
148
149
150
151
152
# File 'lib/economic/entity.rb', line 145

def update_properties(hash)
  hash.each do |key, value|
    setter_method = "#{key}="
    if self.respond_to?(setter_method)
      self.send(setter_method, value)
    end
  end
end