kelredd-resourceful 0.4.9 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -37,8 +37,9 @@ The supported formats are:
37
37
  A suite of cucumber features are available for you to run as an acceptance test. You should look to the features
38
38
  for additional documentation and usage scenarios. To run the features:
39
39
 
40
- # make sure you have the cucumber gem
40
+ # make sure you have these gems
41
41
  $ sudo gem install cucumber
42
+ $ sudo gem install fakeweb
42
43
  # change to the resourceful gem installed location
43
44
  $ cd /opt/local/lib/ruby/gems/x.x/gems/kelredd-resourceful-xxx
44
45
  $ rake features
@@ -17,13 +17,15 @@ module Resourceful
17
17
  @logger.add(Log4r::StdoutOutputter.new('console'))
18
18
  end
19
19
 
20
- def get(path, opts={}, &block)
20
+ protected
21
+
22
+ def call_resource(verb, path, opts={}, &block)
21
23
  path, opts = check_config(path, opts)
22
24
  format = Resourceful::Resource::Format.get(opts[:format])
23
25
 
24
26
  full_resource_path = self.class.resource_path(path, format, opts[:params])
25
- resource_summary = summary('get', full_resource_path)
26
- cache_key = Resourceful::Resource::Cache.key(@host, 'get', full_resource_path)
27
+ resource_summary = summary(verb.to_s, full_resource_path)
28
+ cache_key = Resourceful::Resource::Cache.key(@host, verb.to_s, full_resource_path)
27
29
 
28
30
  if opts[:force] || (resp = cache.read(cache_key)).nil?
29
31
  log "Resource call: #{resource_summary}"
@@ -34,8 +36,6 @@ module Resourceful
34
36
  format.build(resp)
35
37
  end
36
38
 
37
- protected
38
-
39
39
  def check_config(path, opts) # :nodoc:
40
40
  raise Resourceful::Exceptions::ConfigurationError, "invalid agent" unless agent && agent_url && !agent_url.empty?
41
41
  opts ||= {}
@@ -18,21 +18,21 @@ module Resourceful
18
18
  end
19
19
  end
20
20
 
21
- def get(path, opts={})
22
- super(path, opts) do |path|
21
+ def get(path, opts={}, &block)
22
+ call_resource(:get, path, opts, block)
23
+ end
24
+
25
+ protected
26
+
27
+ def call_resource(verb, path, opts, block)
28
+ super(verb, path, opts) do |path|
23
29
  resp = ""
24
- @mechanize.get("#{@host}#{path}") do |page|
25
- resp = if block_given?
26
- yield page
27
- else
28
- page
29
- end
30
+ @mechanize.send(verb.to_s, "#{@host}#{path}") do |page|
31
+ resp = block ? block.call(page) : page
30
32
  end
31
33
  resp.body
32
34
  end
33
35
  end
34
-
35
- protected
36
36
 
37
37
  def agent
38
38
  @mechanize
@@ -11,19 +11,41 @@ module Resourceful
11
11
  @rest_client = ::RestClient::Resource.new(@host, :user => @user, :password => @password)
12
12
  end
13
13
 
14
- def get(path, opts={})
15
- super(path, opts) do |path|
16
- result = @rest_client[path].get
17
- if block_given?
18
- yield result
19
- else
20
- result
21
- end
22
- end
14
+ def get(path, opts={}, &block)
15
+ call_resource(:get, path, opts, block)
16
+ end
17
+
18
+ def post(path, opts={}, body=nil, &block)
19
+ push_resource(:post, path, opts, body, block)
20
+ end
21
+
22
+ def put(path, opts={}, body=nil, &block)
23
+ push_resource(:put, path, opts, body, block)
24
+ end
25
+
26
+ def delete(path, opts={}, body=nil, &block)
27
+ push_resource(:delete, path, opts, body, block)
23
28
  end
24
29
 
25
30
  protected
26
31
 
32
+ def push_resource(verb, path, opts, body, block)
33
+ opts[:body] = body
34
+ call_resource(verb, path, opts, block)
35
+ end
36
+
37
+ def call_resource(verb, path, opts, block)
38
+ super(verb, path, opts) do |path|
39
+ result = case verb.to_sym
40
+ when :get
41
+ @rest_client[path].get
42
+ when :post, :put, :delete
43
+ @rest_client[path].send(verb, opts[:body])
44
+ end
45
+ block ? block.call(result) : result
46
+ end
47
+ end
48
+
27
49
  def agent
28
50
  @rest_client
29
51
  end
@@ -24,12 +24,42 @@ module Resourceful
24
24
  @data
25
25
  end
26
26
 
27
- def attributes
28
- @attributes ||= @@attributes[self.class.name].inject({}) { |hsh, key| hsh[key] = self.send(key); hsh }
27
+ def new_record?
28
+ @data.nil?
29
+ end
30
+
31
+ def attributes(force=false)
32
+ if @attributes.nil? || force
33
+ @attributes = @@attributes[self.class.name].inject({}) { |hsh, key| hsh[key] = self.send(key); hsh }
34
+ end
35
+ @attributes
36
+ end
37
+
38
+ def update_attributes(attr_hash={})
39
+ attr_hash.each do |k,v|
40
+ self.send("#{key}=", v)
41
+ end
42
+ attributes(true)
43
+ end
44
+
45
+ def save
46
+ @data = yield attributes(true)
47
+ reset_attributes
48
+ end
49
+
50
+ def destroy
51
+ yield attributes(true)
52
+ @data = nil
29
53
  end
30
54
 
31
55
  protected
32
56
 
57
+ def push_data(verb, path, opts, data)
58
+ block = opts.delete(:on_response)
59
+ ['id', :id].each { |a| data.delete(a) } if data.kind_of?(::Hash)
60
+ self.class.set_agent.send(verb.to_s, path, opts, data, &block)
61
+ end
62
+
33
63
  def self.attribute(name, type, config={})
34
64
  clean_name = name.to_s.gsub(/\W/,'')
35
65
  add_to_attributes(name.to_s)
@@ -63,6 +93,9 @@ module Resourceful
63
93
  end
64
94
  )
65
95
  end
96
+ define_method("#{name}=") do |value|
97
+ instance_variable_set("@#{clean_name}", value)
98
+ end
66
99
  end
67
100
 
68
101
  def self.has_one(name, config={})
@@ -117,6 +150,20 @@ module Resourceful
117
150
 
118
151
  private
119
152
 
153
+ def reset_attributes
154
+ clear_attributes
155
+ attributes(true)
156
+ end
157
+
158
+ def clear_attributes
159
+ @@attributes ||= {}
160
+ klass_name = self.class.name
161
+ @@attributes[klass_name] ||= []
162
+ @@attributes[klass_name].each do |a|
163
+ instance_variable_set("@#{a.to_s.gsub(/\W/,'')}", nil)
164
+ end
165
+ end
166
+
120
167
  def self.add_to_attributes(name)
121
168
  @@attributes ||= {}
122
169
  klass_name = self.name
@@ -26,8 +26,8 @@ module Resourceful
26
26
  end
27
27
  end
28
28
 
29
- def initialize(json)
30
- raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Json model with '#{json.class.name}' data" unless json.kind_of?(::Hash)
29
+ def initialize(json=nil)
30
+ raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Json model with '#{json.class.name}' data" unless json.nil? || json.kind_of?(::Hash)
31
31
  @data = json
32
32
  end
33
33
 
@@ -26,8 +26,8 @@ module Resourceful
26
26
  end
27
27
  end
28
28
 
29
- def initialize(xml)
30
- raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Xml model with '#{xml.class.name}' data" unless xml.kind_of?(Nokogiri::XML::NodeSet) || xml.kind_of?(Nokogiri::XML::Element)
29
+ def initialize(xml=nil)
30
+ raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Xml model with '#{xml.class.name}' data" unless xml.nil? || xml.kind_of?(Nokogiri::XML::NodeSet) || xml.kind_of?(Nokogiri::XML::Element)
31
31
  @data = xml
32
32
  end
33
33
 
@@ -2,8 +2,8 @@ module Resourceful
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 4
6
- TINY = 9
5
+ MINOR = 5
6
+ TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-resourceful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-06 00:00:00 -07:00
12
+ date: 2009-08-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency