mashed 0.9.1 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -0
- data/lib/mashed/mash.rb +3 -1
- data/lib/mashed/stringy_hash.rb +15 -10
- data/lib/mashed/version.rb +1 -1
- data/spec/mashed/stringy_hash_spec.rb +18 -5
- metadata +2 -2
data/README.md
CHANGED
@@ -47,6 +47,18 @@ m.with_indifferent_access # => nil
|
|
47
47
|
# there is no key of "with_indifferent_access" for the internal hash
|
48
48
|
```
|
49
49
|
|
50
|
+
### There is also a StringyHash
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
h = StringyHash.stringify(a: "A", b: "B")
|
54
|
+
|
55
|
+
h["a"] === h[:a] # => true
|
56
|
+
|
57
|
+
h[:something_new] = "foo"
|
58
|
+
h.key?(:something_new) # => true
|
59
|
+
h.key?("something_new") # => true
|
60
|
+
```
|
61
|
+
|
50
62
|
## Contributing
|
51
63
|
|
52
64
|
1. Fork it
|
data/lib/mashed/mash.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "mashed/stringy_hash"
|
2
|
+
|
1
3
|
module Mashed
|
2
4
|
class Mash < BasicObject
|
3
5
|
def singleton_method_added(symbol)
|
@@ -46,7 +48,7 @@ module Mashed
|
|
46
48
|
else
|
47
49
|
hash.to_hash
|
48
50
|
end
|
49
|
-
@hash =
|
51
|
+
@hash = StringyHash.stringify(hash)
|
50
52
|
end
|
51
53
|
|
52
54
|
def is_a?(other)
|
data/lib/mashed/stringy_hash.rb
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
require 'delegate'
|
2
2
|
|
3
3
|
module Mashed
|
4
|
-
|
5
|
-
def stringify
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
class StringyHash < SimpleDelegator
|
5
|
+
def self.stringify(object)
|
6
|
+
if object.is_a?(Array)
|
7
|
+
object.map { |value| StringyHash.stringify(value) }
|
8
|
+
elsif object.is_a?(Hash)
|
9
|
+
StringyHash.new(object.each_with_object({}) do |(k,v), h|
|
10
|
+
h[k.to_s] = StringyHash.stringify(v)
|
11
|
+
end)
|
12
|
+
else
|
13
|
+
object
|
14
|
+
end
|
10
15
|
end
|
11
|
-
end
|
12
16
|
|
13
|
-
class StringyHash < SimpleDelegator
|
14
17
|
def stringify
|
15
18
|
dup
|
16
19
|
end
|
@@ -24,6 +27,10 @@ module Mashed
|
|
24
27
|
end
|
25
28
|
alias store []=
|
26
29
|
|
30
|
+
def key?(key)
|
31
|
+
super(key.to_s)
|
32
|
+
end
|
33
|
+
|
27
34
|
def delete(key, &blk)
|
28
35
|
super(key.to_s, &blk)
|
29
36
|
end
|
@@ -45,5 +52,3 @@ module Mashed
|
|
45
52
|
end
|
46
53
|
end
|
47
54
|
end
|
48
|
-
|
49
|
-
Hash.send :include, Mashed::ExtendHash
|
data/lib/mashed/version.rb
CHANGED
@@ -1,21 +1,34 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Mashed::StringyHash do
|
4
|
-
let(:hash) {{ a: 1, b: 2, c: { three: 3 }}}
|
4
|
+
let(:hash) {{ a: 1, b: 2, c: { three: 3 }, d: [:e, { f: 4 }]}}
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
def stringify
|
7
|
+
Mashed::StringyHash.stringify(hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ".stringify" do
|
11
|
+
it {
|
12
|
+
expect(stringify).to eq({
|
13
|
+
"a" => 1,
|
14
|
+
"b" => 2,
|
15
|
+
"c" => { "three" => 3 },
|
16
|
+
"d" => [:e, { "f" => 4 }]
|
17
|
+
})
|
18
|
+
}
|
8
19
|
end
|
9
20
|
|
10
21
|
describe "get and set" do
|
11
|
-
let(:s) {
|
22
|
+
let(:s) { stringify }
|
12
23
|
before { s[:a] = "A" }
|
13
24
|
it { expect(s[:a]).to eq("A") }
|
14
25
|
it { expect(s["a"]).to eq("A") }
|
26
|
+
it { expect(s.key?(:a)).to be_true }
|
27
|
+
it { expect(s.key?("a")).to be_true }
|
15
28
|
end
|
16
29
|
|
17
30
|
describe "calls to_s on objects" do
|
18
|
-
let(:s) {
|
31
|
+
let(:s) { stringify }
|
19
32
|
let(:klass) {
|
20
33
|
Class.new do
|
21
34
|
def to_s; "a"; end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mashed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.1
|
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: 2014-
|
12
|
+
date: 2014-03-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|