octothorpe 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75ff578c9887b8a62a0effcc8e51233eefb7743e
4
- data.tar.gz: c449257f2bed3d03ee82b53653771e22711d312e
3
+ metadata.gz: 1dc7649e0f220470c6ec828bdeb73cd870f0e1b6
4
+ data.tar.gz: b1828f26f8d683bdc0347eec4ede391320874cca
5
5
  SHA512:
6
- metadata.gz: 3cb263ec2df833ac54a34cc838b267bd198062cb93c3db6c6ee383fdce4943eb06c9b34f0e0703d79f53aa8c056c8899172fb32339c029fc61b4ad94685911d9
7
- data.tar.gz: 34b01fb89862be06e9c92e2e801c11247b14f4a6cb61b29027d9cec421610225aa4ccde24fbbc55e400de67397c0f6087b3d755bf04db5b3570bebb5772494ab
6
+ metadata.gz: 13788c201cf67ca740757ef58fd693d6b43bef1580b2ff25e3ee7fcc75c9ad642710a0d04b93f98dcdb03836bb826df8f75be8dbc3bef65442004ae1e5ffdf77
7
+ data.tar.gz: d8197ac01d73730e3a6a9c3066a48fb8057eca3284c8e4196358b5b9a0d70f8dc6ed349fecf342cdaacbce832219f14c99f446ccea1d6dc1fab7e2e422ec12c5
data/README.md CHANGED
@@ -74,8 +74,8 @@ hash && (hash[:key] || {})[4]
74
74
  ... and for some reason Ruby's new lonely operator is a problem, then this might just possibly be
75
75
  of use.
76
76
 
77
- Alternatively you might try an OpenStruct, Rails' HashWithIndifferentAccess, the Hashie gem or the
78
- AndAnd gem.
77
+ Alternatively you might try a Struct. (Really, if you can use a Struct, then that would be better).
78
+ Or an OpenStruct, Rails' HashWithIndifferentAccess, or the Hashie gem.
79
79
 
80
80
  ### Why Read-Only?
81
81
 
@@ -41,7 +41,7 @@ class Octothorpe
41
41
  def_delegators :@inner_hash, :select, :map, :reject, :inject
42
42
 
43
43
  # Gem version number
44
- VERSION = '0.4.0'
44
+ VERSION = '0.4.1'
45
45
 
46
46
 
47
47
  # Generic Octothorpe error class
@@ -67,7 +67,7 @@ class Octothorpe
67
67
 
68
68
  def method_missing(method, *attrs)
69
69
  super if (::Kernel.block_given? || !attrs.empty?)
70
- @octothorpe_store[method.to_sym]
70
+ @octothorpe_store[method]
71
71
  end
72
72
 
73
73
  end
@@ -98,11 +98,11 @@ class Octothorpe
98
98
  #
99
99
  # You can use >> to access member objects in somewhat the same way as an OpenStruct.
100
100
  #
101
- # ot = Octotghorpe.new(one: 1, "two" => 2)
101
+ # ot = Octothorpe.new(one: 1, "two" => 2)
102
102
  # ot.>>.one # -> 1
103
103
  #
104
- # This will not work for members that have keys with spaces in, or keys which have the same name
105
- # as methods on Object. Use _get_ for those.
104
+ # This will not work for members that have keys with spaces in, keys which have the same name as
105
+ # methods on Object, or keys that aren't String or Symbol. Use _get_ for those.
106
106
  #
107
107
  def >>; @store; end
108
108
 
@@ -118,7 +118,7 @@ class Octothorpe
118
118
  # Unlike >>, this works for keys with spaces, or keys that have the same name as methods on
119
119
  # Object.
120
120
  #
121
- def get(key); @store.octothorpe_store[key.to_sym]; end
121
+ def get(key); @store.octothorpe_store[octokey key]; end
122
122
 
123
123
  alias send get
124
124
  alias [] get
@@ -150,7 +150,7 @@ class Octothorpe
150
150
  raise Frozen if self.frozen?
151
151
 
152
152
  klass = args.shift unless block_given?
153
- keys = args.map(&:to_sym)
153
+ keys = args.map{|k| octokey k}
154
154
 
155
155
  if block_given?
156
156
  keys.each{|k| @store.octothorpe_store[k] ||= yield k }
@@ -229,10 +229,10 @@ class Octothorpe
229
229
  if thing.kind_of?(Octothorpe)
230
230
  thing.to_h
231
231
  else
232
- thing.each_with_object({}) {|(k,v),m| m[k.to_sym] = v }
232
+ thing.each_with_object({}) {|(k,v),m| m[octokey k] = v }
233
233
  end
234
234
  rescue
235
- raise BadHash
235
+ raise BadHash, $!
236
236
  end
237
237
 
238
238
 
@@ -246,5 +246,13 @@ class Octothorpe
246
246
  thisHash.send(method, otherHash)
247
247
  end
248
248
 
249
+
250
+ ##
251
+ # Munge a potential key so we can use it
252
+ #
253
+ def octokey(thing)
254
+ thing.is_a?(String) ? thing.to_sym : thing
255
+ end
256
+
249
257
  end
250
258
 
@@ -1,4 +1,5 @@
1
1
  require 'octothorpe'
2
+ require 'date'
2
3
 
3
4
  describe Octothorpe do
4
5
 
@@ -28,6 +29,13 @@ describe Octothorpe do
28
29
  expect{ Octothorpe.new(14) }.to raise_exception Octothorpe::BadHash
29
30
  end
30
31
 
32
+ it "accepts a hash with a nil or non-string key" do
33
+ expect{ Octothorpe.new( {nil=>"foo"} ) }.not_to raise_exception
34
+ expect{ Octothorpe.new( {1=>"foo"} ) }.not_to raise_exception
35
+ expect{ Octothorpe.new( {Date.new=>"foo"} ) }.not_to raise_exception
36
+ expect{ Octothorpe.new( {Octothorpe.new=>"foo"} ) }.not_to raise_exception
37
+ end
38
+
31
39
  end
32
40
 
33
41
 
@@ -66,6 +74,15 @@ describe Octothorpe do
66
74
  expect( @ot.get('weird key') ).to eq @hash2[:'weird key']
67
75
  end
68
76
 
77
+ it "will accept nil or non-string keys" do
78
+ hash = { nil => "nil", Date.new => "date", Octothorpe.new => "ot" }
79
+ ot = Octothorpe.new(hash)
80
+
81
+ hash.each do |k,v|
82
+ expect( ot.get(k) ).to eq v
83
+ end
84
+ end
85
+
69
86
  end
70
87
 
71
88
 
@@ -99,6 +116,13 @@ describe Octothorpe do
99
116
  expect( @ot.guard(Array, :foo) ).to eq @ot
100
117
  end
101
118
 
119
+ it "will accept nil or non-string keys" do
120
+ hash = { nil => "nil", Date.new => "date", Octothorpe.new => "ot" }
121
+ ot = Octothorpe.new(hash)
122
+
123
+ expect{ ot.guard(Hash, *hash.keys) }.not_to raise_exception
124
+ end
125
+
102
126
  context "when given a class" do
103
127
 
104
128
  it "accepts a class and list of keys" do
@@ -148,7 +172,6 @@ describe Octothorpe do
148
172
 
149
173
  end
150
174
 
151
-
152
175
  end
153
176
 
154
177
 
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octothorpe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-28 00:00:00.000000000 Z
11
+ date: 2017-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry-doc
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: |
@@ -93,9 +93,9 @@ executables: []
93
93
  extensions: []
94
94
  extra_rdoc_files: []
95
95
  files:
96
- - .hgignore
97
- - .hgtags
98
- - .rspec
96
+ - ".hgignore"
97
+ - ".hgtags"
98
+ - ".rspec"
99
99
  - Gemfile
100
100
  - LICENSE.txt
101
101
  - README.md
@@ -115,17 +115,17 @@ require_paths:
115
115
  - lib
116
116
  required_ruby_version: !ruby/object:Gem::Requirement
117
117
  requirements:
118
- - - '>='
118
+ - - ">="
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - '>='
123
+ - - ">="
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
126
  requirements: []
127
127
  rubyforge_project:
128
- rubygems_version: 2.4.8
128
+ rubygems_version: 2.6.12
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Like a Hash. Better for message passing between classes, I hope.
@@ -133,4 +133,3 @@ test_files:
133
133
  - spec/doc_no_pending.rb
134
134
  - spec/octothorpe_spec.rb
135
135
  - spec/spec_helper.rb
136
- has_rdoc: