ostruct 0.3.1 → 0.6.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2b9fc88b5467418401b1e0a44f555fd467cf3b90dbcd36db7329830afee1b83
4
- data.tar.gz: decc3b34075bc2746bd61fe0498c666dab2154401515fe69a4c1baa0ab2da2a1
3
+ metadata.gz: e36ab53d76adf09c53509a2ac80afb2e8fb7a76fdc18273be6080a32ebf95070
4
+ data.tar.gz: f837cb94bbf9fab791e752e0eacfbbe6f721052a4673441b4f84794c71eae69b
5
5
  SHA512:
6
- metadata.gz: c8166a4d8ec376ec8b8a8fbd7f85e6cc83cb142c06ea9cbb6728011221b2beaa598152f9e9f724d3bf24fb37b89a8261f6728dbeb093b84c8e68887e860824f2
7
- data.tar.gz: 49275721136d6546dba4290e3d14df92b82a84de1e2386d13551d481232da7c82987ddde25ea9baa33761689634d18951a15edce19fb57bb2a9c32ddb1cfbd09
6
+ metadata.gz: 254e895a7bfde536dc044638746d605ff0ac435963a98887015f5ef12ff7b1e5c4cdf1a89d18a956d766dc5ea58fcbfab22f737686c97624cb8de7be0d157166
7
+ data.tar.gz: 0cc6bcb1268e37a81efd0e1fceb08c352e4693d2d799f4dace50bc09b244b2589d3a0bde1e14af04f0183b27b8f9576e2bf479cd258bdb27c8049d359c32b33f
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
4
4
  modification, are permitted provided that the following conditions
5
5
  are met:
6
6
  1. Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
7
+ notice, this list of conditions and the following disclaimer.
8
8
  2. Redistributions in binary form must reproduce the above copyright
9
- notice, this list of conditions and the following disclaimer in the
10
- documentation and/or other materials provided with the distribution.
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
11
 
12
12
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
13
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
data/COPYING ADDED
@@ -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.
data/Gemfile CHANGED
@@ -1,6 +1,10 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in ostruct.gemspec
6
3
  gemspec
4
+
5
+ group :development do
6
+ gem "bundler"
7
+ gem "rake"
8
+ gem "test-unit"
9
+ gem "test-unit-ruby-core"
10
+ end
data/README.md CHANGED
@@ -8,7 +8,7 @@ The `ostruct` library comes pre-packaged with Ruby. No installation is necessary
8
8
 
9
9
  ## Usage
10
10
 
11
- ```
11
+ ```ruby
12
12
  require "ostruct"
13
13
 
14
14
  person = OpenStruct.new
@@ -22,14 +22,14 @@ The `ostruct` library comes pre-packaged with Ruby. No installation is necessary
22
22
 
23
23
  An OpenStruct employs a Hash internally to store the attributes and values and can even be initialized with one:
24
24
 
25
- ```
25
+ ```ruby
26
26
  australia = OpenStruct.new(:country => "Australia", :capital => "Canberra")
27
27
  # => #<OpenStruct country="Australia", capital="Canberra">
