mashed 0.5.1 → 0.9.0

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/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ # - jruby-18mode # JRuby in 1.8 mode
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ # - rbx-18mode
8
+ # - rbx-19mode
9
+ # uncomment this line if your project needs to run something other than `rake`:
10
+ # script: bundle exec rspec spec
data/README.md CHANGED
@@ -33,6 +33,20 @@ tasks.each do |task|
33
33
  end
34
34
  ```
35
35
 
36
+ ### A Mash Is Not A Hash
37
+
38
+ ```ruby
39
+ Mash = Mashed::Mash
40
+
41
+ m = Mash.new(a: "A", b: "B")
42
+ m.map { |key, value| puts [key, value].inspect } # => raise
43
+
44
+ # assuming activesupport is present...
45
+
46
+ m.with_indifferent_access # => nil
47
+ # there is no key of "with_indifferent_access" for the internal hash
48
+ ```
49
+
36
50
  ## Contributing
37
51
 
38
52
  1. Fork it
@@ -40,3 +54,28 @@ end
40
54
  3. Commit your changes (`git commit -am 'Add some feature'`)
41
55
  4. Push to the branch (`git push origin my-new-feature`)
42
56
  5. Create new Pull Request
57
+
58
+ ## StringyHash
59
+
60
+ To help make the Mash work there also is a StringyHash class. It's a
61
+ simple delegate to the normal Hash except that it tries to enforce that
62
+ all incoming and outgoing keys are strings.
63
+
64
+ ### Usage
65
+
66
+ ```ruby
67
+ StringyHash = Mash::StringyHash
68
+
69
+ sh = StringyHash.new(title: "Hello", starred: false, completed_at: nil)
70
+ sh.keys # => ["title", "starred", "completed_at"]
71
+ sh[:title] # => "Hello"
72
+ sh["title"] # => "Hello"
73
+
74
+ class Title
75
+ def to_s
76
+ "title"
77
+ end
78
+ end
79
+
80
+ sh[Title.new] # => "Hello"
81
+ ```
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/mashed/mash.rb CHANGED
@@ -54,34 +54,26 @@ module Mashed
54
54
  end
55
55
  alias kind_of? is_a?
56
56
 
57
- def [](key)
58
- key = key.to_s
59
- wrap_up @hash[key]
60
- end
61
-
62
- def []=(key, value)
63
- key = key.to_s
64
- @hash[key] = value
65
- end
66
-
67
- def keys
68
- @hash.keys
69
- end
70
-
71
57
  def delete(key)
72
58
  wrap_up @hash.delete(key)
73
59
  end
74
60
 
75
61
  def method_missing(symbol, *args, &blk)
76
62
  string = symbol.to_s
77
- if @hash.key?(string)
78
- self[string]
79
- elsif string =~ /\?$/
80
- !!self[string[0..-2]]
81
- elsif string =~ /=$/
63
+ if blk
64
+ super
65
+ elsif args.length == 0
66
+ if @hash.key?(string)
67
+ self[string]
68
+ elsif string =~ /\?$/
69
+ !!self[string[0..-2]]
70
+ else
71
+ nil
72
+ end
73
+ elsif args.length == 1 && string =~ /=$/
82
74
  self[string[0..-2]] = args.first
83
75
  else
84
- nil
76
+ super
85
77
  end
86
78
  end
87
79
 
@@ -104,6 +96,20 @@ module Mashed
104
96
 
105
97
  private
106
98
 
99
+ def [](key)
100
+ key = key.to_s
101
+ wrap_up @hash[key]
102
+ end
103
+
104
+ def []=(key, value)
105
+ key = key.to_s
106
+ @hash[key] = value
107
+ end
108
+
109
+ def keys
110
+ @hash.keys
111
+ end
112
+
107
113
  def wrap_up(thing)
108
114
  if thing.respond_to?(:to_ary)
109
115
  thing.map { |t| wrap(t) }
@@ -1,3 +1,3 @@
1
1
  module Mashed
2
- VERSION = "0.5.1"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -11,18 +11,9 @@ describe Mashed::Mash do
11
11
  it { expect(mash.class).to be(Mashed::Mash) }
12
12
  end
13
13
 
14
- describe "#keys" do
15
- it { expect(mash.keys).to eq(["a", "b", "c"]) }
16
- end
17
-
18
14
  describe "#delete" do
19
15
  before { mash.delete(:a) }
20
- it { expect(mash.keys).to eq(["b", "c"]) }
21
- end
22
-
23
- describe "#[]" do
24
- it { expect(mash[:a]).to eq(1) }
25
- it { expect(mash["b"]).to eq(2) }
16
+ it { expect(mash.methods).to include("b", "c") }
26
17
  end
27
18
 
28
19
  describe "#method_missing" do
@@ -31,6 +22,18 @@ describe Mashed::Mash do
31
22
  expect(mash.b).to eq(2)
32
23
  expect(mash.c).to eq(3)
33
24
  }
25
+
26
+ context "for actual missing keys" do
27
+ it { expect(mash.d).to be_nil }
28
+ end
29
+
30
+ context "for methods with an arity > 0 that are not setters" do
31
+ it { expect { mash.something(1) }.to raise_error(StandardError) }
32
+ end
33
+
34
+ context "for methods with block" do
35
+ it { expect { mash.map { |k,v| [k,v] } }.to raise_error(StandardError) }
36
+ end
34
37
  end
35
38
 
36
39
  describe "#method_missing=" do
@@ -44,7 +47,7 @@ describe Mashed::Mash do
44
47
 
45
48
  describe "respond_to?" do
46
49
  it { expect(mash.respond_to?(:a)).to be_true }
47
- it { expect(mash.respond_to?(:[])).to be_true }
50
+ it { expect(mash.respond_to?(:delete)).to be_true }
48
51
  it { expect(mash.respond_to?(:to_hash)).to be_true }
49
52
  end
50
53
 
@@ -89,4 +92,15 @@ describe Mashed::Mash do
89
92
  mash.to_json
90
93
  }
91
94
  end
95
+
96
+ context "private methods" do
97
+ describe "#keys" do
98
+ it { expect(mash.__send__(:keys)).to eq(["a", "b", "c"]) }
99
+ end
100
+
101
+ describe "#[]" do
102
+ it { expect(mash.__send__(:[],:a)).to eq(1) }
103
+ it { expect(mash.__send__(:[],"b")).to eq(2) }
104
+ end
105
+ end
92
106
  end
@@ -13,4 +13,14 @@ describe Mashed::StringyHash do
13
13
  it { expect(s[:a]).to eq("A") }
14
14
  it { expect(s["a"]).to eq("A") }
15
15
  end
16
+
17
+ describe "calls to_s on objects" do
18
+ let(:s) { hash.stringify }
19
+ let(:klass) {
20
+ Class.new do
21
+ def to_s; "a"; end
22
+ end
23
+ }
24
+ it { expect(s[klass.new]).to eq(1) }
25
+ end
16
26
  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.5.1
4
+ version: 0.9.0
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-01-05 00:00:00.000000000 Z
12
+ date: 2014-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - .gitignore
70
70
  - .rspec
71
71
  - .ruby-version
72
+ - .travis.yml
72
73
  - Gemfile
73
74
  - LICENSE.txt
74
75
  - README.md