kelredd-resourceful 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ module Resourceful
2
+ module Model
3
+ module AttributeTypes
4
+
5
+ module ClassMethods
6
+
7
+ ATTRIBUTE_TYPES = {
8
+ :string => { :method => 'to_s', :kind => ::String },
9
+ :text => { :method => 'to_s', :kind => ::String },
10
+ :integer => { :method => 'to_i', :kind => ::Fixnum },
11
+ :float => { :method => 'to_f', :kind => ::Float },
12
+ :currency => { :method => 'from_currency_to_f', :kind => ::Float },
13
+ :date => { :method => 'to_date', :kind => ::Date },
14
+ :datetime => { :method => 'to_datetime', :kind => ::DateTime },
15
+ :boolean => { :method => 'to_resourceful_boolean', :kind => ::Object },
16
+ :other => { :method => 'to_s', :kind => ::String }
17
+ }
18
+
19
+ def attribute_type_to_method(type)
20
+ (ATTRIBUTE_TYPES.has_key?(type.to_sym) ? ATTRIBUTE_TYPES[type.to_sym] : ATTRIBUTE_TYPES[:other])[:method]
21
+ end
22
+
23
+ def attribute_type_to_kind(type)
24
+ (ATTRIBUTE_TYPES.has_key?(type.to_sym) ? ATTRIBUTE_TYPES[type.to_sym] : ATTRIBUTE_TYPES[:other])[:kind]
25
+ end
26
+
27
+ end
28
+
29
+ module InstanceMethods
30
+ end
31
+
32
+ def self.included(receiver)
33
+ receiver.extend ClassMethods
34
+ receiver.send :include, InstanceMethods
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -1,7 +1,10 @@
1
+ require 'resourceful/model/attribute_types'
2
+
1
3
  module Resourceful
2
4
  module Model
3
5
 
4
6
  class Base
7
+ include Resourceful::Model::AttributeTypes
5
8
 
6
9
  @@agent ||= {}
7
10
  def self.agent(&block)
@@ -92,24 +95,7 @@ module Resourceful
92
95
  add_to_attributes(name.to_s)
93
96
  config ||= {}
94
97
  config[:path] ||= clean_name
95
- content_method = case type.to_sym
96
- when :string
97
- 'to_s'
98
- when :integer
99
- 'to_i'
100
- when :float
101
- 'to_f'
102
- when :currency
103
- 'from_currency_to_f'
104
- when :date
105
- 'to_date'
106
- when :datetime
107
- 'to_datetime'
108
- when :boolean
109
- 'to_resourceful_boolean'
110
- else
111
- 'to_s'
112
- end
98
+ content_method = attribute_type_to_method(type)
113
99
  # method to get the raw attribute variable data (not intended to be overridden)
114
100
  define_method("_#{clean_name}") do
115
101
  fetch_attribute(clean_name, config, content_method)
@@ -5,10 +5,12 @@ module Resourceful
5
5
  module Shoulda
6
6
  module TestUnit
7
7
 
8
+ protected
9
+
8
10
  def should_have_resourceful_attribute(name, opts={})
9
11
  clean_name = ::Resourceful::Model::Base.cleanup_name(name)
10
12
  should_have_instance_methods clean_name, "#{clean_name}=", "_#{clean_name}"
11
- # TODO: Validate this attribute is of type opts[:type] if provided
13
+ should_have_resourceful_typed_attribute(name, opts[:type])
12
14
  end unless Test::Unit::TestCase.method_defined? :should_have_resourceful_attribute
13
15
 
14
16
  def should_have_namespace(ns)
@@ -29,32 +31,71 @@ module Resourceful
29
31
  def should_resourcefully_belong_to(name, opts={})
30
32
  clean_name = ::Resourceful::Model::Base.cleanup_name(name)
31
33
  should_have_instance_methods clean_name
32
- # TODO: Validation :class config present, validation that a namespaced klass is defined
33
- # TODO: Validate it returns something of that klass
34
+ should_have_resourceful_association_item(name, opts)
34
35
  end
35
36
 
36
37
  def should_resourcefully_have_many(name, opts={})
37
38
  clean_name = ::Resourceful::Model::Base.cleanup_name(name)
38
39
  should_have_instance_methods clean_name
39
- # TODO: Validation :class config present, validation that a namespaced klass is defined
40
- # TODO: Validate returns an array of items of that klass
40
+ should_have_resourceful_association_collection(name, opts)
41
41
  end
42
42
 
43
43
  def should_resourcefully_contain_one(name, opts={})
44
44
  clean_name = ::Resourceful::Model::Base.cleanup_name(name)
45
45
  should_have_instance_methods clean_name
46
- # TODO: Validation :class config present, validation that a namespaced klass is defined
47
- # TODO: Validate it returns something of that klass
46
+ should_have_resourceful_association_item(name, opts)
48
47
  end
49
48
 
50
49
  def should_resourcefully_contain_many(name, opts={})
51
50
  clean_name = ::Resourceful::Model::Base.cleanup_name(name)
52
51
  should_have_instance_methods clean_name
53
- # TODO: Validation :class config present, validation that a namespaced klass is defined
54
- # TODO: Validate returns an array of items of that klass
52
+ should_have_resourceful_association_collection(name, opts)
55
53
  end
56
54
 
57
- protected
55
+
56
+
57
+ # Helpers
58
+
59
+ def should_have_resourceful_typed_attribute(name, type)
60
+ if type
61
+ should "have a #{name} resourceful #{type} value" do
62
+ if subject && (val = subject.send(name))
63
+ assert_kind_of Resourceful::Model::Base.attribute_type_to_kind(type), val, "#{name} does not have a #{Resourceful::Model::Base.attribute_type_to_kind(type)} kind of value."
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ def should_have_resourceful_association_item(name, opts)
70
+ klass = described_type
71
+ should_have_resourceful_association_class(opts)
72
+ should "return an item that is a kind of #{opts[:class]}" do
73
+ if subject && (val = subject.send(name))
74
+ assert_kind_of klass.get_namespaced_klass(opts[:class]), val, "#{name} should be a kind of #{opts[:class]}"
75
+ end
76
+ end
77
+ end
78
+
79
+ def should_have_resourceful_association_collection(name, opts)
80
+ klass = described_type
81
+ should_have_resourceful_association_class(opts)
82
+ should "return a collection of items that are a kind of #{opts[:class]}" do
83
+ if subject && (val = subject.send(name))
84
+ assert_kind_of ::Array, val, "#{name} should be a kind of Array"
85
+ if (first_item = val.first)
86
+ assert_kind_of klass.get_namespaced_klass(opts[:class]), first_item, "#{name} first item should be a kind of #{opts[:class]}"
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ def should_have_resourceful_association_class(opts)
93
+ klass = described_type
94
+ should "have a #{opts[:class]} class specified that is defined" do
95
+ assert opts[:class], "resourceful associations require a :class option"
96
+ assert klass.get_namespaced_klass(opts[:class]), "a namespaced #{opts[:class]} is not defined"
97
+ end
98
+ end
58
99
 
59
100
  # Ripped from Shoulda::ActiveRecord::Macros
60
101
  def should_have_instance_methods(*methods)
@@ -76,7 +117,7 @@ module Resourceful
76
117
  assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
77
118
  end
78
119
  end
79
- end
120
+ end unless Test::Unit::TestCase.method_defined? :should_have_class_methods
80
121
 
81
122
  end
82
123
  end
@@ -3,7 +3,7 @@ module Resourceful
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 7
6
- TINY = 1
6
+ TINY = 3
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.1
4
+ version: 0.7.3
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-09-11 00:00:00 -07:00
12
+ date: 2009-09-12 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -92,6 +92,7 @@ files:
92
92
  - lib/resourceful/exceptions.rb
93
93
  - lib/resourceful/extensions.rb
94
94
  - lib/resourceful/model
95
+ - lib/resourceful/model/attribute_types.rb
95
96
  - lib/resourceful/model/base.rb
96
97
  - lib/resourceful/model/embedded_associations.rb
97
98
  - lib/resourceful/model/external_associations.rb
@@ -110,6 +111,7 @@ files:
110
111
  - lib/resourceful.rb
111
112
  has_rdoc: false
112
113
  homepage: ""
114
+ licenses:
113
115
  post_install_message:
114
116
  rdoc_options:
115
117
  - --main
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
133
  requirements: []
132
134
 
133
135
  rubyforge_project:
134
- rubygems_version: 1.2.0
136
+ rubygems_version: 1.3.5
135
137
  signing_key:
136
138
  specification_version: 3
137
139
  summary: A ruby gem to abstract web resource handling.