sequel 0.4.4.1 → 0.4.4.2

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.
Files changed (45) hide show
  1. data/CHANGELOG +10 -0
  2. data/Rakefile +161 -159
  3. data/lib/sequel.rb +14 -10
  4. data/lib/sequel/adapters/adapter_skeleton.rb +2 -1
  5. data/lib/sequel/adapters/ado.rb +2 -1
  6. data/lib/sequel/adapters/db2.rb +5 -3
  7. data/lib/sequel/adapters/dbi.rb +2 -1
  8. data/lib/sequel/adapters/informix.rb +2 -1
  9. data/lib/sequel/adapters/jdbc.rb +3 -2
  10. data/lib/sequel/adapters/mysql.rb +268 -264
  11. data/lib/sequel/adapters/odbc.rb +7 -2
  12. data/lib/sequel/adapters/odbc_mssql.rb +1 -1
  13. data/lib/sequel/adapters/openbase.rb +2 -1
  14. data/lib/sequel/adapters/oracle.rb +2 -1
  15. data/lib/sequel/adapters/postgres.rb +32 -16
  16. data/lib/sequel/adapters/sqlite.rb +7 -6
  17. data/lib/sequel/array_keys.rb +295 -295
  18. data/lib/sequel/connection_pool.rb +1 -1
  19. data/lib/sequel/core_sql.rb +14 -5
  20. data/lib/sequel/database.rb +4 -4
  21. data/lib/sequel/dataset.rb +12 -10
  22. data/lib/sequel/dataset/convenience.rb +10 -8
  23. data/lib/sequel/dataset/sequelizer.rb +19 -16
  24. data/lib/sequel/dataset/sql.rb +43 -30
  25. data/lib/sequel/exceptions.rb +45 -0
  26. data/lib/sequel/migration.rb +7 -5
  27. data/lib/sequel/model.rb +1 -1
  28. data/lib/sequel/model/base.rb +3 -3
  29. data/lib/sequel/model/hooks.rb +0 -4
  30. data/lib/sequel/model/record.rb +9 -9
  31. data/lib/sequel/model/relations.rb +2 -2
  32. data/lib/sequel/pretty_table.rb +6 -3
  33. data/lib/sequel/schema/schema_sql.rb +11 -6
  34. data/lib/sequel/worker.rb +8 -7
  35. data/spec/adapters/sqlite_spec.rb +3 -3
  36. data/spec/array_keys_spec.rb +543 -543
  37. data/spec/connection_pool_spec.rb +6 -3
  38. data/spec/database_spec.rb +4 -4
  39. data/spec/dataset_spec.rb +25 -25
  40. data/spec/migration_spec.rb +1 -1
  41. data/spec/model_spec.rb +16 -16
  42. data/spec/sequelizer_spec.rb +7 -7
  43. data/spec/spec.opts +8 -0
  44. metadata +5 -5
  45. data/lib/sequel/error.rb +0 -22
@@ -50,10 +50,12 @@ module Sequel
50
50
  def self.apply(db, direction)
51
51
  obj = new(db)
52
52
  case direction
53
- when :up: obj.up
54
- when :down: obj.down
53
+ when :up
54
+ obj.up
55
+ when :down
56
+ obj.down
55
57
  else
56
- raise SequelError, "Invalid migration direction (#{direction})"
58
+ raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})"
57
59
  end
58
60
  end
59
61
 
@@ -104,8 +106,8 @@ module Sequel
104
106
  # determine current and target version and direction
105
107
  current ||= get_current_migration_version(db)
106
108
  target ||= latest_migration_version(directory)
107
- raise SequelError, "No current version available" if current.nil?
108
- raise SequelError, "No target version available" if target.nil?
109
+ raise Error, "No current version available" if current.nil?
110
+ raise Error, "No target version available" if target.nil?
109
111
 
110
112
  direction = current < target ? :up : :down
111
113
 
data/lib/sequel/model.rb CHANGED
@@ -256,7 +256,7 @@ module Sequel
256
256
  def self.[](*args)
257
257
  args = args.first if (args.size == 1)
258
258
  if args === true || args === false
259
- raise SequelError, "Invalid filter specified. Did you mean to supply a hash?"
259
+ raise Error::InvalidFilter, "Did you mean to supply a hash?"
260
260
  end
261
261
  dataset[(Hash === args) ? args : primary_key_hash(args)]
262
262
  end
@@ -3,7 +3,7 @@ module Sequel
3
3
  # Returns the database associated with the Model class.
4
4
  def self.db
5
5
  @db ||= (superclass != Object) && superclass.db or
6
- raise SequelError, "No database associated with #{self}"
6
+ raise Error, "No database associated with #{self}"
7
7
  end
8
8
 
9
9
  # Sets the database associated with the Model class.
@@ -20,7 +20,7 @@ module Sequel
20
20
  # Returns the dataset associated with the Model class.
21
21
  def self.dataset
22
22
  @dataset || super_dataset or
23
- raise SequelError, "No dataset associated with #{self}"
23
+ raise Error, "No dataset associated with #{self}"
24
24
  end
25
25
 
26
26
  def self.super_dataset # :nodoc:
@@ -32,7 +32,7 @@ module Sequel
32
32
  # See Dataset#columns for more information.
33
33
  def self.columns
34
34
  @columns ||= @dataset.columns or
35
- raise SequelError, "Could not fetch columns for #{self}"
35
+ raise Error, "Could not fetch columns for #{self}"
36
36
  end
37
37
 
38
38
  # Sets the dataset associated with the Model class.
@@ -1,9 +1,5 @@
1
1
  module Sequel
2
2
  class Model
3
-
4
- class ChainBroken < RuntimeError # :nodoc:
5
- end
6
-
7
3
  # This Hash translates verbs to methodnames used in chain manipulation
8
4
  # methods.
9
5
  VERB_TO_METHOD = {:prepend => :unshift, :append => :push}
@@ -87,7 +87,7 @@ module Sequel
87
87
  @pk ||= {key => @values[key]}
88
88
  end
89
89
  class_def(:cache_key) do
90
- pk = @values[key] || (raise SequelError, 'no primary key for this record')
90
+ pk = @values[key] || (raise Error, 'no primary key for this record')
91
91
  @cache_key ||= "#{self.class}:#{pk}"
92
92
  end
93
93
  meta_def(:primary_key_hash) do |v|
@@ -118,11 +118,11 @@ module Sequel
118
118
 
119
119
  def self.no_primary_key #:nodoc:
120
120
  meta_def(:primary_key) {nil}
121
- meta_def(:primary_key_hash) {|v| raise SequelError, "#{self} does not have a primary key"}
122
- class_def(:this) {raise SequelError, "No primary key is associated with this model"}
123
- class_def(:pk) {raise SequelError, "No primary key is associated with this model"}
124
- class_def(:pk_hash) {raise SequelError, "No primary key is associated with this model"}
125
- class_def(:cache_key) {raise SequelError, "No primary key is associated with this model"}
121
+ meta_def(:primary_key_hash) {|v| raise Error, "#{self} does not have a primary key"}
122
+ class_def(:this) {raise Error, "No primary key is associated with this model"}
123
+ class_def(:pk) {raise Error, "No primary key is associated with this model"}
124
+ class_def(:pk_hash) {raise Error, "No primary key is associated with this model"}
125
+ class_def(:cache_key) {raise Error, "No primary key is associated with this model"}
126
126
  end
127
127
 
128
128
  # Creates new instance with values set to passed-in Hash ensuring that
@@ -149,7 +149,7 @@ module Sequel
149
149
 
150
150
  # Returns a key unique to the underlying record for caching
151
151
  def cache_key
152
- pk = @values[:id] || (raise SequelError, 'no primary key for this record')
152
+ pk = @values[:id] || (raise Error, 'no primary key for this record')
153
153
  @cache_key ||= "#{self.class}:#{pk}"
154
154
  end
155
155
 
@@ -251,7 +251,7 @@ module Sequel
251
251
 
252
252
  # Reloads values from database and returns self.
253
253
  def refresh
254
- @values = this.first || raise(SequelError, "Record not found")
254
+ @values = this.first || raise(Error, "Record not found")
255
255
  self
256
256
  end
257
257
 
@@ -285,7 +285,7 @@ module Sequel
285
285
  end
286
286
 
287
287
  # otherwise, raise an error
288
- raise SequelError, "Invalid column (#{att.inspect}) for #{self}"
288
+ raise Error, "Invalid column (#{att.inspect}) for #{self}"
289
289
  end
290
290
 
291
291
  # define the column accessor
@@ -35,7 +35,7 @@ module Sequel
35
35
  end
36
36
 
37
37
  from = opts[:from]
38
- from || (raise SequelError, "No association source defined (use :from option)")
38
+ from || (raise Error, "No association source defined (use :from option)")
39
39
  key = opts[:key] || (name.to_s + ID_POSTFIX).to_sym
40
40
 
41
41
  setter_name = "#{name}=".to_sym
@@ -93,7 +93,7 @@ module Sequel
93
93
 
94
94
 
95
95
  from = opts[:from]
96
- from || (raise SequelError, "No association source defined (use :from option)")
96
+ from || (raise Error, "No association source defined (use :from option)")
97
97
  key = opts[:key] || (self.to_s + ID_POSTFIX).to_sym
98
98
 
99
99
  case from
@@ -41,9 +41,12 @@ module Sequel
41
41
 
42
42
  def self.format_cell(size, v)
43
43
  case v
44
- when Bignum, Fixnum: "%#{size}d" % v
45
- when Float: "%#{size}g" % v
46
- else "%-#{size}s" % v.to_s
44
+ when Bignum, Fixnum
45
+ "%#{size}d" % v
46
+ when Float
47
+ "%#{size}g" % v
48
+ else
49
+ "%-#{size}s" % v.to_s
47
50
  end
48
51
  end
49
52
 
@@ -9,11 +9,16 @@ module Sequel
9
9
 
10
10
  def on_delete_clause(action)
11
11
  case action
12
- when :restrict: RESTRICT
13
- when :cascade: CASCADE
14
- when :set_null: SET_NULL
15
- when :set_default: SET_DEFAULT
16
- else NO_ACTION
12
+ when :restrict
13
+ RESTRICT
14
+ when :cascade
15
+ CASCADE
16
+ when :set_null
17
+ SET_NULL
18
+ when :set_default
19
+ SET_DEFAULT
20
+ else
21
+ NO_ACTION
17
22
  end
18
23
  end
19
24
 
@@ -115,7 +120,7 @@ module Sequel
115
120
  when :drop_index
116
121
  "DROP INDEX #{default_index_name(table, op[:columns])}"
117
122
  else
118
- raise SequelError, "Unsupported ALTER TABLE operation"
123
+ raise Error, "Unsupported ALTER TABLE operation"
119
124
  end
120
125
  end
121
126
  end
data/lib/sequel/worker.rb CHANGED
@@ -1,9 +1,9 @@
1
- require 'thread'
1
+ require "thread"
2
2
 
3
3
  module Sequel
4
+
4
5
  class Worker < Thread
5
- class WorkerStopError < RuntimeError; end
6
-
6
+
7
7
  attr_reader :queue
8
8
  attr_reader :errors
9
9
 
@@ -18,7 +18,7 @@ module Sequel
18
18
 
19
19
  def work
20
20
  loop {next_job}
21
- rescue WorkerStopError # signals the worker thread to stop
21
+ rescue Sequel::Error::WorkerStop # signals the worker thread to stop
22
22
  ensure
23
23
  rollback! if @transaction && !@errors.empty?
24
24
  end
@@ -38,7 +38,7 @@ module Sequel
38
38
  while busy?
39
39
  sleep 0.1
40
40
  end
41
- self.raise WorkerStopError
41
+ self.raise Error::WorkerStop
42
42
  super
43
43
  end
44
44
 
@@ -46,7 +46,7 @@ module Sequel
46
46
  def next_job
47
47
  @cur = @queue.pop
48
48
  @cur.call
49
- rescue WorkerStopError => e
49
+ rescue Error::WorkerStop => e
50
50
  raise e
51
51
  rescue Exception => e
52
52
  @errors << e
@@ -54,4 +54,5 @@ module Sequel
54
54
  @cur = nil
55
55
  end
56
56
  end
57
- end
57
+
58
+ end
@@ -41,7 +41,7 @@ context "An SQLite database" do
41
41
  @db.auto_vacuum = :none
42
42
  @db.auto_vacuum.should == :none
43
43
 
44
- proc {@db.auto_vacuum = :invalid}.should raise_error(SequelError)
44
+ proc {@db.auto_vacuum = :invalid}.should raise_error(Sequel::Error)
45
45
  end
46
46
 
47
47
  specify "should support getting and setting the synchronous pragma" do
@@ -52,7 +52,7 @@ context "An SQLite database" do
52
52
  @db.synchronous = :full
53
53
  @db.synchronous.should == :full
54
54
 
55
- proc {@db.synchronous = :invalid}.should raise_error(SequelError)
55
+ proc {@db.synchronous = :invalid}.should raise_error(Sequel::Error)
56
56
  end
57
57
 
58
58
  specify "should support getting and setting the temp_store pragma" do
@@ -63,7 +63,7 @@ context "An SQLite database" do
63
63
  @db.temp_store = :memory
64
64
  @db.temp_store.should == :memory
65
65
 
66
- proc {@db.temp_store = :invalid}.should raise_error(SequelError)
66
+ proc {@db.temp_store = :invalid}.should raise_error(Sequel::Error)
67
67
  end
68
68
 
69
69
  specify "should be able to execute multiple statements at once" do
@@ -1,544 +1,544 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
2
-
3
- context "An array with symbol keys" do
4
- setup do
5
- @a = [1, 2, 3]
6
- @a.keys = [:a, :b, :c]
7
- end
8
-
9
- specify "should provide subscript access" do
10
- @a[0].should == 1
11
- @a[0..1].should == [1, 2]
12
-
13
- @a[1] = 4
14
- @a.should == [1, 4, 3]
15
- end
16
-
17
- specify "should provide key access using symbols" do
18
- @a[:a].should == 1
19
- @a[:b].should == 2
20
- @a[:B].should == nil
21
-
22
- @a[:a] = 11
23
- @a.should == [11, 2, 3]
24
- @a[:a].should == 11
25
-
26
- @a[:d] = 4
27
- @a.should == [11, 2, 3, 4]
28
- @a.keys.should == [:a, :b, :c, :d]
29
- end
30
-
31
- specify "should provide key access using strings" do
32
- @a['a'].should == 1
33
- @a['A'].should be_nil
34
-
35
- @a['d'] = 4
36
- @a.should == [1, 2, 3, 4]
37
- @a.keys.should == [:a, :b, :c, :d]
38
- end
39
-
40
- specify "should provide #store functionality" do
41
- @a.store(:a, 11)
42
- @a.should == [11, 2, 3]
43
-
44
- @a.store(:d, 4)
45
- @a.should == [11, 2, 3, 4]
46
-
47
- @a.store('d', 44)
48
- @a.should == [11, 2, 3, 44]
49
- end
50
-
51
- specify "should provide #to_hash/#to_h functionality" do
52
- @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
53
- @a.to_h.should == {:a => 1, :b => 2, :c => 3}
54
- end
55
-
56
- specify "should provide #columns as alias to #keys" do
57
- @a.columns.should == [:a, :b, :c]
58
- @a.columns = [:x, :y, :z]
59
-
60
- @a[:x].should == 1
61
- end
62
-
63
- specify "should provide #slice functionality with keys" do
64
- s = @a.slice(0, 2)
65
- s.should == [1, 2]
66
- s.keys.should == [:a, :b]
67
-
68
- s = @a.slice(1..2)
69
- s.should == [2, 3]
70
- s.keys.should == [:b, :c]
71
- end
72
-
73
- specify "should provide #each_pair iterator" do
74
- pairs = []
75
- @a.each_pair {|k, v| pairs << [k, v]}
76
- pairs.should == [[:a, 1], [:b, 2], [:c, 3]]
77
- end
78
-
79
- specify "should provide stock #delete functionality for arrays without keys" do
80
- a = [1, 2, 3]
81
- a.delete(2)
82
- a.should == [1, 3]
83
- end
84
-
85
- specify "should provide key-based #delete functionality" do
86
- @a.delete(:b)
87
- @a.should == [1, 3]
88
- @a.keys.should == [:a, :c]
89
- @a[:a].should == 1
90
- @a[:c].should == 3
91
- end
92
-
93
- specify "should separate array keys after #delete/#delete_at" do
94
- b = @a.dup
95
-
96
- b.delete(:b)
97
-
98
- @a.keys.should == [:a, :b, :c]
99
- b.keys.should == [:a, :c]
100
- @a.should == [1, 2, 3]
101
- b.should == [1, 3]
102
- @a[:b].should == 2
103
- b[:b].should == nil
104
- end
105
-
106
- specify "should provide #each_key functionality" do
107
- keys = []
108
- @a.each_key {|k| keys << k}
109
- keys.should == [:a, :b, :c]
110
- end
111
-
112
- specify "should provide #each_value functionality" do
113
- values = []
114
- @a.each_value {|v| values << v}
115
- values.should == [1, 2, 3]
116
- end
117
-
118
- specify "should provide stock #include? functionality for arrays without keys" do
119
- [1, 2, 3].include?(2).should be_true
120
- [1, 2, 3].include?(4).should be_false
121
- end
122
-
123
- specify "should provide #has_key?/#member?/#key?/#include? functionality" do
124
- @a.has_key?(:a).should be_true
125
- @a.has_key?(:b).should be_true
126
- @a.has_key?(:c).should be_true
127
- @a.has_key?(:B).should be_false
128
- @a.has_key?(:d).should be_false
129
-
130
- @a.has_key?('a').should be_true
131
- @a.has_key?('b').should be_true
132
- @a.has_key?('c').should be_true
133
- @a.has_key?('A').should be_false
134
- @a.has_key?('d').should be_false
135
-
136
- @a.key?(:a).should be_true
137
- @a.key?(:b).should be_true
138
- @a.key?(:c).should be_true
139
- @a.key?(:B).should be_false
140
- @a.key?(:d).should be_false
141
-
142
- @a.key?('a').should be_true
143
- @a.key?('b').should be_true
144
- @a.key?('c').should be_true
145
- @a.key?('A').should be_false
146
- @a.key?('d').should be_false
147
-
148
- @a.member?(:a).should be_true
149
- @a.member?(:b).should be_true
150
- @a.member?(:c).should be_true
151
- @a.member?(:B).should be_false
152
- @a.member?(:d).should be_false
153
-
154
- @a.member?('a').should be_true
155
- @a.member?('b').should be_true
156
- @a.member?('c').should be_true
157
- @a.member?('A').should be_false
158
- @a.member?('d').should be_false
159
-
160
- @a.include?(:a).should be_true
161
- @a.include?(:b).should be_true
162
- @a.include?(:c).should be_true
163
- @a.include?(:B).should be_false
164
- @a.include?(:d).should be_false
165
-
166
- @a.include?('a').should be_true
167
- @a.include?('b').should be_true
168
- @a.include?('c').should be_true
169
- @a.include?('A').should be_false
170
- @a.include?('d').should be_false
171
- end
172
-
173
- specify "should provide original #include? functionality for arrays without keys" do
174
- [1, 2, 3].include?(:a).should be_false
175
- [1, 2, 3].include?(1).should be_true
176
- end
177
-
178
- specify "should provide #has_value?/#value? functionality" do
179
- @a.has_value?(1).should be_true
180
- @a.has_value?(2).should be_true
181
- @a.has_value?(3).should be_true
182
- @a.has_value?(4).should be_false
183
-
184
- @a.value?(1).should be_true
185
- @a.value?(2).should be_true
186
- @a.value?(3).should be_true
187
- @a.value?(4).should be_false
188
- end
189
-
190
- specify "should provide #fetch functionality" do
191
- @a.fetch(:a).should == 1
192
- @a.fetch(:b).should == 2
193
- @a.fetch(:c).should == 3
194
- proc {@a.fetch(:d)}.should raise_error(IndexError)
195
- @a.fetch(:d, 4).should == 4
196
- @a.fetch(:d, nil).should == nil
197
-
198
- @a.fetch(:a) {|v| v.to_s}.should == '1'
199
- @a.fetch(:d, 4) {|v| v.to_s}.should == '4'
200
- end
201
-
202
- specify "should provide #values functionality" do
203
- @a.values.should == [1, 2, 3]
204
- end
205
-
206
- specify "should provide #dup functionality" do
207
- b = @a.dup
208
- b.should == [1, 2, 3]
209
- b.keys.should == @a.keys
210
-
211
- b[:a].should == 1
212
- b[:b].should == 2
213
- b[:c].should == 3
214
- b[:d].should be_nil
215
-
216
- @a.keys << :e
217
- @a.keys.should == [:a, :b, :c, :e]
218
- b.keys.should == @a.keys
219
- end
220
-
221
- specify "should provide #clone functionality" do
222
- b = @a.clone
223
- b.should == [1, 2, 3]
224
- b.keys.should == @a.keys
225
-
226
- b[:a].should == 1
227
- b[:b].should == 2
228
- b[:c].should == 3
229
- b[:d].should be_nil
230
-
231
- @a.keys << :e
232
- @a.keys.should == [:a, :b, :c, :e]
233
- b.keys.should_not == @a.keys
234
- end
235
-
236
- specify "should provide #merge functionality" do
237
- @a.merge(@a).to_hash.should == {:a => 1, :b => 2, :c => 3}
238
-
239
- @a.merge({:b => 22, :d => 4}).to_hash.should == {:a => 1, :b => 22, :c => 3, :d => 4}
240
-
241
- b = [1, 2, 3]
242
- b.keys = [:b, :c, :d]
243
- @a.merge(b).to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
244
-
245
- # call with a block. The block returns the old value passed to it
246
- @a.merge(b) {|k, o, n| o}.to_hash.should == {:a => 1, :b => 2, :c => 3, :d => 3}
247
- end
248
-
249
- specify "should provide #merge!/#update!/#update functionality" do
250
- @a.merge!(@a)
251
- @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
252
-
253
- @a.update(:b => 22)
254
- @a.to_hash.should == {:a => 1, :b => 22, :c => 3}
255
-
256
- b = [1, 2, 3]
257
- b.keys = [:b, :c, :d]
258
- @a.update!(b)
259
- @a.to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
260
- end
261
- end
262
-
263
- context "An array with string keys" do
264
- setup do
265
- @a = [1, 2, 3]
266
- @a.keys = ['a', 'b', 'c']
267
- end
268
-
269
- specify "should provide key access using symbols" do
270
- @a[:a].should == 1
271
- @a[:b].should == 2
272
- @a[:B].should == nil
273
-
274
- @a[:a] = 11
275
- @a.should == [11, 2, 3]
276
- @a[:a].should == 11
277
-
278
- @a[:d] = 4
279
- @a.should == [11, 2, 3, 4]
280
- @a.keys.should == ['a', 'b', 'c', :d]
281
- end
282
-
283
- specify "should provide key access using strings" do
284
- @a['a'].should == 1
285
- @a['A'].should be_nil
286
-
287
- @a['d'] = 4
288
- @a.should == [1, 2, 3, 4]
289
- @a.keys.should == ['a', 'b', 'c', :d]
290
- end
291
-
292
- specify "should provide #store functionality" do
293
- @a.store(:a, 11)
294
- @a.should == [11, 2, 3]
295
-
296
- @a.store(:d, 4)
297
- @a.should == [11, 2, 3, 4]
298
-
299
- @a.store('d', 44)
300
- @a.should == [11, 2, 3, 44]
301
- end
302
-
303
- specify "should provide #to_hash/#to_h functionality" do
304
- @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
305
- @a.to_h.should == {:a => 1, :b => 2, :c => 3}
306
- end
307
-
308
- specify "should provide #columns as alias to #keys" do
309
- @a.columns.should == ['a', 'b', 'c']
310
- @a.columns = [:x, :y, :z]
311
-
312
- @a[:x].should == 1
313
- end
314
-
315
- specify "should provide #slice functionality with keys" do
316
- s = @a.slice(0, 2)
317
- s.should == [1, 2]
318
- s.keys.should == ['a', 'b']
319
-
320
- s = @a.slice(1..2)
321
- s.should == [2, 3]
322
- s.keys.should == ['b', 'c']
323
- end
324
-
325
- specify "should provide #each_pair iterator" do
326
- pairs = []
327
- @a.each_pair {|k, v| pairs << [k, v]}
328
- pairs.should == [['a', 1], ['b', 2], ['c', 3]]
329
- end
330
-
331
- specify "should provide key-based #delete functionality" do
332
- @a.delete(:b)
333
- @a.should == [1, 3]
334
- @a.keys.should == ['a', 'c']
335
- @a[:a].should == 1
336
- @a[:c].should == 3
337
- end
338
-
339
- specify "should provide #each_key functionality" do
340
- keys = []
341
- @a.each_key {|k| keys << k}
342
- keys.should == ['a', 'b', 'c']
343
- end
344
-
345
- specify "should provide #each_value functionality" do
346
- values = []
347
- @a.each_value {|v| values << v}
348
- values.should == [1, 2, 3]
349
- end
350
-
351
- specify "should provide #has_key?/#member?/#key?/#include? functionality" do
352
- @a.has_key?(:a).should be_true
353
- @a.has_key?(:b).should be_true
354
- @a.has_key?(:c).should be_true
355
- @a.has_key?(:B).should be_false
356
- @a.has_key?(:d).should be_false
357
-
358
- @a.has_key?('a').should be_true
359
- @a.has_key?('b').should be_true
360
- @a.has_key?('c').should be_true
361
- @a.has_key?('A').should be_false
362
- @a.has_key?('d').should be_false
363
-
364
- @a.key?(:a).should be_true
365
- @a.key?(:b).should be_true
366
- @a.key?(:c).should be_true
367
- @a.key?(:B).should be_false
368
- @a.key?(:d).should be_false
369
-
370
- @a.key?('a').should be_true
371
- @a.key?('b').should be_true
372
- @a.key?('c').should be_true
373
- @a.key?('A').should be_false
374
- @a.key?('d').should be_false
375
-
376
- @a.member?(:a).should be_true
377
- @a.member?(:b).should be_true
378
- @a.member?(:c).should be_true
379
- @a.member?(:B).should be_false
380
- @a.member?(:d).should be_false
381
-
382
- @a.member?('a').should be_true
383
- @a.member?('b').should be_true
384
- @a.member?('c').should be_true
385
- @a.member?('A').should be_false
386
- @a.member?('d').should be_false
387
-
388
- @a.include?(:a).should be_true
389
- @a.include?(:b).should be_true
390
- @a.include?(:c).should be_true
391
- @a.include?(:B).should be_false
392
- @a.include?(:d).should be_false
393
-
394
- @a.include?('a').should be_true
395
- @a.include?('b').should be_true
396
- @a.include?('c').should be_true
397
- @a.include?('A').should be_false
398
- @a.include?('d').should be_false
399
- end
400
-
401
- specify "should provide original #include? functionality for arrays without keys" do
402
- [1, 2, 3].include?(:a).should be_false
403
- [1, 2, 3].include?(1).should be_true
404
- end
405
-
406
- specify "should provide #has_value?/#value? functionality" do
407
- @a.has_value?(1).should be_true
408
- @a.has_value?(2).should be_true
409
- @a.has_value?(3).should be_true
410
- @a.has_value?(4).should be_false
411
-
412
- @a.value?(1).should be_true
413
- @a.value?(2).should be_true
414
- @a.value?(3).should be_true
415
- @a.value?(4).should be_false
416
- end
417
-
418
- specify "should provide #fetch functionality" do
419
- @a.fetch(:a).should == 1
420
- @a.fetch(:b).should == 2
421
- @a.fetch(:c).should == 3
422
- proc {@a.fetch(:d)}.should raise_error(IndexError)
423
- @a.fetch(:d, 4).should == 4
424
- @a.fetch(:d, nil).should == nil
425
-
426
- @a.fetch(:a) {|v| v.to_s}.should == '1'
427
- @a.fetch(:d, 4) {|v| v.to_s}.should == '4'
428
- end
429
-
430
- specify "should provide #values functionality" do
431
- @a.values.should == [1, 2, 3]
432
- end
433
-
434
- specify "should provide #dup functionality" do
435
- b = @a.dup
436
- b.should == [1, 2, 3]
437
- b.keys.should == @a.keys
438
-
439
- b[:a].should == 1
440
- b[:b].should == 2
441
- b[:c].should == 3
442
- b[:d].should be_nil
443
-
444
- @a.keys << :e
445
- @a.keys.should == ['a', 'b', 'c', :e]
446
- b.keys.should == @a.keys
447
- end
448
-
449
- specify "should provide #clone functionality" do
450
- b = @a.clone
451
- b.should == [1, 2, 3]
452
- b.keys.should == @a.keys
453
-
454
- b[:a].should == 1
455
- b[:b].should == 2
456
- b[:c].should == 3
457
- b[:d].should be_nil
458
-
459
- @a.keys << :e
460
- @a.keys.should == ['a', 'b', 'c', :e]
461
- b.keys.should_not == @a.keys
462
- end
463
-
464
- specify "should provide #merge functionality" do
465
- @a.merge(@a).to_hash.should == {:a => 1, :b => 2, :c => 3}
466
-
467
- @a.merge({:b => 22, :d => 4}).to_hash.should == {:a => 1, :b => 22, :c => 3, :d => 4}
468
-
469
- b = [1, 2, 3]
470
- b.keys = [:b, :c, :d]
471
- @a.merge(b).to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
472
-
473
- # call with a block. The block returns the old value passed to it
474
- @a.merge(b) {|k, o, n| o}.to_hash.should == {:a => 1, :b => 2, :c => 3, :d => 3}
475
- end
476
-
477
- specify "should provide #merge!/#update!/#update functionality" do
478
- @a.merge!(@a)
479
- @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
480
-
481
- @a.update(:b => 22)
482
- @a.to_hash.should == {:a => 1, :b => 22, :c => 3}
483
-
484
- b = [1, 2, 3]
485
- b.keys = [:b, :c, :d]
486
- @a.update!(b)
487
- @a.to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
488
- end
489
- end
490
-
491
- context "Array.from_hash" do
492
- specify "should construct an array with keys from a hash" do
493
- h = {:x => 1, :y => 2, :z => 3}
494
- a = Array.from_hash(h)
495
- a.to_hash.should == h
496
- end
497
- end
498
-
499
- context "Sequel.use_array_tuples" do
500
- setup do
501
- @c = Class.new(Sequel::Dataset) do
502
- def fetch_rows(sql, &block)
503
- block[{:a => 1, :b => 2, :c => 3}]
504
- end
505
- end
506
-
507
- @ds = @c.new(nil).from(:items)
508
- end
509
-
510
- teardown do
511
- Sequel.use_hash_tuples
512
- end
513
-
514
- specify "should cause the dataset to return array tuples instead of hashes" do
515
- @ds.first.should == {:a => 1, :b => 2, :c => 3}
516
- Sequel.use_array_tuples
517
- a = @ds.first
518
- a.class.should == Array
519
- a.values.sort.should == [1, 2, 3]
520
- a.keys.map {|k| k.to_s}.sort.should == ['a', 'b', 'c']
521
- a[:a].should == 1
522
- a[:b].should == 2
523
- a[:c].should == 3
524
- a[:d].should == nil
525
-
526
- @ds.transform(:a => [proc {|v| v.to_s}, proc {|v| v.to_i}])
527
- a = @ds.first
528
- a[:a].should == '1'
529
-
530
- @ds.transform({})
531
- a = @ds.first
532
- a[:a].should == 1
533
-
534
- @ds.set_model(Hash)
535
- end
536
-
537
- specify "should be reversible using Sequel.use_hash_tuples" do
538
- Sequel.use_array_tuples
539
- @ds.first.class.should == Array
540
-
541
- Sequel.use_hash_tuples
542
- @ds.first.should == {:a => 1, :b => 2, :c => 3}
543
- end
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ context "An array with symbol keys" do
4
+ setup do
5
+ @a = [1, 2, 3]
6
+ @a.keys = [:a, :b, :c]
7
+ end
8
+
9
+ specify "should provide subscript access" do
10
+ @a[0].should == 1
11
+ @a[0..1].should == [1, 2]
12
+
13
+ @a[1] = 4
14
+ @a.should == [1, 4, 3]
15
+ end
16
+
17
+ specify "should provide key access using symbols" do
18
+ @a[:a].should == 1
19
+ @a[:b].should == 2
20
+ @a[:B].should == nil
21
+
22
+ @a[:a] = 11
23
+ @a.should == [11, 2, 3]
24
+ @a[:a].should == 11
25
+
26
+ @a[:d] = 4
27
+ @a.should == [11, 2, 3, 4]
28
+ @a.keys.should == [:a, :b, :c, :d]
29
+ end
30
+
31
+ specify "should provide key access using strings" do
32
+ @a['a'].should == 1
33
+ @a['A'].should be_nil
34
+
35
+ @a['d'] = 4
36
+ @a.should == [1, 2, 3, 4]
37
+ @a.keys.should == [:a, :b, :c, :d]
38
+ end
39
+
40
+ specify "should provide #store functionality" do
41
+ @a.store(:a, 11)
42
+ @a.should == [11, 2, 3]
43
+
44
+ @a.store(:d, 4)
45
+ @a.should == [11, 2, 3, 4]
46
+
47
+ @a.store('d', 44)
48
+ @a.should == [11, 2, 3, 44]
49
+ end
50
+
51
+ specify "should provide #to_hash/#to_h functionality" do
52
+ @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
53
+ @a.to_h.should == {:a => 1, :b => 2, :c => 3}
54
+ end
55
+
56
+ specify "should provide #columns as alias to #keys" do
57
+ @a.columns.should == [:a, :b, :c]
58
+ @a.columns = [:x, :y, :z]
59
+
60
+ @a[:x].should == 1
61
+ end
62
+
63
+ specify "should provide #slice functionality with keys" do
64
+ s = @a.slice(0, 2)
65
+ s.should == [1, 2]
66
+ s.keys.should == [:a, :b]
67
+
68
+ s = @a.slice(1..2)
69
+ s.should == [2, 3]
70
+ s.keys.should == [:b, :c]
71
+ end
72
+
73
+ specify "should provide #each_pair iterator" do
74
+ pairs = []
75
+ @a.each_pair {|k, v| pairs << [k, v]}
76
+ pairs.should == [[:a, 1], [:b, 2], [:c, 3]]
77
+ end
78
+
79
+ specify "should provide stock #delete functionality for arrays without keys" do
80
+ a = [1, 2, 3]
81
+ a.delete(2)
82
+ a.should == [1, 3]
83
+ end
84
+
85
+ specify "should provide key-based #delete functionality" do
86
+ @a.delete(:b)
87
+ @a.should == [1, 3]
88
+ @a.keys.should == [:a, :c]
89
+ @a[:a].should == 1
90
+ @a[:c].should == 3
91
+ end
92
+
93
+ specify "should separate array keys after #delete/#delete_at" do
94
+ b = @a.dup
95
+
96
+ b.delete(:b)
97
+
98
+ @a.keys.should == [:a, :b, :c]
99
+ b.keys.should == [:a, :c]
100
+ @a.should == [1, 2, 3]
101
+ b.should == [1, 3]
102
+ @a[:b].should == 2
103
+ b[:b].should == nil
104
+ end
105
+
106
+ specify "should provide #each_key functionality" do
107
+ keys = []
108
+ @a.each_key {|k| keys << k}
109
+ keys.should == [:a, :b, :c]
110
+ end
111
+
112
+ specify "should provide #each_value functionality" do
113
+ values = []
114
+ @a.each_value {|v| values << v}
115
+ values.should == [1, 2, 3]
116
+ end
117
+
118
+ specify "should provide stock #include? functionality for arrays without keys" do
119
+ [1, 2, 3].include?(2).should be_true
120
+ [1, 2, 3].include?(4).should be_false
121
+ end
122
+
123
+ specify "should provide #has_key?/#member?/#key?/#include? functionality" do
124
+ @a.has_key?(:a).should be_true
125
+ @a.has_key?(:b).should be_true
126
+ @a.has_key?(:c).should be_true
127
+ @a.has_key?(:B).should be_false
128
+ @a.has_key?(:d).should be_false
129
+
130
+ @a.has_key?('a').should be_true
131
+ @a.has_key?('b').should be_true
132
+ @a.has_key?('c').should be_true
133
+ @a.has_key?('A').should be_false
134
+ @a.has_key?('d').should be_false
135
+
136
+ @a.key?(:a).should be_true
137
+ @a.key?(:b).should be_true
138
+ @a.key?(:c).should be_true
139
+ @a.key?(:B).should be_false
140
+ @a.key?(:d).should be_false
141
+
142
+ @a.key?('a').should be_true
143
+ @a.key?('b').should be_true
144
+ @a.key?('c').should be_true
145
+ @a.key?('A').should be_false
146
+ @a.key?('d').should be_false
147
+
148
+ @a.member?(:a).should be_true
149
+ @a.member?(:b).should be_true
150
+ @a.member?(:c).should be_true
151
+ @a.member?(:B).should be_false
152
+ @a.member?(:d).should be_false
153
+
154
+ @a.member?('a').should be_true
155
+ @a.member?('b').should be_true
156
+ @a.member?('c').should be_true
157
+ @a.member?('A').should be_false
158
+ @a.member?('d').should be_false
159
+
160
+ @a.include?(:a).should be_true
161
+ @a.include?(:b).should be_true
162
+ @a.include?(:c).should be_true
163
+ @a.include?(:B).should be_false
164
+ @a.include?(:d).should be_false
165
+
166
+ @a.include?('a').should be_true
167
+ @a.include?('b').should be_true
168
+ @a.include?('c').should be_true
169
+ @a.include?('A').should be_false
170
+ @a.include?('d').should be_false
171
+ end
172
+
173
+ specify "should provide original #include? functionality for arrays without keys" do
174
+ [1, 2, 3].include?(:a).should be_false
175
+ [1, 2, 3].include?(1).should be_true
176
+ end
177
+
178
+ specify "should provide #has_value?/#value? functionality" do
179
+ @a.has_value?(1).should be_true
180
+ @a.has_value?(2).should be_true
181
+ @a.has_value?(3).should be_true
182
+ @a.has_value?(4).should be_false
183
+
184
+ @a.value?(1).should be_true
185
+ @a.value?(2).should be_true
186
+ @a.value?(3).should be_true
187
+ @a.value?(4).should be_false
188
+ end
189
+
190
+ specify "should provide #fetch functionality" do
191
+ @a.fetch(:a).should == 1
192
+ @a.fetch(:b).should == 2
193
+ @a.fetch(:c).should == 3
194
+ proc {@a.fetch(:d)}.should raise_error(IndexError)
195
+ @a.fetch(:d, 4).should == 4
196
+ @a.fetch(:d, nil).should == nil
197
+
198
+ @a.fetch(:a) {|v| v.to_s}.should == '1'
199
+ @a.fetch(:d, 4) {|v| v.to_s}.should == '4'
200
+ end
201
+
202
+ specify "should provide #values functionality" do
203
+ @a.values.should == [1, 2, 3]
204
+ end
205
+
206
+ specify "should provide #dup functionality" do
207
+ b = @a.dup
208
+ b.should == [1, 2, 3]
209
+ b.keys.should == @a.keys
210
+
211
+ b[:a].should == 1
212
+ b[:b].should == 2
213
+ b[:c].should == 3
214
+ b[:d].should be_nil
215
+
216
+ @a.keys << :e
217
+ @a.keys.should == [:a, :b, :c, :e]
218
+ b.keys.should == @a.keys
219
+ end
220
+
221
+ specify "should provide #clone functionality" do
222
+ b = @a.clone
223
+ b.should == [1, 2, 3]
224
+ b.keys.should == @a.keys
225
+
226
+ b[:a].should == 1
227
+ b[:b].should == 2
228
+ b[:c].should == 3
229
+ b[:d].should be_nil
230
+
231
+ @a.keys << :e
232
+ @a.keys.should == [:a, :b, :c, :e]
233
+ b.keys.should_not == @a.keys
234
+ end
235
+
236
+ specify "should provide #merge functionality" do
237
+ @a.merge(@a).to_hash.should == {:a => 1, :b => 2, :c => 3}
238
+
239
+ @a.merge({:b => 22, :d => 4}).to_hash.should == {:a => 1, :b => 22, :c => 3, :d => 4}
240
+
241
+ b = [1, 2, 3]
242
+ b.keys = [:b, :c, :d]
243
+ @a.merge(b).to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
244
+
245
+ # call with a block. The block returns the old value passed to it
246
+ @a.merge(b) {|k, o, n| o}.to_hash.should == {:a => 1, :b => 2, :c => 3, :d => 3}
247
+ end
248
+
249
+ specify "should provide #merge!/#update!/#update functionality" do
250
+ @a.merge!(@a)
251
+ @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
252
+
253
+ @a.update(:b => 22)
254
+ @a.to_hash.should == {:a => 1, :b => 22, :c => 3}
255
+
256
+ b = [1, 2, 3]
257
+ b.keys = [:b, :c, :d]
258
+ @a.update!(b)
259
+ @a.to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
260
+ end
261
+ end
262
+
263
+ context "An array with string keys" do
264
+ setup do
265
+ @a = [1, 2, 3]
266
+ @a.keys = ['a', 'b', 'c']
267
+ end
268
+
269
+ specify "should provide key access using symbols" do
270
+ @a[:a].should == 1
271
+ @a[:b].should == 2
272
+ @a[:B].should == nil
273
+
274
+ @a[:a] = 11
275
+ @a.should == [11, 2, 3]
276
+ @a[:a].should == 11
277
+
278
+ @a[:d] = 4
279
+ @a.should == [11, 2, 3, 4]
280
+ @a.keys.should == ['a', 'b', 'c', :d]
281
+ end
282
+
283
+ specify "should provide key access using strings" do
284
+ @a['a'].should == 1
285
+ @a['A'].should be_nil
286
+
287
+ @a['d'] = 4
288
+ @a.should == [1, 2, 3, 4]
289
+ @a.keys.should == ['a', 'b', 'c', :d]
290
+ end
291
+
292
+ specify "should provide #store functionality" do
293
+ @a.store(:a, 11)
294
+ @a.should == [11, 2, 3]
295
+
296
+ @a.store(:d, 4)
297
+ @a.should == [11, 2, 3, 4]
298
+
299
+ @a.store('d', 44)
300
+ @a.should == [11, 2, 3, 44]
301
+ end
302
+
303
+ specify "should provide #to_hash/#to_h functionality" do
304
+ @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
305
+ @a.to_h.should == {:a => 1, :b => 2, :c => 3}
306
+ end
307
+
308
+ specify "should provide #columns as alias to #keys" do
309
+ @a.columns.should == ['a', 'b', 'c']
310
+ @a.columns = [:x, :y, :z]
311
+
312
+ @a[:x].should == 1
313
+ end
314
+
315
+ specify "should provide #slice functionality with keys" do
316
+ s = @a.slice(0, 2)
317
+ s.should == [1, 2]
318
+ s.keys.should == ['a', 'b']
319
+
320
+ s = @a.slice(1..2)
321
+ s.should == [2, 3]
322
+ s.keys.should == ['b', 'c']
323
+ end
324
+
325
+ specify "should provide #each_pair iterator" do
326
+ pairs = []
327
+ @a.each_pair {|k, v| pairs << [k, v]}
328
+ pairs.should == [['a', 1], ['b', 2], ['c', 3]]
329
+ end
330
+
331
+ specify "should provide key-based #delete functionality" do
332
+ @a.delete(:b)
333
+ @a.should == [1, 3]
334
+ @a.keys.should == ['a', 'c']
335
+ @a[:a].should == 1
336
+ @a[:c].should == 3
337
+ end
338
+
339
+ specify "should provide #each_key functionality" do
340
+ keys = []
341
+ @a.each_key {|k| keys << k}
342
+ keys.should == ['a', 'b', 'c']
343
+ end
344
+
345
+ specify "should provide #each_value functionality" do
346
+ values = []
347
+ @a.each_value {|v| values << v}
348
+ values.should == [1, 2, 3]
349
+ end
350
+
351
+ specify "should provide #has_key?/#member?/#key?/#include? functionality" do
352
+ @a.has_key?(:a).should be_true
353
+ @a.has_key?(:b).should be_true
354
+ @a.has_key?(:c).should be_true
355
+ @a.has_key?(:B).should be_false
356
+ @a.has_key?(:d).should be_false
357
+
358
+ @a.has_key?('a').should be_true
359
+ @a.has_key?('b').should be_true
360
+ @a.has_key?('c').should be_true
361
+ @a.has_key?('A').should be_false
362
+ @a.has_key?('d').should be_false
363
+
364
+ @a.key?(:a).should be_true
365
+ @a.key?(:b).should be_true
366
+ @a.key?(:c).should be_true
367
+ @a.key?(:B).should be_false
368
+ @a.key?(:d).should be_false
369
+
370
+ @a.key?('a').should be_true
371
+ @a.key?('b').should be_true
372
+ @a.key?('c').should be_true
373
+ @a.key?('A').should be_false
374
+ @a.key?('d').should be_false
375
+
376
+ @a.member?(:a).should be_true
377
+ @a.member?(:b).should be_true
378
+ @a.member?(:c).should be_true
379
+ @a.member?(:B).should be_false
380
+ @a.member?(:d).should be_false
381
+
382
+ @a.member?('a').should be_true
383
+ @a.member?('b').should be_true
384
+ @a.member?('c').should be_true
385
+ @a.member?('A').should be_false
386
+ @a.member?('d').should be_false
387
+
388
+ @a.include?(:a).should be_true
389
+ @a.include?(:b).should be_true
390
+ @a.include?(:c).should be_true
391
+ @a.include?(:B).should be_false
392
+ @a.include?(:d).should be_false
393
+
394
+ @a.include?('a').should be_true
395
+ @a.include?('b').should be_true
396
+ @a.include?('c').should be_true
397
+ @a.include?('A').should be_false
398
+ @a.include?('d').should be_false
399
+ end
400
+
401
+ specify "should provide original #include? functionality for arrays without keys" do
402
+ [1, 2, 3].include?(:a).should be_false
403
+ [1, 2, 3].include?(1).should be_true
404
+ end
405
+
406
+ specify "should provide #has_value?/#value? functionality" do
407
+ @a.has_value?(1).should be_true
408
+ @a.has_value?(2).should be_true
409
+ @a.has_value?(3).should be_true
410
+ @a.has_value?(4).should be_false
411
+
412
+ @a.value?(1).should be_true
413
+ @a.value?(2).should be_true
414
+ @a.value?(3).should be_true
415
+ @a.value?(4).should be_false
416
+ end
417
+
418
+ specify "should provide #fetch functionality" do
419
+ @a.fetch(:a).should == 1
420
+ @a.fetch(:b).should == 2
421
+ @a.fetch(:c).should == 3
422
+ proc {@a.fetch(:d)}.should raise_error(IndexError)
423
+ @a.fetch(:d, 4).should == 4
424
+ @a.fetch(:d, nil).should == nil
425
+
426
+ @a.fetch(:a) {|v| v.to_s}.should == '1'
427
+ @a.fetch(:d, 4) {|v| v.to_s}.should == '4'
428
+ end
429
+
430
+ specify "should provide #values functionality" do
431
+ @a.values.should == [1, 2, 3]
432
+ end
433
+
434
+ specify "should provide #dup functionality" do
435
+ b = @a.dup
436
+ b.should == [1, 2, 3]
437
+ b.keys.should == @a.keys
438
+
439
+ b[:a].should == 1
440
+ b[:b].should == 2
441
+ b[:c].should == 3
442
+ b[:d].should be_nil
443
+
444
+ @a.keys << :e
445
+ @a.keys.should == ['a', 'b', 'c', :e]
446
+ b.keys.should == @a.keys
447
+ end
448
+
449
+ specify "should provide #clone functionality" do
450
+ b = @a.clone
451
+ b.should == [1, 2, 3]
452
+ b.keys.should == @a.keys
453
+
454
+ b[:a].should == 1
455
+ b[:b].should == 2
456
+ b[:c].should == 3
457
+ b[:d].should be_nil
458
+
459
+ @a.keys << :e
460
+ @a.keys.should == ['a', 'b', 'c', :e]
461
+ b.keys.should_not == @a.keys
462
+ end
463
+
464
+ specify "should provide #merge functionality" do
465
+ @a.merge(@a).to_hash.should == {:a => 1, :b => 2, :c => 3}
466
+
467
+ @a.merge({:b => 22, :d => 4}).to_hash.should == {:a => 1, :b => 22, :c => 3, :d => 4}
468
+
469
+ b = [1, 2, 3]
470
+ b.keys = [:b, :c, :d]
471
+ @a.merge(b).to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
472
+
473
+ # call with a block. The block returns the old value passed to it
474
+ @a.merge(b) {|k, o, n| o}.to_hash.should == {:a => 1, :b => 2, :c => 3, :d => 3}
475
+ end
476
+
477
+ specify "should provide #merge!/#update!/#update functionality" do
478
+ @a.merge!(@a)
479
+ @a.to_hash.should == {:a => 1, :b => 2, :c => 3}
480
+
481
+ @a.update(:b => 22)
482
+ @a.to_hash.should == {:a => 1, :b => 22, :c => 3}
483
+
484
+ b = [1, 2, 3]
485
+ b.keys = [:b, :c, :d]
486
+ @a.update!(b)
487
+ @a.to_hash.should == {:a => 1, :b => 1, :c => 2, :d => 3}
488
+ end
489
+ end
490
+
491
+ context "Array.from_hash" do
492
+ specify "should construct an array with keys from a hash" do
493
+ h = {:x => 1, :y => 2, :z => 3}
494
+ a = Array.from_hash(h)
495
+ a.to_hash.should == h
496
+ end
497
+ end
498
+
499
+ context "Sequel.use_array_tuples" do
500
+ setup do
501
+ @c = Class.new(Sequel::Dataset) do
502
+ def fetch_rows(sql, &block)
503
+ block[{:a => 1, :b => 2, :c => 3}]
504
+ end
505
+ end
506
+
507
+ @ds = @c.new(nil).from(:items)
508
+ end
509
+
510
+ teardown do
511
+ Sequel.use_hash_tuples
512
+ end
513
+
514
+ specify "should cause the dataset to return array tuples instead of hashes" do
515
+ @ds.first.should == {:a => 1, :b => 2, :c => 3}
516
+ Sequel.use_array_tuples
517
+ a = @ds.first
518
+ a.class.should == Array
519
+ a.values.sort.should == [1, 2, 3]
520
+ a.keys.map {|k| k.to_s}.sort.should == ['a', 'b', 'c']
521
+ a[:a].should == 1
522
+ a[:b].should == 2
523
+ a[:c].should == 3
524
+ a[:d].should == nil
525
+
526
+ @ds.transform(:a => [proc {|v| v.to_s}, proc {|v| v.to_i}])
527
+ a = @ds.first
528
+ a[:a].should == '1'
529
+
530
+ @ds.transform({})
531
+ a = @ds.first
532
+ a[:a].should == 1
533
+
534
+ @ds.set_model(Hash)
535
+ end
536
+
537
+ specify "should be reversible using Sequel.use_hash_tuples" do
538
+ Sequel.use_array_tuples
539
+ @ds.first.class.should == Array
540
+
541
+ Sequel.use_hash_tuples
542
+ @ds.first.should == {:a => 1, :b => 2, :c => 3}
543
+ end
544
544
  end