28
28
  ```
29
29
 
30
30
  Hash keys with spaces or characters that could normally not be used for method calls (e.g. <code>()[]*</code>) will not be immediately available on the OpenStruct object as a method for retrieval or assignment, but can still be reached through the Object#send method.
31
31
 
32
- ```
32
+ ```ruby
33
33
  measurements = OpenStruct.new("length (in inches)" => 24)
34
34
  measurements.send("length (in inches)") # => 24
35
35
 
@@ -41,7 +41,7 @@ Hash keys with spaces or characters that could normally not be used for method c
41
41
 
42
42
  Removing the presence of an attribute requires the execution of the delete_field method as setting the property value to +nil+ will not remove the attribute.
43
43
 
44
- ```
44
+ ```ruby
45
45
  first_pet = OpenStruct.new(:name => "Rowdy", :owner => "John Smith")
46
46
  second_pet = OpenStruct.new(:name => "Rowdy")
47
47
 
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,9 @@ 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
16
  end
17
17
 
18
18
  task :default => [:test]
data/lib/ostruct.rb CHANGED
@@ -93,21 +93,29 @@
93
93
  # o.methods = [:foo, :bar]
94
94
  # o.methods # => [:foo, :bar]
95
95
  #
96
- # To help remedy clashes, OpenStruct uses only protected/private methods ending with `!`
97
- # and defines aliases for builtin public methods by adding a `!`:
96
+ # To help remedy clashes, OpenStruct uses only protected/private methods ending with <code>!</code>
97
+ # and defines aliases for builtin public methods by adding a <code>!</code>:
98
98
  #
99
99
  # o = OpenStruct.new(make: 'Bentley', class: :luxury)
100
100
  # o.class # => :luxury
101
101
  # o.class! # => OpenStruct
102
102
  #
103
- # It is recommended (but not enforced) to not use fields ending in `!`;
103
+ # It is recommended (but not enforced) to not use fields ending in <code>!</code>;
104
104
  # Note that a subclass' methods may not be overwritten, nor can OpenStruct's own methods
105
- # ending with `!`.
105
+ # ending with <code>!</code>.
106
106
  #
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.6.3"
111
+
112
+ HAS_PERFORMANCE_WARNINGS = begin
113
+ Warning[:performance]
114
+ true
115
+ rescue NoMethodError, ArgumentError
116
+ false
117
+ end
118
+ private_constant :HAS_PERFORMANCE_WARNINGS
111
119
 
112
120
  #
113
121
  # Creates a new OpenStruct object. By default, the resulting OpenStruct
@@ -124,6 +132,10 @@ class OpenStruct
124
132
  # data # => #<OpenStruct country="Australia", capital="Canberra">
125
133
  #
126
134
  def initialize(hash=nil)
135
+ if HAS_PERFORMANCE_WARNINGS && Warning[:performance]
136
+ warn "OpenStruct use is discouraged for performance reasons", uplevel: 1, category: :performance
137
+ end
138
+
127
139
  if hash
128
140
  update_to_values!(hash)
129
141
  else
@@ -197,7 +209,7 @@ class OpenStruct
197
209
  # data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
198
210
  #
199
211
  def each_pair
200
- return to_enum(__method__) { @table.size } unless block_given?
212
+ return to_enum(__method__) { @table.size } unless defined?(yield)
201
213
  @table.each_pair{|p| yield p}
202
214
  self
203
215
  end
@@ -221,8 +233,20 @@ class OpenStruct
221
233
  #
222
234
  def new_ostruct_member!(name) # :nodoc:
223
235
  unless @table.key?(name) || is_method_protected!(name)
224
- define_singleton_method!(name) { @table[name] }
225
- define_singleton_method!("#{name}=") {|x| @table[name] = x}
236
+ if defined?(::Ractor.shareable_proc)
237
+ getter_proc = Ractor.shareable_proc { @table[name] }
238
+ setter_proc = Ractor.shareable_proc {|x| @table[name] = x}
239
+ elsif defined?(::Ractor)
240
+ getter_proc = nil.instance_eval{ Proc.new { @table[name] } }
241
+ setter_proc = nil.instance_eval{ Proc.new {|x| @table[name] = x} }
242
+ ::Ractor.make_shareable(getter_proc)
243
+ ::Ractor.make_shareable(setter_proc)
244
+ else
245
+ getter_proc = Proc.new { @table[name] }
246
+ setter_proc = Proc.new {|x| @table[name] = x}
247
+ end
248
+ define_singleton_method!(name, &getter_proc)
249
+ define_singleton_method!("#{name}=", &setter_proc)
226
250
  end
227
251
  end
228
252
  private :new_ostruct_member!
@@ -233,7 +257,15 @@ class OpenStruct
233
257
  elsif name.match?(/!$/)
234
258
  true
235
259
  else
236
- method!(name).owner < OpenStruct
260
+ owner = method!(name).owner
261
+ if owner.class == ::Class
262
+ owner < ::OpenStruct
263
+ else
264
+ self.class!.ancestors.any? do |mod|
265
+ return false if mod == ::OpenStruct
266
+ mod == owner
267
+ end
268
+ end
237
269
  end
238
270
  end
239
271
 
@@ -250,6 +282,7 @@ class OpenStruct
250
282
  end
251
283
  set_ostruct_member_value!(mname, args[0])
252
284
  elsif len == 0
285
+ @table[mid]
253
286
  else
254
287
  begin
255
288
  super
@@ -264,7 +297,7 @@ class OpenStruct
264
297
  # :call-seq:
265
298
  # ostruct[name] -> object
266
299
  #
267
- # Returns the value of an attribute, or `nil` if there is no such attribute.
300
+ # Returns the value of an attribute, or +nil+ if there is no such attribute.
268
301
  #
269
302
  # require "ostruct"
270
303
  # person = OpenStruct.new("name" => "John Smith", "age" => 70)
@@ -299,7 +332,7 @@ class OpenStruct
299
332
  # Finds and returns the object in nested objects
300
333
  # that is specified by +name+ and +identifiers+.
301
334
  # The nested objects may be instances of various classes.
302
- # See {Dig Methods}[rdoc-ref:doc/dig_methods.rdoc].
335
+ # See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
303
336
  #
304
337
  # Examples:
305
338
  # require "ostruct"
@@ -317,8 +350,10 @@ class OpenStruct
317
350
  end
318
351
 
319
352
  #
320
- # Removes the named field from the object. Returns the value that the field
321
- # contained if it was defined.
353
+ # Removes the named field from the object and returns the value the field
354
+ # contained if it was defined. You may optionally provide a block.
355
+ # If the field is not defined, the result of the block is returned,
356
+ # or a NameError is raised if no block was given.
322
357
  #
323
358
  # require "ostruct"
324
359
  #
@@ -332,14 +367,19 @@ class OpenStruct
332
367
  # person.pension = nil
333
368
  # person # => #<OpenStruct name="John", pension=nil>
334
369
  #
335
- def delete_field(name)
370
+ # person.delete_field('number') # => NameError
371
+ #
372
+ # person.delete_field('number') { 8675_309 } # => 8675309
373
+ #
374
+ def delete_field(name, &block)
336
375
  sym = name.to_sym
337
376
  begin
338
377
  singleton_class.remove_method(sym, "#{sym}=")
339
378
  rescue NameError
340
379
  end
341
380
  @table.delete(sym) do
342
- raise! NameError.new("no field `#{sym}' in #{self}", sym)
381
+ return yield if block
382
+ raise! NameError.new("no field '#{sym}' in #{self}", sym)
343
383
  end
344
384
  end
345
385
 
@@ -430,12 +470,23 @@ class OpenStruct
430
470
  update_to_values!(h)
431
471
  end
432
472
 
433
- # Make all public methods (builtin or our own) accessible with `!`:
434
- instance_methods.each do |method|
473
+ # Make all public methods (builtin or our own) accessible with <code>!</code>:
474
+ give_access = instance_methods
475
+ # See https://github.com/ruby/ostruct/issues/30
476
+ give_access -= %i[instance_exec instance_eval eval] if RUBY_ENGINE == 'jruby'
477
+ give_access.each do |method|
478
+ next if method.match(/\W$/)
479
+
435
480
  new_name = "#{method}!"
436
481
  alias_method new_name, method
437
482
  end
438
483
  # Other builtin private methods we use:
439
484
  alias_method :raise!, :raise
440
485
  private :raise!
486
+
487
+ # See https://github.com/ruby/ostruct/issues/40
488
+ if RUBY_ENGINE != 'jruby'
489
+ alias_method :block_given!, :block_given?
490
+ private :block_given!
491
+ end
441
492
  end
data/ostruct.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  name = File.basename(__FILE__, ".gemspec")
4
- version = ["lib", Array.new(name.count("-")+1, "..").join("/")].find do |dir|
4
+ version = ["lib", Array.new(name.count("-")+1, ".").join("/")].find do |dir|
5
5
  break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
6
6
  /^\s*VERSION\s*=\s*"(.*)"/ =~ line and break $1
7
7
  end rescue nil
@@ -16,14 +16,11 @@ 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
- spec.files = [".gitignore", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "lib/ostruct.rb", "ostruct.gemspec"]
23
- spec.bindir = "exe"
24
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.files = [".gitignore", "Gemfile", "COPYING", "BSDL", "README.md", "Rakefile", "bin/console", "bin/setup", "lib/ostruct.rb", "ostruct.gemspec"]
25
23
  spec.require_paths = ["lib"]
26
24
 
27
- spec.add_development_dependency "bundler"
28
- spec.add_development_dependency "rake"
25
+ spec.metadata["changelog_uri"] = spec.homepage + "/releases"
29
26
  end
metadata CHANGED
@@ -1,43 +1,15 @@
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.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc-Andre Lafortune
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-06 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
11
+ date: 2025-07-17 00:00:00.000000000 Z
12
+ dependencies: []
41
13
  description: Class to build custom data structures, similar to a Hash.
42
14
  email:
43
15
  - ruby-core@marc-andre.ca
@@ -46,8 +18,9 @@ extensions: []
46
18
  extra_rdoc_files: []
47
19
  files:
48
20
  - ".gitignore"
21
+ - BSDL
22
+ - COPYING
49
23
  - Gemfile
50
- - LICENSE.txt
51
24
  - README.md
52
25
  - Rakefile
53
26
  - bin/console
@@ -56,8 +29,10 @@ files:
56
29
  - ostruct.gemspec
57
30
  homepage: https://github.com/ruby/ostruct
58
31
  licenses:
32
+ - Ruby
59
33
  - BSD-2-Clause
60
- metadata: {}
34
+ metadata:
35
+ changelog_uri: https://github.com/ruby/ostruct/releases
61
36
  post_install_message:
62
37
  rdoc_options: []
63
38
  require_paths:
@@ -73,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
48
  - !ruby/object:Gem::Version
74
49
  version: '0'
75
50
  requirements: []
76
- rubygems_version: 3.1.2
51
+ rubygems_version: 3.5.11
77
52
  signing_key:
78
53
  specification_version: 4
79
54
  summary: Class to build custom data structures, similar to a Hash.