random-accessible 0.1.3 → 0.1.4

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,13 @@
1
+ class Array
2
+
3
+ old_eql = instance_method(:eql?)
4
+
5
+ define_method(:eql?) do |other|
6
+ if other.is_a? RandomReadable
7
+ other.eql?(self)
8
+ else
9
+ old_eql.bind(self).call(other)
10
+ end
11
+ end
12
+
13
+ end
@@ -242,12 +242,19 @@ module RandomReadable
242
242
  # and evaluates minimum elements needed to get results.
243
243
  # If not, this method is same as Object's and evaluates no element.
244
244
  def eql?(other)
245
- return false unless self.class.eql?(other.class)
245
+ return false unless other.is_a?(RandomReadable)
246
246
  return super(other) unless has_size?
247
247
 
248
+ begin
249
+ return false if other.size != size
250
+ rescue NotImplementedError
251
+ return false
252
+ end
253
+
248
254
  each_index do |i|
249
255
  return false unless at(i).eql?(other.at(i))
250
256
  end
257
+
251
258
  return true
252
259
  end
253
260
 
@@ -586,3 +593,19 @@ module RandomReadable
586
593
  end
587
594
 
588
595
  end
596
+
597
+
598
+ # class Array
599
+ #
600
+ # old_eql = instance_method(:eql?)
601
+ #
602
+ # define_method(:eql?) do |other|
603
+ # if other.is_a? RandomReadable
604
+ # other.eql?(self)
605
+ # else
606
+ # old_eql.bind(self).call(other)
607
+ # end
608
+ # end
609
+ #
610
+ # end
611
+
@@ -0,0 +1,59 @@
1
+ require 'random-accessible'
2
+
3
+
4
+ class HashWrapper
5
+
6
+ include RandomAccessible
7
+
8
+ # You can define arbitral initializer.
9
+ def initialize(ary = nil)
10
+ @h = Hash.new
11
+ if ary.nil?
12
+ @size = 0
13
+ else
14
+ ary.each_with_index do |el, i|
15
+ @h[i] = el
16
+ end
17
+ @size = ary.size
18
+ end
19
+ end
20
+
21
+ # Define a read-accessor.
22
+ # The easiest method to implement is read_access.
23
+ def read_access(pos)
24
+ # The mixin guarantees 0 <= pos < size.
25
+ raise if pos < 0 && @size <= pos
26
+ return @h[pos]
27
+ end
28
+
29
+ # Define a replace-accessor.
30
+ # We recommend that implement replace_access.
31
+ def replace_access(pos, val)
32
+ raise if pos < 0 && @size <= pos
33
+ @h[pos] = val
34
+ end
35
+
36
+ # Define expand.
37
+ # This method is called when increasing the object's size.
38
+ def expand(n)
39
+ n.times do
40
+ @h[@size] = nil
41
+ @size += 1
42
+ end
43
+ end
44
+
45
+ # Define shrink.
46
+ # This method is called when decreasing the object's size.
47
+ def shrink(n)
48
+ n.times do
49
+ @h.delete(@size - 1)
50
+ @size -= 1
51
+ end
52
+ end
53
+
54
+ # Define a size-provider.
55
+ # You can choose from size and length method.
56
+ attr_reader :size
57
+
58
+ end
59
+
@@ -0,0 +1,72 @@
1
+ require 'random-accessible'
2
+ require 'psych'
3
+
4
+ # Read and write YAML files in a directory like an Array.
5
+ class YAMLDir
6
+
7
+ include RandomAccessible
8
+
9
+ def initialize(dirname)
10
+ @dirname = dirname
11
+ @size = 0
12
+
13
+ unless Dir.exist?(dirname)
14
+ Dir.mkdir(dirname)
15
+ end
16
+
17
+ Dir.foreach(dirname) do |name|
18
+ path = File.join(dirname, name)
19
+ #if name =~ /^(\d+).[(yaml)|(yml)]/
20
+ if name =~ /^(\d+).yaml/
21
+ unless FileTest.directory?(path)
22
+ index = Regexp.last_match[1].to_i
23
+ @size = [@size, index].max
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def read_access(pos)
30
+ filename = File.join(@dirname, "#{pos}.yaml")
31
+ unless FileTest.exist?(filename)
32
+ return nil
33
+ end
34
+
35
+ return Psych.load_file(filename)
36
+ end
37
+
38
+ def replace_access(pos, val)
39
+ filename = File.join(@dirname, "#{pos}.yaml")
40
+ File.open(filename, "w") do |file|
41
+ Psych.dump(val, file)
42
+ end
43
+
44
+ @size = [@size, pos + 1].max
45
+ end
46
+
47
+ def shrink(n)
48
+ n.times do
49
+ pos = @size - 1
50
+ filename = File.join(@dirname, "#{pos}.yaml")
51
+ if FileTest.exist?(filename)
52
+ File.delete(filename)
53
+ end
54
+ @size -= 1
55
+ end
56
+ end
57
+
58
+ attr_reader :size
59
+
60
+ end
61
+
62
+ yd = YAMLDir.new ARGV[0]
63
+ 10.times do |i|
64
+ yd[i] = i
65
+ end
66
+ 5.times do |i|
67
+ yd.delete_at(yd.size - 1)
68
+ end
69
+ yd.each_with_index do |i|
70
+ puts "#{i}: #{yd[i]}"
71
+ end
72
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random-accessible
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-07 00:00:00.000000000 Z
12
+ date: 2011-12-16 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'RandomAccessible mixin provides all methods of Array (regard as high-functioning
15
15
  edition of Enumerable).
@@ -27,6 +27,7 @@ extra_rdoc_files:
27
27
  - README.en
28
28
  files:
29
29
  - lib/random-writable.rb
30
+ - lib/array-patch.rb
30
31
  - lib/random-accessible.rb
31
32
  - lib/common-traits.rb
32
33
  - lib/random-readable.rb
@@ -34,6 +35,8 @@ files:
34
35
  - test/test-random-writable.rb
35
36
  - test/test-suite.rb
36
37
  - test/test-random-accessible.rb
38
+ - sample/yaml-dir.rb
39
+ - sample/hash-wrapper.rb
37
40
  - BSDL
38
41
  - README.en
39
42
  homepage: https://github.com/natsuki14/random-accessible