ostruct 0.3.3 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +10 -3
  3. data/lib/ostruct.rb +37 -15
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed2da7f517c77c1e08ec0927885068879a2d304aebb6c804d5a28937d96ee272
4
- data.tar.gz: a4f0bfd0ffd31eec0fb1bd54e0037738758dee9026e6f847713cc35019fdfcb2
3
+ metadata.gz: 012af2cd126f44caa849cff5dc4ae721494c6cc65226ad2637901f95e23142cb
4
+ data.tar.gz: 21d664abb828274f6ff897ad4a5ff7f9b9d568bebcd4eaf270357491ebed4148
5
5
  SHA512:
6
- metadata.gz: 0bd53f08d2b20e753ad004948b2a22ff47956d3fb33424d826e77f672686027bcca16de8e81336dcc0d44233eb6bdb1885e2b5e5467d2d279333f9b98b241e2b
7
- data.tar.gz: 1ff610eb44962ec773f04cb1c3ba158b257d1c08a0bd18af5e17d0da2c5036d7c1e1aea56104a25a089c2d820a043069a925f084cc61058211249f94ee28837e
6
+ metadata.gz: 501fe10289a94a64f481ce487f2575392f5faaa6edb6815427f3b143574baaebf5026887b88af7a1d867da85298dd692c1cc352f2ae6099fd60466c0644a7809
7
+ data.tar.gz: d47056c31c89ec72e73fffd52da97f699200fa58202d274af0a4d11239a4d13e61177031349b84907e68946f4600284057402da344036b47f46d7db83a90f61e
data/Rakefile CHANGED
@@ -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
@@ -93,21 +93,21 @@
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.3"
110
+ VERSION = "0.5.2"
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,17 @@ 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
+ if defined?(::Ractor)
225
+ getter_proc = nil.instance_eval{ Proc.new { @table[name] } }
226
+ setter_proc = nil.instance_eval{ Proc.new {|x| @table[name] = x} }
227
+ ::Ractor.make_shareable(getter_proc)
228
+ ::Ractor.make_shareable(setter_proc)
229
+ else
230
+ getter_proc = Proc.new { @table[name] }
231
+ setter_proc = Proc.new {|x| @table[name] = x}
232
+ end
233
+ define_singleton_method!(name, &getter_proc)
234
+ define_singleton_method!("#{name}=", &setter_proc)
226
235
  end
227
236
  end
228
237
  private :new_ostruct_member!
@@ -273,7 +282,7 @@ class OpenStruct
273
282
  # :call-seq:
274
283
  # ostruct[name] -> object
275
284
  #
276
- # Returns the value of an attribute, or `nil` if there is no such attribute.
285
+ # Returns the value of an attribute, or +nil+ if there is no such attribute.
277
286
  #
278
287
  # require "ostruct"
279
288
  # person = OpenStruct.new("name" => "John Smith", "age" => 70)
@@ -308,7 +317,7 @@ class OpenStruct
308
317
  # Finds and returns the object in nested objects
309
318
  # that is specified by +name+ and +identifiers+.
310
319
  # The nested objects may be instances of various classes.
311
- # See {Dig Methods}[rdoc-ref:doc/dig_methods.rdoc].
320
+ # See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
312
321
  #
313
322
  # Examples:
314
323
  # require "ostruct"
@@ -326,8 +335,10 @@ class OpenStruct
326
335
  end
327
336
 
328
337
  #
329
- # Removes the named field from the object. Returns the value that the field
330
- # contained if it was defined.
338
+ # Removes the named field from the object and returns the value the field
339
+ # contained if it was defined. You may optionally provide a block.
340
+ # If the field is not defined, the result of the block is returned,
341
+ # or a NameError is raised if no block was given.
331
342
  #
332
343
  # require "ostruct"
333
344
  #
@@ -341,6 +352,10 @@ class OpenStruct
341
352
  # person.pension = nil
342
353
  # person # => #<OpenStruct name="John", pension=nil>
343
354
  #
355
+ # person.delete_field('number') # => NameError
356
+ #
357
+ # person.delete_field('number') { 8675_309 } # => 8675309
358
+ #
344
359
  def delete_field(name)
345
360
  sym = name.to_sym
346
361
  begin
@@ -348,6 +363,7 @@ class OpenStruct
348
363
  rescue NameError
349
364
  end
350
365
  @table.delete(sym) do
366
+ return yield if block_given!
351
367
  raise! NameError.new("no field `#{sym}' in #{self}", sym)
352
368
  end
353
369
  end
@@ -439,12 +455,18 @@ class OpenStruct
439
455
  update_to_values!(h)
440
456
  end
441
457
 
442
- # Make all public methods (builtin or our own) accessible with `!`:
443
- instance_methods.each do |method|
458
+ # Make all public methods (builtin or our own) accessible with <code>!</code>:
459
+ give_access = instance_methods
460
+ # See https://github.com/ruby/ostruct/issues/30
461
+ give_access -= %i[instance_exec instance_eval eval] if RUBY_ENGINE == 'jruby'
462
+ give_access.each do |method|
463
+ next if method.match(/\W$/)
464
+
444
465
  new_name = "#{method}!"
445
466
  alias_method new_name, method
446
467
  end
447
468
  # Other builtin private methods we use:
448
469
  alias_method :raise!, :raise
449
- private :raise!
470
+ alias_method :block_given!, :block_given?
471
+ private :raise!, :block_given!
450
472
  end
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.3
4
+ version: 0.5.2
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: 2021-01-13 00:00:00.000000000 Z
11
+ date: 2021-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler