Module: Balanced::Resource

Included in:
BankAccount, Credit, Debit
Defined in:
lib/balanced_ach/resources/resource.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/balanced_ach/resources/resource.rb', line 56

def method_missing(method, *args, &block)
  case method
    when /(.+)\=$/
      attr = method.to_s.chop
      @attributes[attr] = args[0]
    else
      super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/balanced_ach/resources/resource.rb', line 6

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



65
66
67
# File 'lib/balanced_ach/resources/resource.rb', line 65

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#copy_from(other) ⇒ Object



50
51
52
53
54
# File 'lib/balanced_ach/resources/resource.rb', line 50

def copy_from other
  other.instance_variables.each do |ivar|
    instance_variable_set ivar, other.instance_variable_get(ivar)
  end
end

#destroyObject



35
36
37
# File 'lib/balanced_ach/resources/resource.rb', line 35

def destroy
  Balanced.delete @attributes[:uri]
end

#find(*arguments) ⇒ Object

delegate the query to the pager module



14
15
16
# File 'lib/balanced_ach/resources/resource.rb', line 14

def find *arguments
  self.class.find *arguments
end

#initialize(attributes = {}) ⇒ Object



8
9
10
# File 'lib/balanced_ach/resources/resource.rb', line 8

def initialize attributes = {}
  @attributes = attributes
end

#reload(the_response = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/balanced_ach/resources/resource.rb', line 39

def reload the_response = nil
  if the_response
    return if the_response.body.to_s.length.zero?
    fresh = self.class.construct_from_response the_response.body
  else
    fresh = self.find(@attributes[:uri])
  end
  fresh and copy_from fresh
  self
end

#saveObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/balanced_ach/resources/resource.rb', line 18

def save
  uri = @attributes.delete('uri')
  method = :post
  if uri.nil?
    uri = self.class.collection_path
  elsif !Balanced.is_collection(uri)
    method = :put
  end
  @response = Balanced.send(method, uri, self.attributes)
  reload @response
end