kelredd-resourceful 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,17 +3,11 @@ Given /^I am user with the screen_name "([^\"]*)"$/ do |screen_name|
3
3
  end
4
4
 
5
5
  When /^I load my "([^\"]*)" user model$/ do |klass|
6
- name = "User#{klass}"
7
- constant = ::Object
8
- model_klass = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
9
- @result = model_klass.find(@screen_name)
6
+ @result = "User#{klass}".constantize.find(@screen_name)
10
7
  end
11
8
 
12
9
  When /^I load the "([^\"]*)" status "([^\"]*)"$/ do |klass, collection|
13
- name = "Status#{klass}"
14
- constant = ::Object
15
- model_klass = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
16
- @result = model_klass.find(collection)
10
+ @result = "Status#{klass}".constantize.find(collection)
17
11
  end
18
12
 
19
13
  Then /^the result should be a valid User model$/ do
@@ -32,23 +26,38 @@ Then /^the result should be a valid User model$/ do
32
26
  assert_kind_of DateTime, @result.last_status_at
33
27
  assert !@result.last_status.nil?
34
28
  assert @result.last_status != ''
29
+
30
+ assert_kind_of Resourceful::Model::Base, @result.last_status
31
+ assert_valid_status(@result.last_status)
35
32
  end
36
33
 
37
34
  Then /^the result should be a collection of valid Status models$/ do
38
35
  assert_kind_of Array, @result
39
36
  assert_equal 20, @result.length
37
+ assert_valid_status(@result.first)
40
38
 
41
- [:id, :text, :source, :truncated, :favorited, :reply_status, :reply_user, :user_id, :user_screen_name, :user].each do |attribute|
39
+ [:user_id, :user_screen_name, :user].each do |attribute|
42
40
  assert @result.first.respond_to?(attribute)
43
41
  assert_nothing_raised do
44
42
  @result.first.send(attribute.to_s)
45
43
  end
46
44
  end
47
-
48
- [:id, :text, :truncated, :favorited, :user_id, :user_screen_name, :user].each do |attribute|
45
+ [:user_id, :user_screen_name, :user].each do |attribute|
49
46
  assert !@result.first.send(attribute.to_s).nil?
50
47
  end
51
-
52
48
  assert_kind_of Resourceful::Model::Base, @result.first.user
49
+ end
50
+
51
+ def assert_valid_status(status)
52
+ [:id, :text, :source, :truncated, :favorited, :reply_status, :reply_user].each do |attribute|
53
+ assert status.respond_to?(attribute)
54
+ assert_nothing_raised do
55
+ status.send(attribute.to_s)
56
+ end
57
+ end
58
+
59
+ [:id, :text, :truncated, :favorited].each do |attribute|
60
+ assert !status.send(attribute.to_s).nil?
61
+ end
53
62
 
54
63
  end
@@ -19,6 +19,8 @@ class UserXml < Resourceful::Model::Xml
19
19
  attribute :created_on, :date, :path => 'created_at'
20
20
  attribute :last_status_at, :datetime, :path => "status/created_at"
21
21
  attribute :last_status, :string, :path => "status/text"
22
+
23
+ has_one :last_status, :path => "status", :klass => "StatusXml"
22
24
 
23
25
  end
24
26
 
@@ -43,6 +45,8 @@ class UserJson < Resourceful::Model::Json
43
45
  attribute :created_on, :date, :path => 'created_at'
44
46
  attribute :last_status_at, :datetime, :path => "status/created_at"
45
47
  attribute :last_status, :string, :path => "status/text"
48
+
49
+ has_one :last_status, :path => "status", :klass => "StatusJson"
46
50
 
47
51
  end
48
52
 
@@ -65,9 +69,7 @@ class StatusXml < Resourceful::Model::Xml
65
69
  attribute :user_id, :integer, :path => "user/id"
66
70
  attribute :user_screen_name, :integer, :path => "user/screen_name"
67
71
 
68
- def user
69
- @user ||= (get_node('./user') ? UserXml.new(get_node('./user')) : nil)
70
- end
72
+ has_one :user, :path => "user", :klass => "UserXml"
71
73
 
72
74
  end
73
75
 
@@ -88,10 +90,8 @@ class StatusJson < Resourceful::Model::Json
88
90
  attribute :reply_status, :integer, :path => 'in_reply_to_status_id'
89
91
  attribute :reply_user, :integer, :path => 'in_reply_to_user_id'
90
92
  attribute :user_id, :integer, :path => "user/id"
91
- attribute :user_screen_name, :integer, :path => "user/screen_name"
93
+ attribute :user_screen_name, :string, :path => "user/screen_name"
92
94
 
93
- def user
94
- @user ||= UserXml.find(self.user_screen_name)
95
- end
95
+ belongs_to :user, :klass => "UserJson"
96
96
 
97
97
  end
@@ -1,5 +1,4 @@
1
1
  require 'rest_client'
2
- require File.join(File.dirname(__FILE__), '..', 'resource', 'format.rb')
3
2
  require File.join(File.dirname(__FILE__), 'base.rb')
4
3
 
5
4
  module Resourceful
@@ -28,6 +28,32 @@ module Resourceful
28
28
  end
29
29
  end
30
30
 
31
+ unless "".respond_to?(:constantize)
32
+ if Module.method(:const_get).arity == 1
33
+ def constantize #:nodoc:
34
+ names = self.split('::')
35
+ names.shift if names.empty? || names.first.empty?
36
+
37
+ constant = ::Object
38
+ names.each do |name|
39
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
40
+ end
41
+ constant
42
+ end
43
+ else # Ruby 1.9 version
44
+ def constantize #:nodoc:
45
+ names = self.split('::')
46
+ names.shift if names.empty? || names.first.empty?
47
+
48
+ constant = ::Object
49
+ names.each do |name|
50
+ constant = constant.const_get(name, false) || constant.const_missing(name)
51
+ end
52
+ constant
53
+ end
54
+ end
55
+ end
56
+
31
57
  end
32
58
  end
33
59
  end
@@ -20,7 +20,9 @@ module Resourceful
20
20
 
21
21
  protected
22
22
 
23
- def self.attribute(name, type, config)
23
+ def self.attribute(name, type, config={})
24
+ config ||= {}
25
+ config[:path] ||= name
24
26
  content_method = case type.to_sym
25
27
  when :string
26
28
  'to_s'
@@ -38,7 +40,61 @@ module Resourceful
38
40
  'to_s'
39
41
  end
40
42
  define_method(name) do
41
- instance_variable_get("@#{name}") || instance_variable_set("@#{name}", ((a = attribute(config)) && a.kind_of?(String)) ? a.send(content_method) : a)
43
+ instance_variable_get("@#{name}") || \
44
+ instance_variable_set("@#{name}", \
45
+ if ((a = attribute(config)) && a.kind_of?(String))
46
+ a.send(content_method)
47
+ else
48
+ a
49
+ end
50
+ )
51
+ end
52
+ end
53
+
54
+ def self.has_one(name, config={})
55
+ config ||= {}
56
+ config[:path] ||= name
57
+ define_method(name) do
58
+ instance_variable_get("@#{name}") || \
59
+ instance_variable_set("@#{name}", \
60
+ if (c = child(config))
61
+ config[:klass].constantize.new(c) rescue c
62
+ else
63
+ c
64
+ end
65
+ )
66
+ end
67
+ end
68
+
69
+ def self.has_many(name, config={})
70
+ config ||= {}
71
+ config[:path] ||= name
72
+ define_method(name) do
73
+ instance_variable_get("@#{name}") || \
74
+ instance_variable_set("@#{name}", \
75
+ if ((c = child(config)) && c.respond_to?(:collect))
76
+ c.collect do |item|
77
+ config[:klass].constantize.new(item) rescue item
78
+ end
79
+ else
80
+ c
81
+ end
82
+ )
83
+ end
84
+ end
85
+
86
+ def self.belongs_to(name, config={})
87
+ config ||= {}
88
+ config[:id] ||= "#{name}_id"
89
+ define_method(name) do
90
+ instance_variable_get("@#{name}") || \
91
+ instance_variable_set("@#{name}", \
92
+ if ((k = config[:klass].constantize) && k.respond_to?(:find))
93
+ self.respond_to?(config[:id]) ? k.find(self.send(config[:id])) : nil
94
+ else
95
+ nil
96
+ end
97
+ )
42
98
  end
43
99
  end
44
100
 
@@ -25,22 +25,31 @@ module Resourceful
25
25
  end
26
26
 
27
27
  def initialize(json)
28
- @json = json
28
+ raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Json model with '#{json.class.name}' data" unless json.kind_of?(::Hash)
29
+ @data = json
29
30
  end
30
31
 
31
32
  protected
32
33
 
33
- def self.attribute(name, type, config={})
34
- config[:path] ||= name
35
- super(name, type, config)
36
- end
37
-
38
34
  def attribute(config)
39
- paths = config[:path].to_s.split('/')
40
- paths.inject(@json) { |val,path| val.fetch(path, nil) rescue nil }
35
+ begin
36
+ get_node(config[:path])
37
+ rescue Exception => err
38
+ nil
39
+ end
40
+ end
41
+
42
+ def child(config)
43
+ attribute(config)
41
44
  end
42
45
 
43
-
46
+ def self.get_node(json, path_config)
47
+ paths = path_config.to_s.split('/')
48
+ paths.inject(json) { |val,path| val.fetch(path, nil) rescue nil }
49
+ end
50
+ def get_node(path_config)
51
+ self.class.get_node(@data, path_config)
52
+ end
44
53
  end
45
54
 
46
55
  end
@@ -25,33 +25,40 @@ module Resourceful
25
25
  end
26
26
 
27
27
  def initialize(xml)
28
- @xml = xml
28
+ 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
+ @data = xml
29
30
  end
30
31
 
31
32
  protected
32
33
 
33
- def self.attribute(name, type, config={})
34
- config[:path] ||= name
35
- super(name, type, config)
36
- end
37
-
38
34
  def attribute(config)
39
- node = get_node("./#{config[:path]}")
40
- node.content rescue nil
35
+ begin
36
+ get_node("./#{config[:path]}").content
37
+ rescue Exception => err
38
+ nil
39
+ end
40
+ end
41
+
42
+ def child(config)
43
+ begin
44
+ get_node("./#{config[:path]}")
45
+ rescue Exception => err
46
+ nil
47
+ end
41
48
  end
42
49
 
43
50
  def self.get_node(xml, path)
44
51
  xml.xpath(path.to_s).first
45
52
  end
46
53
  def get_node(path)
47
- self.class.get_node(@xml, path)
54
+ self.class.get_node(@data, path)
48
55
  end
49
56
 
50
57
  def self.xml_root_name(xml)
51
58
  xml.root.name
52
59
  end
53
60
  def xml_root_name
54
- self.class.xml_root_name(@xml)
61
+ self.class.xml_root_name(@data)
55
62
  end
56
63
 
57
64
  end
@@ -2,7 +2,7 @@ module Resourceful
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 3
5
+ MINOR = 4
6
6
  TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
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.3.0
4
+ version: 0.4.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-07-24 00:00:00 -07:00
12
+ date: 2009-07-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency