Module: Balanced::Resource

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/balanced/resources/resource.rb', line 149

def method_missing(method, *args, &block)
  if @attributes.has_key?(method.to_s)
    return @attributes[method.to_s]
  end

  case method.to_s
    when /(.+)=$/
      attr = method.to_s.chop
      @attributes[attr] = args[0]
    else
      # This piece of code is a bit disgusting. We will clean it up soon,
      # but basically, we were creating closures using this code snippet
      # but those closures were transferred to the actual classes themselves
      # so you would have something like BankAccount.new.account and this
      # will give the last closure added for a BankAccount even if it has
      # nothing to do with the actual class itself.
      #
      # This caused some weird errors, so the best thing to do was to just
      # move this piece of code and "dynamically" enable it for all
      # method requests that are essentially #{method}_uri.
      #
      # This solves the acute problem, for now.
      if @hyperlinks.has_key? "#{method}"
        value = @hyperlinks["#{method}"]
        result = value.call
        return result
      else
        super
      end
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#hrefObject

Returns the value of attribute href.



11
12
13
# File 'lib/balanced/resources/resource.rb', line 11

def href
  @href
end

Returns the value of attribute hyperlinks.



9
10
11
# File 'lib/balanced/resources/resource.rb', line 9

def hyperlinks
  @hyperlinks
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/balanced/resources/resource.rb', line 10

def id
  @id
end

Returns the value of attribute links.



12
13
14
# File 'lib/balanced/resources/resource.rb', line 12

def links
  @links
end

Class Method Details

.included(base) ⇒ Object



195
196
197
# File 'lib/balanced/resources/resource.rb', line 195

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

Instance Method Details

#copy_from(other) ⇒ Object



143
144
145
146
147
# File 'lib/balanced/resources/resource.rb', line 143

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

#does_resource_respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/balanced/resources/resource.rb', line 191

def does_resource_respond_to?(method_name)
  @attributes.has_key?(method_name.to_s) or @hyperlinks.has_key?(method_name.to_s)
end

#fetch(*arguments) ⇒ Object Also known as: find

delegate the query to the pager module



81
82
83
# File 'lib/balanced/resources/resource.rb', line 81

def fetch(*arguments)
  self.class.find *arguments
end

#hydrate(links, meta) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/balanced/resources/resource.rb', line 39

def hydrate(links, meta)
  # links here is 'marketplaces.events'
  links.each do |key, uri|
    # key is 'marketplaces.events'
    property = key.sub(/.*?\./, '')
    # property here is events
    # if marketplace.links is a hash
    # for each property and uri, we want to set them on the instance
    # as pagers.
    #
    # right?
    if self.links.include? property
      link_value = self.links[property]
      # expands /customers/{marketplaces.owner_customer} to /customers/ACxxxxx
      template = Addressable::Template.new(uri)
      uri = template.expand("#{key}" => link_value).to_s
      @hyperlinks[property] = Balanced::Utils.callable(
          link_value.nil? ? link_value : lambda { self.class.find(uri) }
      )
    else
      unless uri.nil? || uri.is_a?(Hash)
        # matches something like '/blah/{class.attribute}/bliakdf'
        begin
          match_data = /\{(#{self.class.collection_name}\.(\w+))\}/.match(uri)
        rescue TypeError => ex
          puts 'what the fuck'
          raise ex
        end
        unless match_data.nil?
          attribute_path = match_data[1]
          attribute_name = match_data[2]
          template = Addressable::Template.new(uri)
          uri = template.expand("#{attribute_path}" => @attributes[attribute_name]).to_s
        end
      end
      @hyperlinks[property] = Balanced::Utils.callable(Balanced::Pager.new(uri, {}))
    end
  end
end

#initialize(attributes = {}) ⇒ Object



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

def initialize(attributes = {})
  @attributes = Utils.indifferent_read_access attributes
  @hyperlinks = {}
end

#reload(the_response = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/balanced/resources/resource.rb', line 132

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[:href])
  end
  fresh and copy_from fresh
  self
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/balanced/resources/resource.rb', line 182

def respond_to?(method_name, include_private=false)
  does_resource_respond_to?(method_name) || super
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/balanced/resources/resource.rb', line 186

def respond_to_missing?(method_name, include_private=false)
  does_resource_respond_to?(method_name) || super
end

#sanitizeObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/balanced/resources/resource.rb', line 109

def sanitize
  to_submit = {}
  @attributes.each do |key, value|
    if value.is_a? Balanced::Resource
      to_submit[key] = value.href if value.href
    else
      to_submit[key] = value
    end
  end
  to_submit
end

#saveObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/balanced/resources/resource.rb', line 87

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

  attributes_to_submit = self.sanitize
  begin
    @response = Balanced.send(method, href, attributes_to_submit)
  rescue Balanced::Error
    # restore the href on the instance if there was an exception
    # this will allow us to try to fix any attributes and save again
    @attributes['href'] = href
    raise
  end

  reload @response
end

#unstoreObject Also known as: destroy



127
128
129
# File 'lib/balanced/resources/resource.rb', line 127

def unstore
  Balanced.unstore @attributes[:href]
end