deepstruct 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTY3MzliMTEzZDNiNTNkYjNmZTE0ZDdkMjQ5N2Y2ZDExOWEyZWMyYQ==
5
+ data.tar.gz: !binary |-
6
+ MDI3MjMzNzg2MmY0ZjA5OTc5MzBjN2FiNTk2NjQzNzY1NjU0NTQwMw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjA0NDA4MTFhNDQxZGVkMDMyOWExNGRhMGRmZjM4NTY0ZjNjYzAyMjFiMWZj
10
+ ZDlkNjMyN2JjMGQ5NTZjODEzYzVjNzIwMzQ0ZjEyNDc2NTk5OWYzMTk5NTcy
11
+ ODUzYTEyYTU5NWJlODllOTUzMDcxMWFlNTAwNjdmOThmNjZhNGU=
12
+ data.tar.gz: !binary |-
13
+ MGU2ZTE0ZTExMTdmZTBhMDhiOWFmZjY4ZGQ2MDJmYmQxYzYxYzU4MjY5NzEx
14
+ NDZjNDk1NjM5MTYwODQxZTA5NTM3Nzc1YmQ0MDk0YmY4YWExMTYxODU2NWIw
15
+ MzMzNzczZWNkZGNlMmM2MDFmYzRlMDEzMWMzYjdhMzIzN2NiZTI=
@@ -22,36 +22,44 @@ module DeepStruct
22
22
  "#<#{self.class} #{@value.inspect}>"
23
23
  end
24
24
 
25
- def to_json
26
- @value.to_json
25
+ def to_json(*args)
26
+ @value.to_json(*args)
27
27
  end
28
28
  end
29
29
 
30
30
  class HashWrapper < DeepWrapper
31
- def respond_to?(method, include_private = false)
32
- @value.respond_to?(method, include_private) || self.has_key?(method.to_s.chomp('='))
31
+ def respond_to?(name, include_private = false)
32
+ super || @value.respond_to?(name, include_private) || case name.to_s
33
+ when /\A(.*)\?\z/
34
+ has_key?($1)
35
+ when /\=\z/
36
+ true
37
+ else
38
+ has_key?(name.to_s)
39
+ end
33
40
  end
34
41
 
35
42
  # Given a symbol or a string this yields the variant of the key that
36
43
  # exists in the wrapped hash if any. If none exists (or the key is not
37
44
  # a symbol or string) the input value is passed through unscathed.
38
- def indiffrently(key, &block)
45
+ def indifferently(key, &block)
39
46
  return yield(key) if @value.has_key?(key)
40
47
  return yield(key.to_s) if key.is_a?(Symbol) && @value.has_key?(key.to_s)
41
48
  return yield(key.to_sym) if key.is_a?(String) && @value.has_key?(key.to_sym)
42
49
  return yield(key)
43
50
  end
51
+ alias_method :indiffrently, :indifferently # Fixes spelling error
44
52
 
45
53
  def []=(key, value)
46
- indiffrently(key) { |key| @value[key] = value }
54
+ indifferently(key) { |key| @value[key] = value }
47
55
  end
48
56
 
49
57
  def [](key)
50
- indiffrently(key) { |key| DeepStruct.wrap(@value[key]) }
58
+ indifferently(key) { |key| DeepStruct.wrap(@value[key]) }
51
59
  end
52
60
 
53
61
  def has_key?(key)
54
- indiffrently(key) { |key| @value.has_key?(key) }
62
+ indifferently(key) { |key| @value.has_key?(key) }
55
63
  end
56
64
 
57
65
  def method_missing(method, *args, &block)
@@ -1,3 +1,3 @@
1
1
  module Deepstruct
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -63,6 +63,12 @@ describe DeepStruct do
63
63
  struct.to_json.should eq('{"a":[1,2,3]}')
64
64
  end
65
65
 
66
+ it 'supports being converted to json when inside something else' do
67
+ struct = DeepStruct.wrap({:a => [1,2,3]})
68
+ outer = {hello: struct}
69
+ outer.to_json.should eq('{"hello":{"a":[1,2,3]}}')
70
+ end
71
+
66
72
  it "raises NoMethodError when reading missing keys" do
67
73
  ->{DeepStruct.wrap({}).not_there}.should raise_error(NoMethodError)
68
74
  end
@@ -89,30 +95,42 @@ describe DeepStruct do
89
95
  end
90
96
 
91
97
  context "HashWrapper, #respond_to?" do
92
- it "responds to hash methods" do
93
- struct = DeepStruct.wrap({:a => true})
94
- struct.size.should eq(1)
95
- struct.respond_to?(:size).should be_true
98
+ %w(unwrap size each).each do |method_name|
99
+ it "responds to ##{method_name}" do
100
+ struct = DeepStruct.wrap({:a => true})
101
+ struct.respond_to?(method_name).should be_true
102
+ struct.respond_to?(method_name.to_sym).should be_true
103
+ end
96
104
  end
97
105
 
98
106
  it "responds to keys that are present as symbols" do
99
107
  struct = DeepStruct.wrap({:a => nil})
100
108
  struct.respond_to?(:a).should be_true
109
+ struct.respond_to?('a').should be_true
101
110
  end
102
111
 
103
112
  it "responds to keys that are present as strings" do
104
113
  struct = DeepStruct.wrap({'a' => nil})
105
114
  struct.respond_to?(:a).should be_true
115
+ struct.respond_to?('a').should be_true
106
116
  end
107
117
 
108
118
  it "doesn't respond to missing keys" do
109
119
  struct = DeepStruct.wrap({:a => true})
110
120
  struct.respond_to?(:b).should be_false
121
+ struct.respond_to?('b').should be_false
111
122
  end
112
123
 
113
124
  it "responds to keys that can be assigned to" do
114
125
  struct = DeepStruct.wrap({:a => true})
115
126
  struct.respond_to?(:a=).should be_true
127
+ struct.respond_to?('a=').should be_true
128
+ end
129
+
130
+ it "responds to keys with '?' suffix" do
131
+ struct = DeepStruct.wrap({:a => true})
132
+ struct.respond_to?(:a?).should be_true
133
+ struct.respond_to?('a?').should be_true
116
134
  end
117
135
  end
118
136
 
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deepstruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
5
- prerelease:
4
+ version: 0.0.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Simen Svale Skogsrud
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -63,33 +58,26 @@ files:
63
58
  - spec/spec_helper.rb
64
59
  homepage: ''
65
60
  licenses: []
61
+ metadata: {}
66
62
  post_install_message:
67
63
  rdoc_options: []
68
64
  require_paths:
69
65
  - lib
70
66
  required_ruby_version: !ruby/object:Gem::Requirement
71
- none: false
72
67
  requirements:
73
68
  - - ! '>='
74
69
  - !ruby/object:Gem::Version
75
70
  version: '0'
76
- segments:
77
- - 0
78
- hash: -4475093401064523013
79
71
  required_rubygems_version: !ruby/object:Gem::Requirement
80
- none: false
81
72
  requirements:
82
73
  - - ! '>='
83
74
  - !ruby/object:Gem::Version
84
75
  version: '0'
85
- segments:
86
- - 0
87
- hash: -4475093401064523013
88
76
  requirements: []
89
77
  rubyforge_project: deepstruct
90
- rubygems_version: 1.8.24
78
+ rubygems_version: 2.1.11
91
79
  signing_key:
92
- specification_version: 3
80
+ specification_version: 4
93
81
  summary: A sibling of ostruct that handles deeply nested structures and basic data
94
82
  type detection
95
83
  test_files: