map 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -6,10 +6,10 @@ SYNOPSIS
6
6
  hash that works in all rubies
7
7
 
8
8
  maps are rad ordered hashes that are both ordered, string/symbol
9
- indifferent, and have all sorts of sweetness like recursive convertsion,
10
- more robust implementation than HashWithIndifferentAccess, support for
11
- struct like (map.foo) access, and support for option/keyword access which
12
- avoids several nasty classes of errors in many ruby libraries
9
+ indifferent, and have all sorts of sweetness like recursive conversion, more
10
+ robust implementation than HashWithIndifferentAccess, support for struct
11
+ like (map.foo) access, and support for option/keyword access which avoids
12
+ several nasty classes of errors in many ruby libraries
13
13
 
14
14
  INSTALL
15
15
  gem install map
@@ -24,9 +24,11 @@ DESCRIPTION
24
24
  #
25
25
  m = Map[:k, :v, :key, :val]
26
26
  m = Map(:k, :v, :key, :val)
27
+ m = Map.new(:k, :v, :key, :val)
27
28
 
28
29
  m = Map[[:k, :v], [:key, :val]]
29
- m = Map[{:k => :v, :key => :val}]
30
+ m = Map(:k => :v, :key => :val) # ruh-oh, the input hash loses order!
31
+ m = Map.new(:k => :v, :key => :val) # ruh-oh, the input hash loses order!
30
32
 
31
33
 
32
34
  m = Map.new
@@ -37,16 +39,23 @@ DESCRIPTION
37
39
  p m.keys #=> ['a','b','c'] ### always ordered!
38
40
  p m.keys #=> [0,1,2] ### always ordered!
39
41
 
40
- # maps don't care between symbol and string keys
42
+ # maps don't care about symbol vs.string keys
41
43
  #
42
44
  p m[:a] #=> 0
43
45
  p m["a"] #=> 0
44
46
 
47
+ # even via deep nesting
48
+ #
49
+ p m[:foo]['bar'][:baz] #=> 42
50
+
45
51
  # many functions operate in a way one would expect from an ordered container
46
52
  #
47
53
  m.update(:k2 => :v2)
48
54
  m.update(:k2, :v2)
49
55
 
56
+ key_val_pair = m.shift
57
+ key_val_pair = m.pop
58
+
50
59
  # maps keep mapiness for even deep operations
51
60
  #
52
61
  m.update :nested => {:hashes => {:are => :converted}}
@@ -58,7 +67,7 @@ DESCRIPTION
58
67
  p s.foo.bar #=> 42
59
68
 
60
69
  # because option parsing is such a common use case for needing string/symbol
61
- # indifference map.rb comes out of the box for option support
70
+ # indifference map.rb comes out of the box loaded with option support
62
71
  #
63
72
  def foo(*args, &block)
64
73
  opts = Map.options(args)
@@ -76,7 +85,7 @@ DESCRIPTION
76
85
 
77
86
  # this avoids such bugs as
78
87
  #
79
- options = {:read_only => true}
88
+ options = {:read_only => false}
80
89
  read_only = options[:read_only] || true # should be false but is true
81
90
 
82
91
  # with options this becomes
data/lib/map.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Map < Hash
2
- Version = '1.5.0' unless defined?(Version)
2
+ Version = '1.5.1' unless defined?(Version)
3
3
  Load = Kernel.method(:load) unless defined?(Load)
4
4
 
5
5
  class << Map
@@ -1,6 +1,6 @@
1
1
  class Map
2
2
  class Struct
3
- instance_methods.each { |m| undef_method m unless m =~ /^__/ }
3
+ instance_methods.each { |m| undef_method m unless m =~ /^__|object_id/ }
4
4
 
5
5
  attr :map
6
6
 
@@ -4,7 +4,9 @@
4
4
 
5
5
  def Testing(*args, &block)
6
6
  Class.new(Test::Unit::TestCase) do
7
- def self.slug_for(*args)
7
+ eval("This=self")
8
+
9
+ def This.slug_for(*args)
8
10
  string = args.flatten.compact.join('-')
9
11
  words = string.to_s.scan(%r/\w+/)
10
12
  words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
@@ -12,10 +14,14 @@
12
14
  words.join('-').downcase
13
15
  end
14
16
 
15
- @@testing_subclass_count = 0 unless defined?(@@testing_subclass_count)
16
- @@testing_subclass_count += 1
17
+ def This.testing_subclass_count
18
+ @testing_subclass_count ||= 1
19
+ ensure
20
+ @testing_subclass_count += 1
21
+ end
22
+
17
23
  slug = slug_for(*args).gsub(%r/-/,'_')
18
- name = ['TESTING', '%03d' % @@testing_subclass_count, slug].delete_if{|part| part.empty?}.join('_')
24
+ name = ['TESTING', '%03d' % This.testing_subclass_count, slug].delete_if{|part| part.empty?}.join('_')
19
25
  name = name.upcase!
20
26
  const_set(:Name, name)
21
27
  def self.name() const_get(:Name) end
@@ -106,7 +106,7 @@ Testing Map do
106
106
 
107
107
  testing 'that maps are string/symbol indifferent for recursive look-ups' do
108
108
  map = assert{ Map(:a => {:b => {:c => 42}}) }
109
- assert{ map[:a] = {:b => {:c, 42}} }
109
+ assert{ map[:a] = {:b => {:c => 42}} }
110
110
  assert{ map[:a][:b][:c] == 42 }
111
111
  assert{ map['a'][:b][:c] == 42 }
112
112
  assert{ map['a']['b'][:c] == 42 }
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 5
8
- - 0
9
- version: 1.5.0
8
+ - 1
9
+ version: 1.5.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ara T. Howard