ostruct 0.3.1 → 0.5.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +12 -5
  3. data/lib/ostruct.rb +32 -9
  4. data/ostruct.gemspec +1 -1
  5. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2b9fc88b5467418401b1e0a44f555fd467cf3b90dbcd36db7329830afee1b83
4
- data.tar.gz: decc3b34075bc2746bd61fe0498c666dab2154401515fe69a4c1baa0ab2da2a1
3
+ metadata.gz: 21e6b4898eb983f5662872ec9b061babc748c196756225e561a2606f1be43f92
4
+ data.tar.gz: ed5975f18423c5db6e0cc0262ce17aa8a625a9488ab8a3a0a556287e71e7881d
5
5
  SHA512:
6
- metadata.gz: c8166a4d8ec376ec8b8a8fbd7f85e6cc83cb142c06ea9cbb6728011221b2beaa598152f9e9f724d3bf24fb37b89a8261f6728dbeb093b84c8e68887e860824f2
7
- data.tar.gz: 49275721136d6546dba4290e3d14df92b82a84de1e2386d13551d481232da7c82987ddde25ea9baa33761689634d18951a15edce19fb57bb2a9c32ddb1cfbd09
6
+ metadata.gz: d1993c5247e4d0aa91ef62d1cdc943eb7e95b18a74abc0c6739c611ee4676c32a792801bb7b5f2f51c9992799863a5a6aeee66ad7ed69dd0f5b5b6ee0e227a2b
7
+ data.tar.gz: 119f99ac4213fe46116ffba0c142af7cbba91c7b7e7273ff4fd154585df5f31aa2fdc0048fd4dddc8de21e4ab3e95e1214da40fbb2608126a795f83a2162c628
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
1
  task :release do
2
- cur = `git branch --show-current`
2
+ cur = `git branch --show-current`.chomp
3
3
  if cur != 'master'
4
4
  puts 'Release only from master branch'
5
- exit(1)
5
+ exit(-1)
6
6
  end
7
7
  end
8
8
 
@@ -10,9 +10,16 @@ require "bundler/gem_tasks"
10
10
  require "rake/testtask"
11
11
 
12
12
  Rake::TestTask.new(:test) do |t|
13
- t.libs << "test" << "test/lib"
14
- t.libs << "lib"
15
- t.test_files = FileList['test/**/test_*.rb']
13
+ t.libs << "test/lib"
14
+ t.ruby_opts << "-rhelper"
15
+ t.test_files = FileList["test/**/test_*.rb"]
16
+ end
17
+
18
+ task :sync_tool do
19
+ require 'fileutils'
20
+ FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
21
+ FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
22
+ FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
16
23
  end
17
24
 
18
25
  task :default => [:test]
data/lib/ostruct.rb CHANGED
@@ -107,7 +107,7 @@
107
107
  # For all these reasons, consider not using OpenStruct at all.
108
108
  #
109
109
  class OpenStruct
110
- VERSION = "0.3.1"
110
+ VERSION = "0.5.0"
111
111
 
112
112
  #
113
113
  # Creates a new OpenStruct object. By default, the resulting OpenStruct
@@ -197,7 +197,7 @@ class OpenStruct
197
197
  # data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
198
198
  #
199
199
  def each_pair
200
- return to_enum(__method__) { @table.size } unless block_given?
200
+ return to_enum(__method__) { @table.size } unless block_given!
201
201
  @table.each_pair{|p| yield p}
202
202
  self
203
203
  end
@@ -221,8 +221,14 @@ class OpenStruct
221
221
  #
222
222
  def new_ostruct_member!(name) # :nodoc:
223
223
  unless @table.key?(name) || is_method_protected!(name)
224
- define_singleton_method!(name) { @table[name] }
225
- define_singleton_method!("#{name}=") {|x| @table[name] = x}
224
+ getter_proc = Proc.new { @table[name] }
225
+ setter_proc = Proc.new {|x| @table[name] = x}
226
+ if defined?(::Ractor)
227
+ ::Ractor.make_shareable(getter_proc)
228
+ ::Ractor.make_shareable(setter_proc)
229
+ end
230
+ define_singleton_method!(name, &getter_proc)
231
+ define_singleton_method!("#{name}=", &setter_proc)
226
232
  end
227
233
  end
228
234
  private :new_ostruct_member!
@@ -233,7 +239,15 @@ class OpenStruct
233
239
  elsif name.match?(/!$/)
234
240
  true
235
241
  else
236
- method!(name).owner < OpenStruct
242
+ owner = method!(name).owner
243
+ if owner.class == ::Class
244
+ owner < ::OpenStruct
245
+ else
246
+ self.class.ancestors.any? do |mod|
247
+ return false if mod == ::OpenStruct
248
+ mod == owner
249
+ end
250
+ end
237
251
  end
238
252
  end
239
253
 
@@ -250,6 +264,7 @@ class OpenStruct
250
264
  end
251
265
  set_ostruct_member_value!(mname, args[0])
252
266
  elsif len == 0
267
+ @table[mid]
253
268
  else
254
269
  begin
255
270
  super
@@ -299,7 +314,7 @@ class OpenStruct
299
314
  # Finds and returns the object in nested objects
300
315
  # that is specified by +name+ and +identifiers+.
301
316
  # The nested objects may be instances of various classes.
302
- # See {Dig Methods}[rdoc-ref:doc/dig_methods.rdoc].
317
+ # See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
303
318
  #
304
319
  # Examples:
305
320
  # require "ostruct"
@@ -317,8 +332,10 @@ class OpenStruct
317
332
  end
318
333
 
319
334
  #
320
- # Removes the named field from the object. Returns the value that the field
321
- # contained if it was defined.
335
+ # Removes the named field from the object and returns the value the field
336
+ # contained if it was defined. You may optionally provide a block.
337
+ # If the field is not defined, the result of the block is returned,
338
+ # or a NameError is raised if no block was given.
322
339
  #
323
340
  # require "ostruct"
324
341
  #
@@ -332,6 +349,10 @@ class OpenStruct
332
349
  # person.pension = nil
333
350
  # person # => #<OpenStruct name="John", pension=nil>
334
351
  #
352
+ # person.delete_field('number') # => NameError
353
+ #
354
+ # person.delete_field('number') { 8675_309 } # => 8675309
355
+ #
335
356
  def delete_field(name)
336
357
  sym = name.to_sym
337
358
  begin
@@ -339,6 +360,7 @@ class OpenStruct
339
360
  rescue NameError
340
361
  end
341
362
  @table.delete(sym) do
363
+ return yield if block_given!
342
364
  raise! NameError.new("no field `#{sym}' in #{self}", sym)
343
365
  end
344
366
  end
@@ -437,5 +459,6 @@ class OpenStruct
437
459
  end
438
460
  # Other builtin private methods we use:
439
461
  alias_method :raise!, :raise
440
- private :raise!
462
+ alias_method :block_given!, :block_given?
463
+ private :raise!, :block_given!
441
464
  end
data/ostruct.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.summary = %q{Class to build custom data structures, similar to a Hash.}
17
17
  spec.description = %q{Class to build custom data structures, similar to a Hash.}
18
18
  spec.homepage = "https://github.com/ruby/ostruct"
19
- spec.license = "BSD-2-Clause"
19
+ spec.licenses = ["Ruby", "BSD-2-Clause"]
20
20
  spec.required_ruby_version = ">= 2.5.0"
21
21
 
22
22
  spec.files = [".gitignore", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "lib/ostruct.rb", "ostruct.gemspec"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc-Andre Lafortune
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-06 00:00:00.000000000 Z
11
+ date: 2021-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,6 +56,7 @@ files:
56
56
  - ostruct.gemspec
57
57
  homepage: https://github.com/ruby/ostruct
58
58
  licenses:
59
+ - Ruby
59
60
  - BSD-2-Clause
60
61
  metadata: {}
61
62
  post_install_message:
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubygems_version: 3.1.2
77
+ rubygems_version: 3.3.0.dev
77
78
  signing_key:
78
79
  specification_version: 4
79
80
  summary: Class to build custom data structures, similar to a Hash.