rubysl-ostruct 2.0.4 → 2.1.0

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: 25ef06576d34ac774a46a52f900e3b3057d0e9d5
4
- data.tar.gz: 365deda9a3abdef2615f0057f3e0a7f229a7f24b
3
+ metadata.gz: c12ce42389ead852a5244a59e2fe7d438d304cf8
4
+ data.tar.gz: 4b56e31554e57999035e36558074638397ac0468
5
5
  SHA512:
6
- metadata.gz: bdce30cac56bc487845665ef339d436dcc8147be59bbb9587c2b1a1a2a7aad5486153869a72a05533ad29db3b26ee715f08d853633a3f241af9f6bf898f4ad53
7
- data.tar.gz: 407fe1f2dba5e83592e9701eefa25bf429534676db0d9ab297c948cf7e559f14b22522c724a2c2d418c0671480597fa8ab2741fdf12c4b8ccd4b11a88b85ecfb
6
+ metadata.gz: f05918ea27221e21c2a35c65a0583b0bcbc969270b1345cf5685a45efd122c55a5ecf2f4a4dd1deda9a0de90f9df25a67600a0f2b512ca44f93c568b4b8ea83e
7
+ data.tar.gz: 04ab76f6dadd4019e8b9ca33b5fc7e049368fa67ca8cc3cf63b26c61876f165956f0f4dfb03de562ecd33aba9b81612944011e74c3038cdda0ff8bba6815bd56
@@ -1,6 +1,14 @@
1
1
  language: ruby
2
2
  env:
3
3
  - RUBYLIB=lib
4
- script: bundle exec mspec spec
4
+ - RUBYLIB=
5
+ script: mspec spec
5
6
  rvm:
6
- - rbx-nightly-19mode
7
+ - 2.0.0
8
+ - rbx-2.2.1
9
+ matrix:
10
+ exclude:
11
+ - rvm: 2.0.0
12
+ env: RUBYLIB=lib
13
+ - rvm: rbx-2.2.1
14
+ env: RUBYLIB=
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
@@ -30,12 +30,6 @@
30
30
  # An OpenStruct employs a Hash internally to store the methods and values and
31
31
  # can even be initialized with one:
32
32
  #
33
- # country_data = { :country => "Australia", :population => 20_000_000 }
34
- # australia = OpenStruct.new(country_data)
35
- # p australia # -> <OpenStruct country="Australia" population=20000000>
36
- #
37
- # You may also define the hash in the initialization call:
38
- #
39
33
  # australia = OpenStruct.new(:country => "Australia", :population => 20_000_000)
40
34
  # p australia # -> <OpenStruct country="Australia" population=20000000>
41
35
  #
@@ -67,8 +61,8 @@
67
61
  #
68
62
  # == Implementation:
69
63
  #
70
- # An OpenStruct utilizes Ruby's method lookup structure to and find and define
71
- # the necessary methods for properties. This is accomplished through the method
64
+ # An OpenStruct utilizes Ruby's method lookup structure to find and define the
65
+ # necessary methods for properties. This is accomplished through the method
72
66
  # method_missing and define_method.
73
67
  #
74
68
  # This should be a consideration if there is a concern about the performance of
@@ -80,7 +74,8 @@ class OpenStruct
80
74
  # Creates a new OpenStruct object. By default, the resulting OpenStruct
81
75
  # object will have no attributes.
82
76
  #
83
- # The optional +hash+, if given, will generate attributes and values.
77
+ # The optional +hash+, if given, will generate attributes and values
78
+ # (can be a Hash, an OpenStruct or a Struct).
84
79
  # For example:
85
80
  #
86
81
  # require 'ostruct'
@@ -89,16 +84,10 @@ class OpenStruct
89
84
  #
90
85
  # p data # -> <OpenStruct country="Australia" population=20000000>
91
86
  #
92
- # You may also define the hash in the initialization call:
93
- #
94
- # australia = OpenStruct.new(:country => "Australia",
95
- # :population => 20_000_000)
96
- # p australia # -> <OpenStruct country="Australia" population=20000000>
97
- #
98
87
  def initialize(hash=nil)
99
88
  @table = {}
100
89
  if hash
101
- for k,v in hash
90
+ hash.each_pair do |k, v|
102
91
  @table[k.to_sym] = v
103
92
  new_ostruct_member(k)
104
93
  end
@@ -115,37 +104,39 @@ class OpenStruct
115
104
  #
116
105
  # Converts the OpenStruct to a hash with keys representing
117
106
  # each attribute (as symbols) and their corresponding values
107
+ # Example:
108
+ #
109
+ # require 'ostruct'
110
+ # data = OpenStruct.new("country" => "Australia", :population => 20_000_000)
111
+ # data.to_h # => {:country => "Australia", :population => 20000000 }
112
+ #
118
113
  def to_h
119
114
  @table.dup
120
115
  end
121
116
 
122
117
  #
123
- # Provides marshalling support for use by the Marshal library. Returning the
124
- # underlying Hash table that contains the functions defined as the keys and
125
- # the values assigned to them.
118
+ # Yields all attributes (as a symbol) along with the corresponding values
119
+ # or returns an enumerator if not block is given.
120
+ # Example:
126
121
  #
127
- # require 'ostruct'
122
+ # require 'ostruct'
123
+ # data = OpenStruct.new("country" => "Australia", :population => 20_000_000)
124
+ # data.each_pair.to_a # => [[:country, "Australia"], [:population, 20000000]]
128
125
  #
129
- # person = OpenStruct.new
130
- # person.name = 'John Smith'
131
- # person.age = 70
126
+ def each_pair
127
+ return to_enum __method__ unless block_given?
128
+ @table.each_pair { |p| yield p }
129
+ end
130
+
132
131
  #
133
- # person.marshal_dump # => { :name => 'John Smith', :age => 70 }
132
+ # Provides marshalling support for use by the Marshal library.
134
133
  #
135
134
  def marshal_dump
136
135
  @table
137
136
  end
138
137
 
139
138
  #
140
- # Provides marshalling support for use by the Marshal library. Accepting
141
- # a Hash of keys and values which will be used to populate the internal table
142
- #
143
- # require 'ostruct'
144
- #
145
- # event = OpenStruct.new
146
- # hash = { 'time' => Time.now, 'title' => 'Birthday Party' }
147
- # event.marshal_load(hash)
148
- # event.title # => 'Birthday Party'
139
+ # Provides marshalling support for use by the Marshal library.
149
140
  #
150
141
  def marshal_load(x)
151
142
  @table = x
@@ -153,8 +144,8 @@ class OpenStruct
153
144
  end
154
145
 
155
146
  #
156
- # #modifiable is used internally to check if the OpenStruct is able to be
157
- # modified before granting access to the internal Hash table to be augmented.
147
+ # Used internally to check if the OpenStruct is able to be
148
+ # modified before granting access to the internal Hash table to be modified.
158
149
  #
159
150
  def modifiable
160
151
  begin
@@ -167,39 +158,58 @@ class OpenStruct
167
158
  protected :modifiable
168
159
 
169
160
  #
170
- # new_ostruct_member is used internally to defined properties on the
161
+ # Used internally to defined properties on the
171
162
  # OpenStruct. It does this by using the metaprogramming function
172
- # define_method for both the getter method and the setter method.
163
+ # define_singleton_method for both the getter method and the setter method.
173
164
  #
174
165
  def new_ostruct_member(name)
175
166
  name = name.to_sym
176
- unless self.respond_to?(name)
177
- class << self; self; end.class_eval do
178
- define_method(name) { @table[name] }
179
- define_method("#{name}=") { |x| modifiable[name] = x }
180
- end
167
+ unless respond_to?(name)
168
+ define_singleton_method(name) { @table[name] }
169
+ define_singleton_method("#{name}=") { |x| modifiable[name] = x }
181
170
  end
182
171
  name
183
172
  end
173
+ protected :new_ostruct_member
184
174
 
185
175
  def method_missing(mid, *args) # :nodoc:
186
176
  mname = mid.id2name
187
177
  len = args.length
188
- if mname.chomp!('=') && mid != :[]=
178
+ if mname.chomp!('=')
189
179
  if len != 1
190
180
  raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
191
181
  end
192
182
  modifiable[new_ostruct_member(mname)] = args[0]
193
- elsif len == 0 && mid != :[]
183
+ elsif len == 0
194
184
  @table[mid]
195
185
  else
196
186
  raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1)
197
187
  end
198
188
  end
199
189
 
190
+ # Returns the value of a member.
200
191
  #
201
- # Remove the named field from the object. Returning the value that the field
202
- # contained if it has defined.
192
+ # person = OpenStruct.new('name' => 'John Smith', 'age' => 70)
193
+ # person[:age] # => 70, same as ostruct.age
194
+ #
195
+ def [](name)
196
+ @table[name.to_sym]
197
+ end
198
+
199
+ #
200
+ # Sets the value of a member.
201
+ #
202
+ # person = OpenStruct.new('name' => 'John Smith', 'age' => 70)
203
+ # person[:age] = 42 # => equivalent to ostruct.age = 42
204
+ # person.age # => 42
205
+ #
206
+ def []=(name, value)
207
+ modifiable[new_ostruct_member(name)] = value
208
+ end
209
+
210
+ #
211
+ # Remove the named field from the object. Returns the value that the field
212
+ # contained if it was defined.
203
213
  #
204
214
  # require 'ostruct'
205
215
  #
@@ -209,8 +219,8 @@ class OpenStruct
209
219
  #
210
220
  def delete_field(name)
211
221
  sym = name.to_sym
212
- @table.delete sym
213
222
  singleton_class.__send__(:remove_method, sym, "#{name}=")
223
+ @table.delete sym
214
224
  end
215
225
 
216
226
  InspectKey = :__inspect_key__ # :nodoc:
@@ -246,11 +256,28 @@ class OpenStruct
246
256
 
247
257
  #
248
258
  # Compares this object and +other+ for equality. An OpenStruct is equal to
249
- # +other+ when +other+ is an OpenStruct and the two object's Hash tables are
259
+ # +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
250
260
  # equal.
251
261
  #
252
262
  def ==(other)
253
- return false unless(other.kind_of?(OpenStruct))
254
- return @table == other.table
263
+ return false unless other.kind_of?(OpenStruct)
264
+ @table == other.table
265
+ end
266
+
267
+ #
268
+ # Compares this object and +other+ for equality. An OpenStruct is eql? to
269
+ # +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
270
+ # eql?.
271
+ #
272
+ def eql?(other)
273
+ return false unless other.kind_of?(OpenStruct)
274
+ @table.eql?(other.table)
275
+ end
276
+
277
+ # Compute a hash-code for this OpenStruct.
278
+ # Two hashes with the same content will have the same hash code
279
+ # (and will be eql?).
280
+ def hash
281
+ @table.hash
255
282
  end
256
283
  end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  class OpenStruct
3
- VERSION = "2.0.4"
3
+ VERSION = "2.1.0"
4
4
  end
5
5
  end
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "mspec", "~> 1.5"
24
+ spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
24
25
  end
@@ -0,0 +1,19 @@
1
+ require 'ostruct'
2
+
3
+ describe "OpenStruct#each_pair" do
4
+ before :each do
5
+ @os = OpenStruct.new("a" => 1, :b => 2)
6
+ end
7
+
8
+ it "returns an enumerator if no block given" do
9
+ enum = @os.each_pair
10
+ enum.should be_an_instance_of(enumerator_class)
11
+ enum.to_a.should == [[:a, 1], [:b, 2]]
12
+ end
13
+
14
+ it "yields the key and value of each pair to a block expecting |key, value|" do
15
+ all_args = []
16
+ @os.each_pair { |key, value| all_args << [key, value] }
17
+ all_args.sort.should == [[:a, 1], [:b, 2]]
18
+ end
19
+ end
@@ -2,19 +2,14 @@ require "ostruct"
2
2
 
3
3
  describe "OpenStruct#[]" do
4
4
  before :each do
5
- @os = OpenStruct.new
5
+ @os = OpenStruct.new(:foo => 42)
6
6
  end
7
7
 
8
- ruby_version_is ""..."2.0" do
9
- it "raises a NoMethodError" do
10
- lambda { @os[:foo] }.should raise_error(NoMethodError)
11
- end
8
+ it "returns the associated value using a symbol" do
9
+ @os[:foo].should == 42
12
10
  end
13
11
 
14
- ruby_version_is "2.0" do
15
- it "returns the associated value" do
16
- @os.foo = 42
17
- @os[:foo].should == 42
18
- end
12
+ it "returns the associated value using a string" do
13
+ @os["foo"].should == 42
19
14
  end
20
15
  end
@@ -2,20 +2,26 @@ require "ostruct"
2
2
 
3
3
  describe "OpenStruct#[]=" do
4
4
  before :each do
5
- @os = OpenStruct.new
5
+ @os = OpenStruct.new(:bar => 100)
6
6
  end
7
7
 
8
- ruby_bug "redmine:4179", "1.9.2" do
9
- ruby_version_is ""..."2.0" do
10
- it "raises a NoMethodError" do
11
- lambda { @os[:foo] = 2 }.should raise_error(NoMethodError)
12
- end
13
- end
14
- ruby_version_is "2.0" do
15
- it "sets the associated value" do
16
- @os[:foo] = 42
17
- @os.foo.should == 42
18
- end
19
- end
8
+ it "sets the associated value using a symbol" do
9
+ @os[:foo] = 42
10
+ @os.foo.should == 42
11
+ end
12
+
13
+ it "sets the associated value using a string" do
14
+ @os["foo"] = 42
15
+ @os.foo.should == 42
16
+ end
17
+
18
+ it "updates the associated value using a symbol" do
19
+ @os[:bar] = 42
20
+ @os.bar.should == 42
21
+ end
22
+
23
+ it "updates the associated value using a string" do
24
+ @os["bar"] = 42
25
+ @os.bar.should == 42
20
26
  end
21
27
  end
@@ -0,0 +1,22 @@
1
+ require 'ostruct'
2
+
3
+ describe "OpenStruct#eql?" do
4
+ before :each do
5
+ @os = OpenStruct.new(:a => 1, :b => 2)
6
+ end
7
+
8
+ it "returns true when the passed argument is an OpenStruct and contains the same hash table" do
9
+ os2 = OpenStruct.new(:a => 1, :b => 2)
10
+ @os.should eql(os2)
11
+ end
12
+
13
+ it "returns false when the passed argument is an OpenStruct and contains a different hash table" do
14
+ os2 = OpenStruct.new(:a => 2, :b => 2)
15
+ @os.should_not eql(os2)
16
+ end
17
+
18
+ it "returns false when the passed argument isn't an OpenStruct" do
19
+ h = {:a => 1, :b => 2}
20
+ @os.should_not eql(h)
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ require 'ostruct'
2
+
3
+ describe "OpenStruct#hash" do
4
+ before :each do
5
+ @os = OpenStruct.new(:a => 1)
6
+ @os2 = OpenStruct.new
7
+ end
8
+
9
+ it "returns a fixnum" do
10
+ @os.hash.should be_an_instance_of(Fixnum)
11
+ end
12
+
13
+ it "returns the same fixnum for OpenStruct with the same content" do
14
+ @os.hash.should_not == @os2.hash
15
+
16
+ @os2.a = 1
17
+ @os.hash.should == @os2.hash
18
+ end
19
+ end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-ostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-07 00:00:00.000000000 Z
11
+ date: 2015-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
+ type: :development
16
+ prerelease: false
15
17
  requirement: !ruby/object:Gem::Requirement
16
18
  requirements:
17
- - - ~>
19
+ - - "~>"
18
20
  - !ruby/object:Gem::Version
19
21
  version: '1.3'
20
- type: :development
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.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
+ type: :development
30
+ prerelease: false
29
31
  requirement: !ruby/object:Gem::Requirement
30
32
  requirements:
31
- - - ~>
33
+ - - "~>"
32
34
  - !ruby/object:Gem::Version
33
35
  version: '10.0'
34
- type: :development
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: mspec
43
+ type: :development
44
+ prerelease: false
43
45
  requirement: !ruby/object:Gem::Requirement
44
46
  requirements:
45
- - - ~>
47
+ - - "~>"
46
48
  - !ruby/object:Gem::Version
47
49
  version: '1.5'
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
48
57
  type: :development
49
58
  prerelease: false
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '2.0'
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ~>
66
+ - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '1.5'
68
+ version: '2.0'
55
69
  description: Ruby standard library ostruct.
56
70
  email:
57
71
  - brixen@gmail.com
@@ -59,10 +73,11 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - .gitignore
63
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".travis.yml"
64
78
  - Gemfile
65
79
  - LICENSE
80
+ - MRI_LICENSE
66
81
  - README.md
67
82
  - Rakefile
68
83
  - lib/ostruct.rb
@@ -71,11 +86,14 @@ files:
71
86
  - lib/rubysl/ostruct/version.rb
72
87
  - rubysl-ostruct.gemspec
73
88
  - spec/delete_field_spec.rb
89
+ - spec/each_pair_spec.rb
74
90
  - spec/element_reference_spec.rb
75
91
  - spec/element_set_spec.rb
92
+ - spec/eql_spec.rb
76
93
  - spec/equal_value_spec.rb
77
94
  - spec/fixtures/classes.rb
78
95
  - spec/frozen_spec.rb
96
+ - spec/hash_spec.rb
79
97
  - spec/initialize_copy_spec.rb
80
98
  - spec/initialize_spec.rb
81
99
  - spec/inspect_spec.rb
@@ -98,27 +116,30 @@ require_paths:
98
116
  - lib
99
117
  required_ruby_version: !ruby/object:Gem::Requirement
100
118
  requirements:
101
- - - ~>
119
+ - - "~>"
102
120
  - !ruby/object:Gem::Version
103
121
  version: '2.0'
104
122
  required_rubygems_version: !ruby/object:Gem::Requirement
105
123
  requirements:
106
- - - '>='
124
+ - - ">="
107
125
  - !ruby/object:Gem::Version
108
126
  version: '0'
109
127
  requirements: []
110
128
  rubyforge_project:
111
- rubygems_version: 2.0.7
129
+ rubygems_version: 2.4.8
112
130
  signing_key:
113
131
  specification_version: 4
114
132
  summary: Ruby standard library ostruct.
115
133
  test_files:
116
134
  - spec/delete_field_spec.rb
135
+ - spec/each_pair_spec.rb
117
136
  - spec/element_reference_spec.rb
118
137
  - spec/element_set_spec.rb
138
+ - spec/eql_spec.rb
119
139
  - spec/equal_value_spec.rb
120
140
  - spec/fixtures/classes.rb
121
141
  - spec/frozen_spec.rb
142
+ - spec/hash_spec.rb
122
143
  - spec/initialize_copy_spec.rb
123
144
  - spec/initialize_spec.rb
124
145
  - spec/inspect_spec.rb
@@ -131,4 +152,3 @@ test_files:
131
152
  - spec/table_spec.rb
132
153
  - spec/to_h_spec.rb
133
154
  - spec/to_s_spec.rb
134
- has_rdoc: