restfully 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
data/lib/restfully.rb CHANGED
@@ -12,7 +12,7 @@ require 'restfully/collection'
12
12
 
13
13
 
14
14
  module Restfully
15
- VERSION = "0.3.0"
15
+ VERSION = "0.3.1"
16
16
  class << self
17
17
  attr_accessor :adapter
18
18
  end
@@ -4,23 +4,29 @@ module Restfully
4
4
  # Resource and Collection classes should clearly include a common module to deal with links, attributes, associations and loading
5
5
  class Collection < DelegateClass(Array)
6
6
 
7
- attr_reader :state, :raw, :uri, :session, :title, :offset, :total
7
+ attr_reader :state, :raw, :uri, :session, :title, :offset, :total, :items
8
8
 
9
9
  def initialize(uri, session, options = {})
10
10
  options = options.symbolize_keys
11
11
  @uri = uri
12
12
  @title = options[:title]
13
13
  @session = session
14
- @state = :unloaded
15
- @items = []
16
- @indexes = {}
17
- @associations = {}
18
- @attributes = {}
14
+ reset
19
15
  super(@items)
20
16
  end
21
17
 
22
18
  def loaded?; @state == :loaded; end
23
19
 
20
+ def reset
21
+ @last_request_hash = nil
22
+ @associations = {}
23
+ @attributes = {}
24
+ @indexes = {}
25
+ @items = []
26
+ @raw = nil
27
+ @state = :unloaded
28
+ end
29
+
24
30
  def method_missing(method, *args)
25
31
  load
26
32
  if association = @associations[method.to_s]
@@ -28,7 +34,7 @@ module Restfully
28
34
  association.load(*args)
29
35
  elsif method.to_s =~ /^by_(.+)$/
30
36
  key = $1
31
- @indexes[method.to_s] ||= @items.inject({}) { |accu, item|
37
+ @indexes[method.to_s] ||= self.inject({}) { |accu, item|
32
38
  accu[item.has_key?(key) ? item[key] : item.send(key.to_sym)] = item
33
39
  accu
34
40
  }
@@ -46,14 +52,14 @@ module Restfully
46
52
 
47
53
  def load(options = {})
48
54
  options = options.symbolize_keys
49
- force_reload = !!options.delete(:reload) || options.has_key?(:query)
50
- if loaded? && !force_reload && options[:raw].nil?
55
+ force_reload = !!options.delete(:reload)
56
+ request_hash = [:get, options].hash
57
+ if loaded? && !force_reload && request_hash == @last_request_hash
51
58
  self
52
59
  else
60
+ reset
61
+ @last_request_hash = request_hash
53
62
  @raw = options[:raw]
54
- @associations.clear
55
- @attributes.clear
56
- @items.clear
57
63
  if raw.nil? || force_reload
58
64
  response = session.get(uri, options)
59
65
  @raw = response.body
@@ -68,7 +74,7 @@ module Restfully
68
74
  value.each do |item|
69
75
  self_link = (item['links'] || []).map{|link| Link.new(link)}.detect{|link| link.self?}
70
76
  if self_link && self_link.valid?
71
- @items << Resource.new(self_link.href, session).load(:raw => item)
77
+ self.push Resource.new(self_link.href, session).load(:raw => item)
72
78
  else
73
79
  session.logger.warn "Resource #{key} does not have a 'self' link. skipped."
74
80
  end
@@ -83,7 +89,7 @@ module Restfully
83
89
  @attributes.store(key, value)
84
90
  end
85
91
  end
86
- end
92
+ end
87
93
  @state = :loaded
88
94
  self
89
95
  end
@@ -109,9 +115,9 @@ module Restfully
109
115
  output += "\n#{options[:space]}#{key.inspect} => #{value.inspect}"
110
116
  end
111
117
  end
112
- unless @items.empty?
118
+ unless self.empty?
113
119
  output += "\n#{options[:space]}------------ ITEMS ------------"
114
- @items.each do |value|
120
+ self.each do |value|
115
121
  output += "\n#{options[:space]}#{value.class.name}"
116
122
  end
117
123
  end
data/restfully.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{restfully}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cyril Rohr"]
12
- s.date = %q{2009-10-26}
12
+ s.date = %q{2009-10-27}
13
13
  s.default_executable = %q{restfully}
14
14
  s.description = %q{Experimental code for auto-generation of wrappers on top of RESTful APIs that follow HATEOAS principles and provide OPTIONS methods and/or Allow headers.}
15
15
  s.email = %q{cyril.rohr@gmail.com}
@@ -29,6 +29,7 @@ describe Collection do
29
29
  it "should not load if already loaded and no :reload" do
30
30
  collection = Collection.new("uri", mock("session"))
31
31
  collection.should_receive(:loaded?).and_return(true)
32
+ collection.instance_variable_set(:@last_request_hash, [:get, {}].hash)
32
33
  collection.load(:reload => false).should == collection
33
34
  end
34
35
  it "should load when :reload param is true [already loaded]" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restfully
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Rohr
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-26 00:00:00 +01:00
12
+ date: 2009-10-27 00:00:00 +01:00
13
13
  default_executable: restfully
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency