Skip to content

Commit 1694c3d

Browse files
committed
Replace ostruct with custom version
Avoid future deprecations, behavior changes, and performance warnings. Closes #1545 See #1525
1 parent e458be0 commit 1694c3d

File tree

18 files changed

+93
-16
lines changed

18 files changed

+93
-16
lines changed

benchmarks/struct_vs_ostruct.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'benchmark'
2+
require 'ostruct'
3+
require_relative '../lib/yard'
4+
5+
n = 100000
6+
class MyStruct < Struct.new(:a, :b, :c); end
7+
ostruct = OpenStruct.new
8+
yostruct = YARD::OpenStruct.new
9+
mystruct = MyStruct.new
10+
11+
Benchmark.bmbm do |x|
12+
x.report("Struct.new(args)") { n.times { MyStruct.new 1, 2, 3 } }
13+
x.report("Struct (assign)") { n.times { mystruct.a = 1 } }
14+
x.report("Struct (read)") { n.times { mystruct.a } }
15+
x.report("OpenStruct.new(args)") { n.times { OpenStruct.new a: 1, b: 2, c: 3 } }
16+
x.report("OpenStruct.new (blank)") { n.times { OpenStruct.new } }
17+
x.report("OpenStruct (assign)") { n.times { ostruct.a = 1 } }
18+
x.report("OpenStruct (read)") { n.times { ostruct.a } }
19+
x.report("YARD::OpenStruct.new(args)") { n.times { YARD::OpenStruct.new a: 1, b: 2, c: 3 } }
20+
x.report("YARD::OpenStruct.new (blank)") { n.times { YARD::OpenStruct.new } }
21+
x.report("YARD::OpenStruct (assign)") { n.times { yostruct.a = 1 } }
22+
x.report("YARD::OpenStruct (read)") { n.times { yostruct.a } }
23+
end

lib/yard/autoload.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ module Markup # Namespace for markup providers
298298
autoload :DocstringParser, __p('docstring_parser')
299299
autoload :GemIndex, __p('gem_index')
300300
autoload :Logger, __p('logging')
301+
autoload :OpenStruct, __p('open_struct')
301302
autoload :Options, __p('options')
302303
autoload :Registry, __p('registry')
303304
autoload :RegistryResolver, __p('registry_resolver')

lib/yard/code_objects/macro_object.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
32

43
module YARD
54
module CodeObjects

lib/yard/docstring_parser.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
32

43
module YARD
54
# Parses text and creates a {Docstring} object to represent documentation

lib/yard/handlers/processor.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
32

43
module YARD
54
module Handlers

lib/yard/open_struct.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module YARD
2+
# An OpenStruct compatible struct class that allows for basic access of attributes
3+
# via +struct.attr_name+ and +struct.attr_name = value+.
4+
class OpenStruct
5+
def initialize(hash = {})
6+
@table = hash.each_pair { |k, v| [k.to_sym, v] }
7+
end
8+
9+
# @private
10+
def method_missing(name, *args)
11+
if name.to_s.end_with?('=')
12+
varname = name.to_s[0..-2].to_sym
13+
__cache_lookup__(varname)
14+
self[varname] = args.first
15+
else
16+
__cache_lookup__(name)
17+
self[name]
18+
end
19+
end
20+
21+
def to_h
22+
@table.dup
23+
end
24+
25+
def ==(other)
26+
other.is_a?(self.class) && to_h == other.to_h
27+
end
28+
29+
def hash
30+
@table.hash
31+
end
32+
33+
def dig(*keys)
34+
@table.dig(*keys)
35+
end
36+
37+
def []=(key, value)
38+
@table[key.to_sym] = value
39+
end
40+
41+
def [](key)
42+
@table[key.to_sym]
43+
end
44+
45+
def each_pair(&block)
46+
@table.each_pair(&block)
47+
end
48+
49+
def marshal_dump
50+
@table
51+
end
52+
53+
def marshal_load(data)
54+
@table = data
55+
end
56+
57+
private
58+
59+
def __cache_lookup__(name)
60+
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
61+
def #{name}; @table[:#{name}]; end
62+
def #{name}=(v); @table[:#{name}] = v; end
63+
RUBY
64+
end
65+
end
66+
end

lib/yard/parser/source_parser.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22
require 'stringio'
3-
require 'ostruct'
43

54
module YARD
65
module Parser

lib/yard/tags/directives.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
32

43
module YARD
54
module Tags

lib/yard/templates/engine.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
32

43
module YARD
54
module Templates

lib/yard/templates/template_options.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
32

43
module YARD
54
module Templates

0 commit comments

Comments
 (0)