production_open_struct 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +10 -0
- data/MIT-LICENSE +20 -0
- data/README.md +56 -0
- data/VERSION +1 -0
- data/lib/production_open_struct.rb +47 -0
- data/production_open_struct.gemspec +32 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d66f2bc476482745d878fdd7ddcbeca5eb7ee08f3f6f455449913fee661f848b
|
4
|
+
data.tar.gz: 52799916fea68a7a9c0fc57cdabfdc9a62aff4f16c0014cb1291bf50e07725a4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d074f8eea4c9a220c41394b91a69123a43e04f9018293af6c87f2d798806628ace0a9ebdb05855da01c8c13d75bd4ea280321d7f5540e520e1847eb7d3501484
|
7
|
+
data.tar.gz: 3c6ed9917bebdbb928ec8dff696eac4ea9e626c60ea552b0841e9ec8ae6100eb6df9a05dcb9cddf4a5bb8c58a02d76e9d8a9ede92b33fcf1427267a0605d1794
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## 1.0.0
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Override OpenStuct to not define singleton methods which clear the Ruby method cache.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2021 Brian Durand
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
[![Continuous Integration](https://github.com/bdurand/production_open_struct/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/bdurand/production_open_struct/actions/workflows/continuous_integration.yml)
|
2
|
+
[![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
|
3
|
+
|
4
|
+
# Production OpenStuct
|
5
|
+
|
6
|
+
This gem overrides behavior in the [OpenStruct](https://github.com/ruby/ostruct) implementation defined in the Ruby standard library. While OpenStruct can be useful for one off scripts or as testing mocks, it can cause performance issues in production environments.
|
7
|
+
|
8
|
+
OpenStruct defines singleton methods on every object created for each key in the hash. Defining these methods is slow and it busts the global method cache used by the Ruby VM requiring it to be rebuilt after every OpenStruct object is created.
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
# This defines methods "foo" and "foo=" on object and busts the method cache
|
12
|
+
object = OpenStruct.new(foo: "bar")
|
13
|
+
object.foo # => "bar"
|
14
|
+
object.foo = "biz"
|
15
|
+
object.foo # => "bix"
|
16
|
+
```
|
17
|
+
|
18
|
+
It is not a good idea to use OpenStruct in your production code. It is much more efficient and safer to just define some simple classes or use `Struct` rather than using OpenStruct. However, not everyone sticks to this and you can end up with external libraries in your application that do use OpenStruct.
|
19
|
+
|
20
|
+
This gem solves the OpenStruct performance issues by simply overriding the code in OpenStruct that defines singleton methods. This doesn't have any functional affect on OpenStruct objects; you can still use the attribute reader and writer methods. However, these will now go through `method_missing` every time you call them. This does add its own overhead but it is far more performant in most cases than defining dynamic methods. Furthermore, the global method cache is no longer impacted by creating OpenStruct objects.
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Nothing is needed to use this gem other than requiring it.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require "production_open_struct"
|
28
|
+
```
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'production_open_struct'
|
36
|
+
```
|
37
|
+
|
38
|
+
And then execute:
|
39
|
+
```bash
|
40
|
+
$ bundle
|
41
|
+
```
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
```bash
|
45
|
+
$ gem install production_open_struct
|
46
|
+
```
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Open a pull request on GitHub.
|
51
|
+
|
52
|
+
Please use the [standardrb](https://github.com/testdouble/standard) syntax and lint your code with `standardrb --fix` before submitting.
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ostruct"
|
4
|
+
|
5
|
+
# Overrides OpenStruct behavior to no longer define singleton methods on each object.
|
6
|
+
module ProductionOpenStruct
|
7
|
+
def delete_field(name)
|
8
|
+
sym = name.to_sym
|
9
|
+
@table.delete(sym) do
|
10
|
+
return yield if block_given!
|
11
|
+
raise! NameError.new("no field `#{sym}' in #{self}", sym)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def new_ostruct_member!(name)
|
18
|
+
# no-op to avoid defining singleton methods that will bust the method cache.
|
19
|
+
name.to_sym
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(method_name, *args) # :nodoc:
|
23
|
+
len = args.length
|
24
|
+
if method_name.to_s.end_with?("=")
|
25
|
+
if len != 1
|
26
|
+
raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
27
|
+
end
|
28
|
+
self[method_name.to_s.chomp("=")] = args[0]
|
29
|
+
elsif len == 0
|
30
|
+
@table[method_name]
|
31
|
+
else
|
32
|
+
begin
|
33
|
+
super
|
34
|
+
rescue NoMethodError => err
|
35
|
+
err.backtrace.shift
|
36
|
+
raise!
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def respond_to_missing?(method_name, include_private = false)
|
42
|
+
key = method_name.to_s.chomp("=").to_sym
|
43
|
+
@table.include?(key) || super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
OpenStruct.prepend(ProductionOpenStruct) unless ENV["PRODUCTION_OPEN_STRUCT_AUTO_INCLUDE"] == "false"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "production_open_struct"
|
3
|
+
spec.version = File.read(File.expand_path("../VERSION", __FILE__)).strip
|
4
|
+
spec.authors = ["Brian Durand"]
|
5
|
+
spec.email = ["bbdurand@gmail.com"]
|
6
|
+
|
7
|
+
spec.summary = "Modifies OpenStruct so that it doesn't define singleton methods on each object which busts the Ruby method cache which can cause performance issues in production applications."
|
8
|
+
spec.homepage = "https://github.com/bdurand/production_open_struct"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
# Specify which files should be added to the gem when it is released.
|
12
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
13
|
+
ignore_files = %w[
|
14
|
+
.
|
15
|
+
Appraisals
|
16
|
+
Gemfile
|
17
|
+
Gemfile.lock
|
18
|
+
Rakefile
|
19
|
+
bin/
|
20
|
+
gemfiles/
|
21
|
+
spec/
|
22
|
+
]
|
23
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| ignore_files.any? { |path| f.start_with?(path) } }
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler"
|
30
|
+
|
31
|
+
spec.required_ruby_version = ">= 2.5"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: production_open_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Durand
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-08-26 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
|
+
description:
|
28
|
+
email:
|
29
|
+
- bbdurand@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CHANGELOG.md
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- VERSION
|
38
|
+
- lib/production_open_struct.rb
|
39
|
+
- production_open_struct.gemspec
|
40
|
+
homepage: https://github.com/bdurand/production_open_struct
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '2.5'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.1.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Modifies OpenStruct so that it doesn't define singleton methods on each object
|
63
|
+
which busts the Ruby method cache which can cause performance issues in production
|
64
|
+
applications.
|
65
|
+
test_files: []
|