couchmodel 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -38,6 +38,7 @@ To define a model, it's necessary to create a subclass of <tt>CouchModel::Base</
38
38
  key_accessor :name
39
39
  key_accessor :email
40
40
  key_accessor :language, :default => "en"
41
+ key_accessor :birthday, :type => :date
41
42
 
42
43
  end
43
44
 
@@ -52,7 +53,8 @@ manually by calling <tt>CouchModel::Configuration.setup_databases</tt> and
52
53
 
53
54
  The method <tt>key_accessor</tt> defined access methods to the given keys of the CouchDB document. It's also possible
54
55
  to use <tt>key_reader</tt> and <tt>key_writer</tt> here. If the <tt>:default</tt> option is passed, the key will get
55
- a default value assigned during initialization of the class.
56
+ a default value assigned during initialization of the class. The <tt>:type</tt> option specifies the type of the
57
+ attribute. Currently the types <tt>:integer</tt>, <tt>:string</tt>, <tt>:date</tt> and <tt>:time</tt> are supported.
56
58
 
57
59
  == Design documents
58
60
 
data/Rakefile CHANGED
@@ -9,8 +9,8 @@ task :default => :spec
9
9
 
10
10
  specification = Gem::Specification.new do |specification|
11
11
  specification.name = "couchmodel"
12
- specification.version = "0.1.2"
13
- specification.date = "2010-06-12"
12
+ specification.version = "0.1.3"
13
+ specification.date = "2010-07-09"
14
14
 
15
15
  specification.authors = [ "Philipp Bruell" ]
16
16
  specification.email = "b.phifty@gmail.com"
@@ -1,3 +1,4 @@
1
+ require 'time'
1
2
 
2
3
  module CouchModel
3
4
 
@@ -16,18 +17,20 @@ module CouchModel
16
17
 
17
18
  def key_reader(key, options = { })
18
19
  raise ArgumentError, "method #{key} is already defined" if method_defined?(:"#{key}")
19
- set_default key, options[:default] if options.has_key?(:default)
20
- define_method :"#{key}" do
21
- @attributes[key.to_s]
22
- end
20
+ default, type = options.values_at :default, :type
21
+ set_default key, default if default
22
+ send :"define_#{type || :string}_reader", key
23
+ rescue NoMethodError
24
+ raise ArgumentError, "type #{type} isn't supported"
23
25
  end
24
26
 
25
27
  def key_writer(key, options = { })
26
28
  raise ArgumentError, "method #{key}= is already defined" if method_defined?(:"#{key}=")
27
- set_default key, options[:default] if options.has_key?(:default)
28
- define_method :"#{key}=" do |value|
29
- @attributes[key.to_s] = value
30
- end
29
+ default, type = options.values_at :default, :type
30
+ set_default key, default if default
31
+ send :"define_#{type || :string}_writer", key
32
+ rescue NoMethodError
33
+ raise ArgumentError, "type #{type} isn't supported"
31
34
  end
32
35
 
33
36
  def key_accessor(*arguments)
@@ -35,6 +38,56 @@ module CouchModel
35
38
  key_writer *arguments
36
39
  end
37
40
 
41
+ private
42
+
43
+ def define_integer_reader(name)
44
+ define_method :"#{name}" do
45
+ @attributes[name.to_s].to_i
46
+ end
47
+ end
48
+
49
+ def define_integer_writer(name)
50
+ define_method :"#{name}=" do |value|
51
+ @attributes[name.to_s] = value.to_i
52
+ end
53
+ end
54
+
55
+ def define_string_reader(name)
56
+ define_method :"#{name}" do
57
+ @attributes[name.to_s]
58
+ end
59
+ end
60
+
61
+ def define_string_writer(name)
62
+ define_method :"#{name}=" do |value|
63
+ @attributes[name.to_s] = value
64
+ end
65
+ end
66
+
67
+ def define_date_reader(name)
68
+ define_method :"#{name}" do
69
+ Date.parse @attributes[name.to_s]
70
+ end
71
+ end
72
+
73
+ def define_date_writer(name)
74
+ define_method :"#{name}=" do |value|
75
+ @attributes[name.to_s] = value.to_s
76
+ end
77
+ end
78
+
79
+ def define_time_reader(name)
80
+ define_method :"#{name}" do
81
+ Time.parse @attributes[name.to_s]
82
+ end
83
+ end
84
+
85
+ def define_time_writer(name)
86
+ define_method :"#{name}=" do |value|
87
+ @attributes[name.to_s] = value.strftime("%Y-%m-%d %H:%M:%S %z")
88
+ end
89
+ end
90
+
38
91
  end
39
92
 
40
93
  end
@@ -39,7 +39,7 @@ module CouchModel
39
39
  define_method :"#{reader_name}" do
40
40
  klass = Object.const_get class_name
41
41
  klass.find self.send(key)
42
- end
42
+ end
43
43
  end
44
44
 
45
45
  def define_belongs_to_writer(writer_name, class_name, key)
@@ -31,8 +31,8 @@ describe "integration" do
31
31
  end
32
32
 
33
33
  it "should use the selector" do
34
- @user_one.memberships("yesterday").should include(@membership_one)
35
- @user_one.memberships("today").should_not include(@membership_one)
34
+ @user_one.memberships(Time.parse("2010-07-07").strftime("%Y-%m-%d %H:%M:%S %z")).should include(@membership_one)
35
+ @user_one.memberships(Time.parse("2010-07-08").strftime("%Y-%m-%d %H:%M:%S %z")).should_not include(@membership_one)
36
36
  end
37
37
 
38
38
  end
@@ -15,6 +15,7 @@ class User < CouchModel::Base
15
15
 
16
16
  key_accessor :username
17
17
  key_accessor :email, :default => "no email"
18
+ key_accessor :birthday, :type => :date
18
19
 
19
20
  has_many :memberships,
20
21
  :class_name => "Membership",
@@ -27,15 +28,15 @@ class Membership < CouchModel::Base
27
28
 
28
29
  setup_database DATABASE
29
30
 
30
- key_accessor :created_at
31
+ key_accessor :created_at, :type => :time
31
32
 
32
33
  belongs_to :user, :class_name => "User"
33
34
 
34
35
  end
35
36
 
36
37
  def create_users_and_memberships
37
- @user_one = User.create :username => "user one"
38
- @user_two = User.create :username => "user two"
39
- @membership_one = Membership.create :created_at => "yesterday", :user => @user_one
40
- @membership_two = Membership.create :created_at => "yesterday", :user => @user_two
38
+ @user_one = User.create :username => "user one", :birthday => Date.parse("2000-07-07")
39
+ @user_two = User.create :username => "user two", :birthday => Date.parse("2000-07-07")
40
+ @membership_one = Membership.create :created_at => Time.parse("2010-07-07"), :user => @user_one
41
+ @membership_two = Membership.create :created_at => Time.parse("2010-07-07"), :user => @user_two
41
42
  end
@@ -50,12 +50,7 @@ describe "integration" do
50
50
  describe "all" do
51
51
 
52
52
  it "should include the saved user" do
53
- begin
54
- User.all.should include(@user_one)
55
- rescue Object => error
56
- puts error.backtrace
57
- raise error
58
- end
53
+ User.all.should include(@user_one)
59
54
  User.all.should include(@user_two)
60
55
  end
61
56
 
@@ -73,4 +68,14 @@ describe "integration" do
73
68
 
74
69
  end
75
70
 
71
+ describe "birthday" do
72
+
73
+ it "should return a time" do
74
+ @user_one.birthday.should be_instance_of(Date)
75
+ user = User.find @user_one.id
76
+ user.birthday.should be_instance_of(Date)
77
+ end
78
+
79
+ end
80
+
76
81
  end
@@ -8,6 +8,12 @@ class AccessorTestModel < CouchModel::Base
8
8
  key_reader :test_one, :default => "test default"
9
9
  key_writer :test_two, :default => "test default"
10
10
  key_accessor :test_three, :default => "test default"
11
+
12
+ key_accessor :test_integer, :type => :integer
13
+ key_accessor :test_string, :type => :string
14
+ key_accessor :test_date, :type => :date
15
+ key_accessor :test_time, :type => :time
16
+
11
17
  end
12
18
 
13
19
  describe AccessorTestModel do
@@ -30,7 +36,33 @@ describe AccessorTestModel do
30
36
  @model.test_one.should == "test default"
31
37
  end
32
38
 
33
- it "should raise an exception if the reader method is already defined" do
39
+ it "should raise an ArgumentError on unsupported type" do
40
+ lambda do
41
+ AccessorTestModel.key_reader :test, :type => :unsupported
42
+ end.should raise_error(ArgumentError)
43
+ end
44
+
45
+ it "should define readers for integer types" do
46
+ @model.test_integer = 3
47
+ @model.test_integer.should be_instance_of(Fixnum)
48
+ end
49
+
50
+ it "should define readers for string types" do
51
+ @model.test_string = "3"
52
+ @model.test_string.should be_instance_of(String)
53
+ end
54
+
55
+ it "should define readers for date types" do
56
+ @model.test_date = Date.today
57
+ @model.test_date.should be_instance_of(Date)
58
+ end
59
+
60
+ it "should define readers for time types" do
61
+ @model.test_time = Time.now
62
+ @model.test_time.should be_instance_of(Time)
63
+ end
64
+
65
+ it "should raise an ArgumentError if the reader method is already defined" do
34
66
  lambda do
35
67
  AccessorTestModel.key_reader :test_one
36
68
  end.should raise_error(ArgumentError)
@@ -52,6 +84,32 @@ describe AccessorTestModel do
52
84
  AccessorTestModel.defaults["test_two"].should == "test default"
53
85
  end
54
86
 
87
+ it "should raise an ArgumentError on unsupported type" do
88
+ lambda do
89
+ AccessorTestModel.key_writer :test, :type => :unsupported
90
+ end.should raise_error(ArgumentError)
91
+ end
92
+
93
+ it "should define writers for integer types" do
94
+ @model.test_integer = 3
95
+ @model.attributes["test_integer"].should == 3
96
+ end
97
+
98
+ it "should define writers for string types" do
99
+ @model.test_string = "3"
100
+ @model.attributes["test_string"].should == "3"
101
+ end
102
+
103
+ it "should define writers for date types" do
104
+ @model.test_date = Date.parse("2010-07-07")
105
+ @model.attributes["test_date"].should == "2010-07-07"
106
+ end
107
+
108
+ it "should define writers for time types" do
109
+ @model.test_time = Time.parse("2010-07-07 10:10:10")
110
+ @model.attributes["test_time"].should == "2010-07-07 10:10:10 +0200"
111
+ end
112
+
55
113
  it "should raise an exception if the writer method is already defined" do
56
114
  lambda do
57
115
  AccessorTestModel.key_writer :test_two
@@ -76,4 +134,4 @@ describe AccessorTestModel do
76
134
 
77
135
  end
78
136
 
79
- end
137
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Philipp Bruell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-12 00:00:00 +02:00
17
+ date: 2010-07-09 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency