couchmodel 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
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.4"
13
- specification.date = "2010-07-12"
12
+ specification.version = "0.1.5"
13
+ specification.date = "2010-07-15"
14
14
 
15
15
  specification.authors = [ "Philipp Bruell" ]
16
16
  specification.email = "b.phifty@gmail.com"
@@ -78,6 +78,21 @@ module CouchModel
78
78
  @changed_attributes = { }
79
79
  end
80
80
 
81
+ def merge_multiparameter_attributes(attributes)
82
+ result = attributes.stringify_keys
83
+ self.class.key_definitions.each do |key, definition|
84
+ case definition[:type]
85
+ when :date
86
+ parameters = attributes.values_at(*(1..3).map{ |index| "#{key}(#{index}i)" }).map(&:to_i)
87
+ result[key] = Date.civil *parameters rescue nil unless result[key].is_a?(Date)
88
+ when :time
89
+ parameters = attributes.values_at(*(1..6).map{ |index| "#{key}(#{index}i)" }).map(&:to_i)
90
+ result[key] = Time.mktime *parameters rescue nil unless result[key].is_a?(Time)
91
+ end
92
+ end
93
+ result
94
+ end
95
+
81
96
  class << self
82
97
 
83
98
  alias key_accessor_without_dirty key_accessor
@@ -31,12 +31,13 @@ module CouchModel
31
31
  @attributes = { Configuration::CLASS_KEY => klass.to_s }
32
32
  self.attributes = attributes
33
33
 
34
- klass.defaults.each do |key, value|
35
- @attributes[key] = value unless @attributes.has_key?(key)
36
- end
34
+ klass.key_definitions.each do |key, definition|
35
+ @attributes[key] = definition[:default] if definition.has_key?(:default) && !@attributes.has_key?(key)
36
+ end
37
37
  end
38
38
 
39
39
  def attributes=(attributes)
40
+ attributes = merge_multiparameter_attributes attributes
40
41
  attributes.each { |key, value| self.send :"#{key}=", value if self.respond_to?(:"#{key}=") }
41
42
  end
42
43
 
@@ -96,7 +97,7 @@ module CouchModel
96
97
  private
97
98
 
98
99
  def rev=(value)
99
- @attributes["_rev"] = value
100
+ @attributes["_rev"] = value
100
101
  end
101
102
 
102
103
  def load_response(response)
@@ -130,15 +131,6 @@ module CouchModel
130
131
  raise error
131
132
  end
132
133
 
133
- def self.set_default(key, value)
134
- @defaults ||= { }
135
- @defaults[key.to_s] = value
136
- end
137
-
138
- def self.defaults
139
- @defaults || { }
140
- end
141
-
142
134
  def self.create(*arguments)
143
135
  model = new *arguments
144
136
  model.save ? model : nil
@@ -152,4 +144,4 @@ module CouchModel
152
144
 
153
145
  end
154
146
 
155
- end
147
+ end
@@ -15,20 +15,22 @@ module CouchModel
15
15
 
16
16
  module ClassMethods
17
17
 
18
+ attr_reader :key_definitions
19
+
18
20
  def key_reader(key, options = { })
19
21
  raise ArgumentError, "method #{key} is already defined" if method_defined?(:"#{key}")
20
- default, type = options.values_at :default, :type
21
- set_default key, default if default
22
- send :"define_#{type || :string}_reader", key
22
+ set_key_definition key, options
23
+ type = options[:type] || :string
24
+ send :"define_#{type}_reader", key
23
25
  rescue NoMethodError
24
26
  raise ArgumentError, "type #{type} isn't supported"
25
27
  end
26
28
 
27
29
  def key_writer(key, options = { })
28
30
  raise ArgumentError, "method #{key}= is already defined" if method_defined?(:"#{key}=")
29
- default, type = options.values_at :default, :type
30
- set_default key, default if default
31
- send :"define_#{type || :string}_writer", key
31
+ set_key_definition key, options
32
+ type = options[:type] || :string
33
+ send :"define_#{type}_writer", key
32
34
  rescue NoMethodError
33
35
  raise ArgumentError, "type #{type} isn't supported"
34
36
  end
@@ -40,6 +42,11 @@ module CouchModel
40
42
 
41
43
  private
42
44
 
45
+ def set_key_definition(key, definition)
46
+ @key_definitions ||= { }
47
+ @key_definitions[key.to_s] = definition
48
+ end
49
+
43
50
  def define_integer_reader(name)
44
51
  define_method :"#{name}" do
45
52
  @attributes[name.to_s].to_i
@@ -86,7 +93,7 @@ module CouchModel
86
93
 
87
94
  def define_time_writer(name)
88
95
  define_method :"#{name}=" do |value|
89
- @attributes[name.to_s] = value ? value.strftime("%Y-%m-%d %H:%M:%S %z") : nil
96
+ @attributes[name.to_s] = value.is_a?(Time) ? value.strftime("%Y-%m-%d %H:%M:%S %z") : value
90
97
  end
91
98
  end
92
99
 
@@ -14,8 +14,9 @@ class User < CouchModel::Base
14
14
  setup_database DATABASE
15
15
 
16
16
  key_accessor :username
17
- key_accessor :email, :default => "no email"
17
+ key_accessor :email, :default => "no email"
18
18
  key_accessor :birthday, :type => :date
19
+ key_accessor :lunch, :type => :time
19
20
 
20
21
  has_many :memberships,
21
22
  :class_name => "Membership",
@@ -35,8 +36,24 @@ class Membership < CouchModel::Base
35
36
  end
36
37
 
37
38
  def create_users_and_memberships
38
- @user_one = User.create :username => "user one", :birthday => Date.parse("2000-07-07")
39
- @user_two = User.create :username => "user two", :birthday => nil
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
39
+ @user_one = User.create :username => "user one",
40
+ :birthday => Date.parse("2000-07-07"),
41
+ :lunch => Time.parse("2010/10/21 12:13:14")
42
+
43
+ @user_two = User.create :username => "user two",
44
+ "birthday(1i)" => "2010",
45
+ "birthday(2i)" => "2",
46
+ "birthday(3i)" => "20",
47
+ "lunch(1i)" => "2010",
48
+ "lunch(2i)" => "10",
49
+ "lunch(3i)" => "21",
50
+ "lunch(4i)" => "12",
51
+ "lunch(5i)" => "13",
52
+ "lunch(6i)" => "14"
53
+
54
+ @membership_one = Membership.create :created_at => Time.parse("2010-07-07"),
55
+ :user => @user_one
56
+
57
+ @membership_two = Membership.create :created_at => Time.parse("2010-07-07"),
58
+ :user => @user_two
42
59
  end
@@ -6,7 +6,16 @@ describe "integration" do
6
6
  use_real_transport!
7
7
 
8
8
  before :each do
9
- @user = User.new :username => "user"
9
+ @user = User.new "username" => "user",
10
+ "birthday(1i)" => "2010",
11
+ "birthday(2i)" => "3",
12
+ "birthday(3i)" => "15",
13
+ "lunch(1i)" => "2010",
14
+ "lunch(2i)" => "10",
15
+ "lunch(3i)" => "21",
16
+ "lunch(4i)" => "12",
17
+ "lunch(5i)" => "13",
18
+ "lunch(6i)" => "14"
10
19
  end
11
20
 
12
21
  describe "setup" do
@@ -29,6 +38,22 @@ describe "integration" do
29
38
 
30
39
  end
31
40
 
41
+ describe "birthday" do
42
+
43
+ it "should return the correct date" do
44
+ @user.birthday.should == Date.parse("2010/03/15")
45
+ end
46
+
47
+ end
48
+
49
+ describe "lunch" do
50
+
51
+ it "should return the correct time" do
52
+ @user.lunch.should == Time.parse("2010/10/21 12:13:14")
53
+ end
54
+
55
+ end
56
+
32
57
  describe "save" do
33
58
 
34
59
  it "should create the model" do
@@ -70,11 +70,24 @@ describe "integration" do
70
70
 
71
71
  describe "birthday" do
72
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
- @user_two.birthday.should be_nil
73
+ it "should return the correct date" do
74
+ @user_one.reload
75
+ @user_one.birthday.should == Date.parse("2000/07/07")
76
+
77
+ @user_two.reload
78
+ @user_two.birthday.should == Date.parse("2010/02/20")
79
+ end
80
+
81
+ end
82
+
83
+ describe "lunch" do
84
+
85
+ it "should return the correct time" do
86
+ @user_one.reload
87
+ @user_one.lunch.should == Time.parse("2010/10/21 12:13:14")
88
+
89
+ @user_two.reload
90
+ @user_two.lunch.should == Time.parse("2010/10/21 12:13:14")
78
91
  end
79
92
 
80
93
  end
@@ -7,6 +7,8 @@ class ActiveTestModel < CouchModel::Base
7
7
 
8
8
  key_accessor :name
9
9
  key_accessor :email
10
+ key_accessor :date, :type => :date
11
+ key_accessor :time, :type => :time
10
12
 
11
13
  validates_presence_of :name
12
14
 
@@ -125,6 +127,31 @@ describe ActiveTestModel do
125
127
 
126
128
  end
127
129
 
130
+ describe "attributes=" do
131
+
132
+ it "should convert multiple date parameters into one date field" do
133
+ @model.attributes = {
134
+ "date(1i)" => "2010",
135
+ "date(2i)" => "3",
136
+ "date(3i)" => "15"
137
+ }
138
+ @model.date.should == Date.parse("2010/03/15")
139
+ end
140
+
141
+ it "should convert multiple time parameters into one time field" do
142
+ @model.attributes = {
143
+ "time(1i)" => "2010",
144
+ "time(2i)" => "3",
145
+ "time(3i)" => "15",
146
+ "time(4i)" => "10",
147
+ "time(5i)" => "11",
148
+ "time(6i)" => "12"
149
+ }
150
+ @model.time.should == Time.parse("2010/03/15 10:11:12")
151
+ end
152
+
153
+ end
154
+
128
155
  describe "save" do
129
156
 
130
157
  before :each do
@@ -81,7 +81,7 @@ describe AccessorTestModel do
81
81
  end
82
82
 
83
83
  it "should set a default value" do
84
- AccessorTestModel.defaults["test_two"].should == "test default"
84
+ AccessorTestModel.key_definitions["test_two"].should == { :default => "test default" }
85
85
  end
86
86
 
87
87
  it "should raise an ArgumentError on unsupported type" do
@@ -103,11 +103,19 @@ describe AccessorTestModel do
103
103
  it "should define writers for date types" do
104
104
  @model.test_date = Date.parse("2010-07-07")
105
105
  @model.attributes["test_date"].should == "2010-07-07"
106
+
107
+ @model.test_date = "2010-07-07"
108
+ @model.attributes["test_date"].should == "2010-07-07"
109
+ @model.test_date.should == Date.parse("2010-07-07")
106
110
  end
107
111
 
108
112
  it "should define writers for time types" do
109
113
  @model.test_time = Time.parse("2010-07-07 10:10:10")
110
114
  @model.attributes["test_time"].should == "2010-07-07 10:10:10 +0200"
115
+
116
+ @model.test_time = "2010-07-07 10:10:10"
117
+ @model.attributes["test_time"].should == "2010-07-07 10:10:10"
118
+ @model.test_time.should == Time.parse("2010-07-07 10:10:10")
111
119
  end
112
120
 
113
121
  it "should raise an exception if the writer method is already defined" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
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-07-12 00:00:00 +02:00
17
+ date: 2010-07-15 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency