kelredd-resourceful 0.7.26 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -16,7 +16,7 @@ Resourceful uses agents to fetch and push data to and from web resources. Suppo
16
16
 
17
17
  == Installation
18
18
 
19
- sudo gem install kelredd-resourceful --source http://gemcutter.org
19
+ gem install kelredd-resourceful
20
20
 
21
21
  == Dependencies
22
22
 
@@ -90,7 +90,7 @@ A suite of cucumber features are available for you to run as acceptance tests.
90
90
 
91
91
  == License
92
92
 
93
- Copyright (c) 2009 Kelly Redding
93
+ Copyright (c) 2010 Kelly Redding (mailto:kelly@kelredd.com)
94
94
 
95
95
  Permission is hereby granted, free of charge, to any person
96
96
  obtaining a copy of this software and associated documentation
data/Rakefile CHANGED
@@ -24,7 +24,9 @@ spec = Gem::Specification.new do |s|
24
24
  s.add_dependency('rest-client')
25
25
  s.add_dependency('mechanize')
26
26
  s.add_dependency('log4r')
27
- s.add_dependency('kelredd-useful', '>= 0.2.5')
27
+ s.add_dependency('kelredd-useful', [">= 0.2.5"])
28
+
29
+ s.add_development_dependency("shoulda", [">= 2.10.2"])
28
30
  end
29
31
 
30
32
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -43,10 +45,3 @@ task :gemspec do
43
45
  File.open(file, 'w') {|f| f << spec.to_ruby }
44
46
  puts "Created gemspec: #{file}"
45
47
  end
46
-
47
- require 'cucumber'
48
- require 'cucumber/rake/task'
49
-
50
- Cucumber::Rake::Task.new(:features) do |t|
51
- t.cucumber_opts = "test/features --format pretty"
52
- end
@@ -42,23 +42,32 @@ module Resourceful::Model::ActiverecordAssociations
42
42
  def belongs_to_resourceful(name, config={})
43
43
  clean_name = Resourceful::Model::Base.cleanup_name(name)
44
44
  config ||= {}
45
- raise ArgumentError, "belongs_to_resourceful requires a :class_name option to be specified" unless config[:class_name]
46
- class_name = config.delete(:class_name).to_s
45
+
47
46
  find_method_name = (config.delete(:method) || 'find').to_s
48
47
  force = config.delete(:force) || false
49
48
  foreign_key_name = config.delete(:foreign_key_name) || 'id'
50
49
  foreign_key_method = config.delete(:foreign_key) || "#{clean_name}_id"
51
- Resourceful.add_to_associations(self.name, clean_name, {
52
- :type => :belongs_to,
53
- :class_name => class_name,
54
- :foreign_key_name => foreign_key_name,
55
- :foreign_key_method => foreign_key_method,
56
- :find_method_name => find_method_name
57
- })
50
+
51
+ if !(polymorphic = !!config.delete(:polymorphic))
52
+ raise ArgumentError, "belongs_to_resourceful requires a :class_name option to be specified" unless config[:class_name]
53
+ class_name = config.delete(:class_name).to_s
54
+ Resourceful.add_to_associations(self.name, clean_name, {
55
+ :type => :belongs_to,
56
+ :class_name => class_name,
57
+ :foreign_key_name => foreign_key_name,
58
+ :foreign_key_method => foreign_key_method,
59
+ :find_method_name => find_method_name
60
+ })
61
+ end
62
+
58
63
  define_method(name) do |*args|
59
64
  reload = args.first || false
60
65
  if reload || (assoc_val = instance_variable_get("@#{clean_name}")).nil?
61
- klass = class_name.resourceful_constantize
66
+ klass = if polymorphic
67
+ self.send("#{clean_name}_type")
68
+ else
69
+ class_name
70
+ end.resourceful_constantize
62
71
  raise ArgumentError, "belongs_to_resourceful :class_name '#{class_name}' is not defined" if klass.nil?
63
72
  unless self.respond_to?(foreign_key_method)
64
73
  raise ArgumentError, "belongs_to_resourceful requires a '#{foreign_key_method}' method defined to return the foreign_key"
@@ -74,8 +74,14 @@ module Resourceful::Model
74
74
  end
75
75
 
76
76
  def save
77
- @data = yield attributes(true)
78
- reset_attributes
77
+ # TODO: I think this is where it is failing if response does not return any data
78
+ # => test it!
79
+ if response_data = yield(attributes(true))
80
+ @data = response_data
81
+ reset_attributes
82
+ else
83
+ @attributes
84
+ end
79
85
  end
80
86
 
81
87
  def destroy
@@ -42,23 +42,32 @@ module Resourceful::Model::ExternalAssociations
42
42
  def belongs_to(name, config={})
43
43
  clean_name = cleanup_name(name)
44
44
  config ||= {}
45
- raise ArgumentError, "belongs_to requires a :class option to be specified" unless config[:class]
46
- class_name = config.delete(:class).to_s
45
+
47
46
  find_method_name = (config.delete(:method) || 'find').to_s
48
47
  force = config.delete(:force) || false
49
48
  foreign_key_name = config.delete(:foreign_key_name) || 'id'
50
49
  foreign_key_method = config.delete(:foreign_key) || "#{clean_name}_id"
51
- Resourceful.add_to_associations(self.name, clean_name, {
52
- :type => :belongs_to,
53
- :class_name => class_name,
54
- :foreign_key_name => foreign_key_name,
55
- :foreign_key_method => foreign_key_method,
56
- :find_method_name => find_method_name
57
- })
50
+
51
+ if !(polymorphic = !!config.delete(:polymorphic))
52
+ raise ArgumentError, "belongs_to requires a :class option to be specified" unless config[:class]
53
+ class_name = config.delete(:class).to_s
54
+ Resourceful.add_to_associations(self.name, clean_name, {
55
+ :type => :belongs_to,
56
+ :class_name => class_name,
57
+ :foreign_key_name => foreign_key_name,
58
+ :foreign_key_method => foreign_key_method,
59
+ :find_method_name => find_method_name
60
+ })
61
+ end
62
+
58
63
  define_method(name) do |*args|
59
64
  reload = args.first || false
60
65
  if reload || (assoc_val = instance_variable_get("@#{clean_name}")).nil?
61
- klass = self.class.get_namespaced_klass(class_name)
66
+ klass = self.class.get_namespaced_klass(if polymorphic
67
+ self.send("#{clean_name}_type")
68
+ else
69
+ class_name
70
+ end)
62
71
  raise ArgumentError, "belongs_to :class '#{class_name}' is not defined in any given namespaces" if klass.nil?
63
72
  unless self.respond_to?(foreign_key_method)
64
73
  raise ArgumentError, "belongs_to requires a '#{foreign_key_method}' method defined to return the foreign_key"
@@ -42,7 +42,11 @@ module Resourceful::Model
42
42
 
43
43
  def self.get_result_data(result, search)
44
44
  hsh_keys = search.kind_of?(String) ? search.split(/\s+/) : nil
45
- result_data = !hsh_keys.nil? && !hsh_keys.empty? && result.respond_to?(:get_value) ? (result.get_value(hsh_keys) || result) : result
45
+ if !hsh_keys.nil? && !hsh_keys.empty? && result.respond_to?(:get_value)
46
+ result.get_value(hsh_keys) || result
47
+ else
48
+ result
49
+ end
46
50
  end
47
51
 
48
52
  def self.format
@@ -39,7 +39,11 @@ module Resourceful::Model
39
39
 
40
40
  def push_data(verb, path, opts, data, search=nil)
41
41
  result = super(verb, path, opts, data)
42
- search.nil? ? result : (result.search(search) || result)
42
+ if search.nil?
43
+ result
44
+ else
45
+ result.search(search) || result
46
+ end
43
47
  end
44
48
 
45
49
  def self.format
@@ -8,40 +8,60 @@ module Resourceful::Resource
8
8
 
9
9
  SUPPORTED = ['json', 'xml']
10
10
 
11
- def self.get(format)
12
- raise Resourceful::Exceptions::FormatError, "the format '#{format}' is not supported" unless SUPPORTED.include?(format.to_s)
13
- self.send(format)
14
- end
15
-
16
- def self.json
17
- Resourceful::Resource::Json
18
- end
11
+ class << self
19
12
 
20
- def self.xml
21
- Resourceful::Resource::Xml
22
- end
23
-
24
- def self.to_s
25
- raise 'method not implemented by this format'
26
- end
13
+ def get(format)
14
+ raise Resourceful::Exceptions::FormatError, "the format '#{format}' is not supported" unless SUPPORTED.include?(format.to_s)
15
+ self.send(format)
16
+ end
17
+
18
+ def json
19
+ Resourceful::Resource::Json
20
+ end
21
+
22
+ def xml
23
+ Resourceful::Resource::Xml
24
+ end
25
+
26
+ def to_s
27
+ raise 'method not implemented by this format'
28
+ end
29
+
30
+ def build(str)
31
+ raise 'method not implemented by this format'
32
+ end
27
33
 
28
- def self.build(str)
29
- raise 'method not implemented by this format'
30
- end
34
+ protected
31
35
 
36
+ def parsable?(data)
37
+ if !data.nil? && !data.to_s.strip.empty? && data.to_s.strip != 'null'
38
+ yield if block_given?
39
+ else
40
+ nil
41
+ end
42
+ end
43
+
44
+ end
45
+
32
46
  end
33
47
  end
34
48
 
35
49
  module Resourceful::Resource
36
50
  class Json < Resourceful::Resource::Format
37
51
 
38
- def self.to_s
39
- "json"
40
- end
41
-
42
- def self.build(json_str)
43
- !json_str.nil? && !json_str.to_s.empty? && (json_str.to_s != 'null') ? JSON.parse(json_str.to_s) : nil
44
- end
52
+ class << self
53
+
54
+ def to_s
55
+ "json"
56
+ end
57
+
58
+ def build(json_str)
59
+ parsable?(json_str) do
60
+ JSON.parse(json_str.to_s.strip)
61
+ end
62
+ end
63
+
64
+ end
45
65
 
46
66
  end
47
67
  end
@@ -49,13 +69,19 @@ end
49
69
  module Resourceful::Resource
50
70
  class Xml < Resourceful::Resource::Format
51
71
 
52
- def self.to_s
53
- "xml"
54
- end
55
-
56
- def self.build(xml_str)
57
- Nokogiri::XML(xml_str.to_s)
72
+ class << self
73
+
74
+ def to_s
75
+ "xml"
76
+ end
77
+
78
+ def build(xml_str)
79
+ parsable?(xml_str) do
80
+ Nokogiri::XML(xml_str.to_s)
81
+ end
82
+ end
83
+
58
84
  end
59
-
85
+
60
86
  end
61
87
  end
@@ -72,17 +72,22 @@ module Resourceful::Shoulda::TestUnit
72
72
 
73
73
  def should_have_resourceful_association_item(name, opts)
74
74
  klass = described_type
75
- should_have_resourceful_association_class(opts)
76
- should "return an item that is a kind of #{opts[:class]}" do
75
+ should_have_resourceful_association_class(name, opts)
76
+ should "return an item that is of the correct class" do
77
77
  if subject && (val = subject.send(name))
78
- assert_kind_of klass.get_namespaced_klass(opts[:class]), val, "#{name} should be a kind of #{opts[:class]}"
78
+ class_name = if !!opts[:polymorphic]
79
+ val.send("#{name}_type")
80
+ else
81
+ opts[:class]
82
+ end
83
+ assert_kind_of klass.get_namespaced_klass(class_name), val, "#{name} should be a kind of #{class_name}"
79
84
  end
80
85
  end
81
86
  end
82
87
 
83
88
  def should_have_resourceful_association_collection(name, opts)
84
89
  klass = described_type
85
- should_have_resourceful_association_class(opts)
90
+ should_have_resourceful_association_class(name, opts)
86
91
  should "return a collection of items that are a kind of #{opts[:class]}" do
87
92
  if subject && (val = subject.send(name))
88
93
  assert_kind_of ::Array, val, "#{name} should be a kind of Array"
@@ -93,11 +98,17 @@ module Resourceful::Shoulda::TestUnit
93
98
  end
94
99
  end
95
100
 
96
- def should_have_resourceful_association_class(opts)
101
+ def should_have_resourceful_association_class(name, opts)
102
+ # TODO: update this for polymorphic
97
103
  klass = described_type
98
104
  should "have a #{opts[:class]} class specified that is defined" do
99
- assert opts[:class], "resourceful associations require a :class option"
100
- assert klass.get_namespaced_klass(opts[:class]), "a namespaced #{opts[:class]} is not defined"
105
+ class_name = if !!opts[:polymorphic]
106
+ subject.send("#{name}_type")
107
+ else
108
+ opts[:class]
109
+ end
110
+ assert class_name, "resourceful can't determine the class name from either the :class option or a :polymorphic setting"
111
+ assert klass.get_namespaced_klass(class_name), "a namespaced #{class_name} is not defined"
101
112
  end
102
113
  end
103
114
 
@@ -2,8 +2,8 @@ module Resourceful
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 7
6
- TINY = 26
5
+ MINOR = 8
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.7.26
4
+ version: 0.8.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-12-06 00:00:00 -06:00
12
+ date: 2010-02-11 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -72,6 +72,16 @@ dependencies:
72
72
  - !ruby/object:Gem::Version
73
73
  version: 0.2.5
74
74
  version:
75
+ - !ruby/object:Gem::Dependency
76
+ name: shoulda
77
+ type: :development
78
+ version_requirement:
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.10.2
84
+ version:
75
85
  description:
76
86
  email: kelly@kelredd.com
77
87
  executables: []