mashed 0.9.1 → 1.0.1

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.
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 = hash.stringify
51
+ @hash = StringyHash.stringify(hash)
50
52
  end
51
53
 
52
54
  def is_a?(other)
@@ -1,16 +1,19 @@
1
1
  require 'delegate'
2
2
 
3
3
  module Mashed
4
- module ExtendHash
5
- def stringify
6
- StringyHash.new(dup.each_with_object({}) do |(k,v), h|
7
- v = v.stringify if v.respond_to?(:stringify)
8
- h[k.to_s] = v
9
- end)
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
@@ -1,3 +1,3 @@
1
1
  module Mashed
2
- VERSION = "0.9.1"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -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
- describe "#stringify" do
7
- it { expect(hash.stringify).to eq("a" => 1, "b" => 2, "c" => { "three" => 3 }) }
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) { hash.stringify }
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) { hash.stringify }
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.9.1
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-02-12 00:00:00.000000000 Z
12
+ date: 2014-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler