dynamic_class 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +179 -0
- data/Rakefile +11 -0
- data/bin/benchmark.rb +145 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dynamic_class.gemspec +27 -0
- data/lib/dynamic_class/version.rb +3 -0
- data/lib/dynamic_class.rb +118 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f0bcefb083d8034817163abd4a89cea881857f76
|
4
|
+
data.tar.gz: f022a9ff7afefd4f40383b5e2be213a9510dab95
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1743ad57ee809cc4900c37b33d02e97838570ba5e408aa5f9a2f690b5c071b0cdb6bff9582e8e2f2d88fdb6d726966632c6f7e5392c5b91f78b11eca718f4027
|
7
|
+
data.tar.gz: dddc7b18eb8ca3a59439fb72df86e756384a8e94b0b7bdf1cef88835ec8bc64cd687e3983a32182024249c5f3a24f27290e7a2af301cb6dd5cfebacdf4a9a785
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at ariel.caplan@mail.yu.edu. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 amcaplan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
# DynamicClass
|
2
|
+
|
3
|
+
Many developers use `OpenStruct` as a convenient way of consuming APIs through
|
4
|
+
a nifty data object. But the performance penalty is pretty awful.
|
5
|
+
|
6
|
+
`DynamicClass` offers a better solution, optimizing for the case where you
|
7
|
+
need to create objects with the same set of properties every time, but you
|
8
|
+
can't define the needed keys until runtime. `DynamicClass` works by defining
|
9
|
+
instance methods on the class every time it encounters a new propery.
|
10
|
+
|
11
|
+
Let's see it in action:
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
Animal = DynamicClass.new do
|
15
|
+
def speak
|
16
|
+
"The #{type} makes a #{sound} sound!"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
dog = Animal.new(type: 'dog', sound: 'woof')
|
21
|
+
# => #<Animal:0x007fdb2b818ba8 @type="dog", @sound="woof">
|
22
|
+
dog.speak
|
23
|
+
# => The dog makes a woof sound!
|
24
|
+
|
25
|
+
cat = Animal.new
|
26
|
+
# => #<Animal:0x007fdb2b83b180>
|
27
|
+
cat.to_h
|
28
|
+
# => {:type=>nil, :sound=>nil}
|
29
|
+
# The class has been changed by the dog!
|
30
|
+
```
|
31
|
+
|
32
|
+
Because methods are defined on the class (unlike `OpenStruct` which defines
|
33
|
+
methods on the object's singleton class), there is no need to define a method
|
34
|
+
more than once. This means that, past the first time a property is added,
|
35
|
+
the cost of setting a property drops.
|
36
|
+
|
37
|
+
The results are pretty astounding. Here are the results of the benchmark in
|
38
|
+
`bin/benchmark.rb` (including a few other `OpenStruct`-like solutions for
|
39
|
+
comparison):
|
40
|
+
|
41
|
+
```
|
42
|
+
Initialization benchmark
|
43
|
+
|
44
|
+
Calculating -------------------------------------
|
45
|
+
OpenStruct 11.183k i/100ms
|
46
|
+
PersistentOpenStruct 46.448k i/100ms
|
47
|
+
OpenFastStruct 47.295k i/100ms
|
48
|
+
DynamicClass 47.797k i/100ms
|
49
|
+
RegularClass 101.410k i/100ms
|
50
|
+
-------------------------------------------------
|
51
|
+
OpenStruct 138.431k (±13.8%) i/s - 682.163k
|
52
|
+
PersistentOpenStruct 757.737k (± 5.3%) i/s - 3.809M
|
53
|
+
OpenFastStruct 783.310k (± 6.0%) i/s - 3.925M
|
54
|
+
DynamicClass 766.130k (± 3.6%) i/s - 3.872M
|
55
|
+
RegularClass 3.037M (± 7.2%) i/s - 15.110M
|
56
|
+
|
57
|
+
Comparison:
|
58
|
+
RegularClass: 3037473.6 i/s
|
59
|
+
OpenFastStruct: 783309.5 i/s - 3.88x slower
|
60
|
+
DynamicClass: 766129.7 i/s - 3.96x slower
|
61
|
+
PersistentOpenStruct: 757736.8 i/s - 4.01x slower
|
62
|
+
OpenStruct: 138430.6 i/s - 21.94x slower
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
Assignment Benchmark
|
67
|
+
|
68
|
+
Calculating -------------------------------------
|
69
|
+
OpenStruct 107.675k i/100ms
|
70
|
+
PersistentOpenStruct 108.952k i/100ms
|
71
|
+
OpenFastStruct 59.163k i/100ms
|
72
|
+
DynamicClass 133.406k i/100ms
|
73
|
+
RegularClass 134.345k i/100ms
|
74
|
+
-------------------------------------------------
|
75
|
+
OpenStruct 3.511M (± 4.2%) i/s - 17.551M
|
76
|
+
PersistentOpenStruct 3.491M (± 4.3%) i/s - 17.432M
|
77
|
+
OpenFastStruct 950.760k (± 4.8%) i/s - 4.792M
|
78
|
+
DynamicClass 8.891M (± 5.7%) i/s - 44.291M
|
79
|
+
RegularClass 8.939M (± 5.8%) i/s - 44.603M
|
80
|
+
|
81
|
+
Comparison:
|
82
|
+
RegularClass: 8939463.2 i/s
|
83
|
+
DynamicClass: 8890563.0 i/s - 1.01x slower
|
84
|
+
OpenStruct: 3511253.2 i/s - 2.55x slower
|
85
|
+
PersistentOpenStruct: 3491119.3 i/s - 2.56x slower
|
86
|
+
OpenFastStruct: 950760.2 i/s - 9.40x slower
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
Access Benchmark
|
91
|
+
|
92
|
+
Calculating -------------------------------------
|
93
|
+
OpenStruct 121.935k i/100ms
|
94
|
+
PersistentOpenStruct 122.673k i/100ms
|
95
|
+
OpenFastStruct 111.492k i/100ms
|
96
|
+
DynamicClass 136.066k i/100ms
|
97
|
+
RegularClass 135.946k i/100ms
|
98
|
+
-------------------------------------------------
|
99
|
+
OpenStruct 5.603M (± 6.1%) i/s - 27.923M
|
100
|
+
PersistentOpenStruct 5.613M (± 5.8%) i/s - 27.969M
|
101
|
+
OpenFastStruct 3.683M (± 6.9%) i/s - 18.396M
|
102
|
+
DynamicClass 9.674M (± 5.8%) i/s - 48.167M
|
103
|
+
RegularClass 9.809M (± 5.5%) i/s - 48.941M
|
104
|
+
|
105
|
+
Comparison:
|
106
|
+
RegularClass: 9808944.8 i/s
|
107
|
+
DynamicClass: 9674457.9 i/s - 1.01x slower
|
108
|
+
PersistentOpenStruct: 5612626.6 i/s - 1.75x slower
|
109
|
+
OpenStruct: 5603365.4 i/s - 1.75x slower
|
110
|
+
OpenFastStruct: 3683298.4 i/s - 2.66x slower
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
All-Together Benchmark
|
115
|
+
|
116
|
+
Calculating -------------------------------------
|
117
|
+
OpenStruct 11.371k i/100ms
|
118
|
+
PersistentOpenStruct 41.683k i/100ms
|
119
|
+
OpenFastStruct 30.589k i/100ms
|
120
|
+
DynamicClass 52.041k i/100ms
|
121
|
+
RegularClass 100.225k i/100ms
|
122
|
+
-------------------------------------------------
|
123
|
+
OpenStruct 136.382k (±14.6%) i/s - 659.518k
|
124
|
+
PersistentOpenStruct 647.752k (± 4.4%) i/s - 3.251M
|
125
|
+
OpenFastStruct 416.183k (± 5.4%) i/s - 2.080M
|
126
|
+
DynamicClass 827.546k (± 4.3%) i/s - 4.163M
|
127
|
+
RegularClass 3.034M (± 6.6%) i/s - 15.134M
|
128
|
+
|
129
|
+
Comparison:
|
130
|
+
RegularClass: 3033808.8 i/s
|
131
|
+
DynamicClass: 827546.3 i/s - 3.67x slower
|
132
|
+
PersistentOpenStruct: 647751.5 i/s - 4.68x slower
|
133
|
+
OpenFastStruct: 416183.1 i/s - 7.29x slower
|
134
|
+
OpenStruct: 136382.0 i/s - 22.24x slower
|
135
|
+
```
|
136
|
+
|
137
|
+
`DynamicClass` is still behind plain old Ruby classes, but it's the best (or
|
138
|
+
effectively tied for best) out of the pack when it comes to `OpenStruct` and
|
139
|
+
friends.
|
140
|
+
|
141
|
+
## Installation
|
142
|
+
|
143
|
+
Add this line to your application's Gemfile:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
gem 'dynamic_class'
|
147
|
+
```
|
148
|
+
|
149
|
+
And then execute:
|
150
|
+
|
151
|
+
$ bundle
|
152
|
+
|
153
|
+
Or install it yourself as:
|
154
|
+
|
155
|
+
$ gem install dynamic_class
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
160
|
+
`rake spec` to run the tests. You can run the benchmark using `rake benchmark`.
|
161
|
+
You can also run `bin/console` for an interactive prompt that will allow you to
|
162
|
+
experiment.
|
163
|
+
|
164
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
165
|
+
|
166
|
+
## Contributing
|
167
|
+
|
168
|
+
Bug reports and pull requests are welcome. This project is intended to be a
|
169
|
+
safe, welcoming space for collaboration, and contributors are expected to adhere
|
170
|
+
to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
171
|
+
|
172
|
+
For functionality changes or bug fixes, please include tests. For performance
|
173
|
+
enhancements, please run the benchmarks and include results in your pull
|
174
|
+
request.
|
175
|
+
|
176
|
+
## License
|
177
|
+
|
178
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
179
|
+
|
data/Rakefile
ADDED
data/bin/benchmark.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'benchmark/ips'
|
2
|
+
require 'dynamic_class'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'persistent_open_struct'
|
5
|
+
require 'ofstruct'
|
6
|
+
|
7
|
+
class RegularClass
|
8
|
+
attr_accessor :foo
|
9
|
+
|
10
|
+
def initialize(args)
|
11
|
+
@foo = args[:foo]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
MyDynamicClass = DynamicClass.new
|
16
|
+
|
17
|
+
puts "Initialization benchmark\n\n"
|
18
|
+
|
19
|
+
Benchmark.ips do |x|
|
20
|
+
input_hash = { foo: :bar }
|
21
|
+
|
22
|
+
x.report('OpenStruct') do
|
23
|
+
OpenStruct.new(input_hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
x.report('PersistentOpenStruct') do
|
27
|
+
PersistentOpenStruct.new(input_hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
x.report('OpenFastStruct') do
|
31
|
+
OpenFastStruct.new(input_hash)
|
32
|
+
end
|
33
|
+
|
34
|
+
x.report('DynamicClass') do
|
35
|
+
MyDynamicClass.new(input_hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
x.report('RegularClass') do
|
39
|
+
RegularClass.new(input_hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
x.compare!
|
43
|
+
end
|
44
|
+
|
45
|
+
puts "\n\nAssignment Benchmark\n\n"
|
46
|
+
|
47
|
+
Benchmark.ips do |x|
|
48
|
+
os = OpenStruct.new(foo: :bar)
|
49
|
+
pos = PersistentOpenStruct.new(foo: :bar)
|
50
|
+
ofs = OpenFastStruct.new(foo: :bar)
|
51
|
+
dc = MyDynamicClass.new(foo: :bar)
|
52
|
+
rgc = RegularClass.new(foo: :bar)
|
53
|
+
|
54
|
+
x.report('OpenStruct') do
|
55
|
+
os.foo = :bar
|
56
|
+
end
|
57
|
+
|
58
|
+
x.report('PersistentOpenStruct') do
|
59
|
+
pos.foo = :bar
|
60
|
+
end
|
61
|
+
|
62
|
+
x.report('OpenFastStruct') do
|
63
|
+
ofs.foo = :bar
|
64
|
+
end
|
65
|
+
|
66
|
+
x.report('DynamicClass') do
|
67
|
+
dc.foo = :bar
|
68
|
+
end
|
69
|
+
|
70
|
+
x.report('RegularClass') do
|
71
|
+
rgc.foo = :bar
|
72
|
+
end
|
73
|
+
|
74
|
+
x.compare!
|
75
|
+
end
|
76
|
+
|
77
|
+
puts "\n\nAccess Benchmark\n\n"
|
78
|
+
|
79
|
+
Benchmark.ips do |x|
|
80
|
+
os = OpenStruct.new(foo: :bar)
|
81
|
+
pos = PersistentOpenStruct.new(foo: :bar)
|
82
|
+
ofs = OpenFastStruct.new(foo: :bar)
|
83
|
+
dc = MyDynamicClass.new(foo: :bar)
|
84
|
+
rgc = RegularClass.new(foo: :bar)
|
85
|
+
|
86
|
+
x.report('OpenStruct') do
|
87
|
+
os.foo
|
88
|
+
end
|
89
|
+
|
90
|
+
x.report('PersistentOpenStruct') do
|
91
|
+
pos.foo
|
92
|
+
end
|
93
|
+
|
94
|
+
x.report('OpenFastStruct') do
|
95
|
+
ofs.foo
|
96
|
+
end
|
97
|
+
|
98
|
+
x.report('DynamicClass') do
|
99
|
+
dc.foo
|
100
|
+
end
|
101
|
+
|
102
|
+
x.report('RegularClass') do
|
103
|
+
rgc.foo
|
104
|
+
end
|
105
|
+
|
106
|
+
x.compare!
|
107
|
+
end
|
108
|
+
|
109
|
+
puts "\n\nAll-Together Benchmark\n\n"
|
110
|
+
|
111
|
+
Benchmark.ips do |x|
|
112
|
+
input_hash = { foo: :bar }
|
113
|
+
|
114
|
+
x.report('OpenStruct') do
|
115
|
+
os = OpenStruct.new(input_hash)
|
116
|
+
os.foo = :bar
|
117
|
+
os.foo
|
118
|
+
end
|
119
|
+
|
120
|
+
x.report('PersistentOpenStruct') do
|
121
|
+
pos = PersistentOpenStruct.new(input_hash)
|
122
|
+
pos.foo = :bar
|
123
|
+
pos.foo
|
124
|
+
end
|
125
|
+
|
126
|
+
x.report('OpenFastStruct') do
|
127
|
+
ofs = OpenFastStruct.new(input_hash)
|
128
|
+
ofs.foo = :bar
|
129
|
+
ofs.foo
|
130
|
+
end
|
131
|
+
|
132
|
+
x.report('DynamicClass') do
|
133
|
+
dc = MyDynamicClass.new(input_hash)
|
134
|
+
dc.foo = :bar
|
135
|
+
dc.foo
|
136
|
+
end
|
137
|
+
|
138
|
+
x.report('RegularClass') do
|
139
|
+
rgc = RegularClass.new(input_hash)
|
140
|
+
rgc.foo = :bar
|
141
|
+
rgc.foo
|
142
|
+
end
|
143
|
+
|
144
|
+
x.compare!
|
145
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dynamic_class"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dynamic_class/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dynamic_class"
|
8
|
+
spec.version = DynamicClass::VERSION
|
9
|
+
spec.authors = ["amcaplan"]
|
10
|
+
spec.email = ["ariel.caplan@mail.yu.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{Create classes that define themselves... eventually.}
|
13
|
+
spec.description = %q{Specifically designed as an OpenStruct-like tool for consuming APIs, dynamic_class lets your classes define their own getters and setters at runtime based on the data instances receive at instantiation.}
|
14
|
+
spec.homepage = "https://github.com/amcaplan/dynamic_class"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "ofstruct", "~> 0.2"
|
25
|
+
spec.add_development_dependency "persistent_open_struct", "~> 0.0"
|
26
|
+
spec.add_development_dependency "benchmark-ips", "~> 2.3.0"
|
27
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require "dynamic_class/version"
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
module DynamicClass
|
5
|
+
def self.new(&block)
|
6
|
+
::Class.new(::DynamicClass::Class).tap do |klass|
|
7
|
+
klass.class_exec(&block) if block_given?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Class
|
12
|
+
class << self
|
13
|
+
def attributes
|
14
|
+
@attributes ||= Set.new
|
15
|
+
end
|
16
|
+
|
17
|
+
# Always revert to original #to_h in case the parent class has already
|
18
|
+
# redefined #to_h.
|
19
|
+
def inherited(subclass)
|
20
|
+
subclass.class_eval <<-RUBY
|
21
|
+
def to_h
|
22
|
+
{}.tap do |hash|
|
23
|
+
each_pair do |key, value|
|
24
|
+
hash[key] = value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
RUBY
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(attributes = {})
|
33
|
+
attributes.each_pair do |key, value|
|
34
|
+
send(:[]=, key, value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_h
|
39
|
+
{}.tap do |hash|
|
40
|
+
each_pair do |key, value|
|
41
|
+
hash[key] = value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def []=(key, value)
|
47
|
+
key = key.to_sym
|
48
|
+
instance_variable_set(:"@#{key}", value)
|
49
|
+
add_methods!(key) unless self.class.attributes.include?(key)
|
50
|
+
end
|
51
|
+
|
52
|
+
def [](key)
|
53
|
+
instance_variable_get(:"@#{key}")
|
54
|
+
end
|
55
|
+
|
56
|
+
def each_pair
|
57
|
+
return to_enum(__method__) { self.class.attributes.size } unless block_given?
|
58
|
+
self.class.attributes.map do |attribute|
|
59
|
+
yield(attribute, instance_variable_get(:"@#{attribute}"))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def method_missing(mid, *args)
|
64
|
+
len = args.length
|
65
|
+
if mname = mid[/.*(?==\z)/m]
|
66
|
+
if len != 1
|
67
|
+
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
68
|
+
end
|
69
|
+
self[mname] = args.first
|
70
|
+
elsif len == 0
|
71
|
+
self[mid]
|
72
|
+
else
|
73
|
+
raise ArgumentError, "wrong number of arguments (#{len} for 0)", caller(1)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def delete_field(key)
|
78
|
+
self[key] = nil
|
79
|
+
end
|
80
|
+
|
81
|
+
def ==(other)
|
82
|
+
other.is_a?(self.class) && to_h == other.to_h
|
83
|
+
end
|
84
|
+
|
85
|
+
def eql?(other)
|
86
|
+
other.is_a?(self.class) && to_h.eql?(other.to_h)
|
87
|
+
end
|
88
|
+
|
89
|
+
def hash
|
90
|
+
to_h.hash
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
def add_methods!(key)
|
95
|
+
self.class.send(:attr_accessor, key)
|
96
|
+
self.class.attributes << key
|
97
|
+
|
98
|
+
# I'm pretty sure this is safe, because attempting to add an attribute
|
99
|
+
# that isn't a valid instance variable name will raise an error. Please
|
100
|
+
# contact the maintainer if you find a situation where this could be a
|
101
|
+
# security problem.
|
102
|
+
#
|
103
|
+
# The reason to use class_eval here is because, based on benchmarking,
|
104
|
+
# this defines the fastest version of #to_h possible.
|
105
|
+
self.class.class_eval <<-RUBY
|
106
|
+
def to_h
|
107
|
+
{
|
108
|
+
#{
|
109
|
+
self.class.attributes.map { |attribute|
|
110
|
+
"#{attribute.inspect} => #{attribute}"
|
111
|
+
}.join(",\n")
|
112
|
+
}
|
113
|
+
}
|
114
|
+
end
|
115
|
+
RUBY
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- amcaplan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-31 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: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ofstruct
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: persistent_open_struct
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: benchmark-ips
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.3.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.3.0
|
97
|
+
description: Specifically designed as an OpenStruct-like tool for consuming APIs,
|
98
|
+
dynamic_class lets your classes define their own getters and setters at runtime
|
99
|
+
based on the data instances receive at instantiation.
|
100
|
+
email:
|
101
|
+
- ariel.caplan@mail.yu.edu
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- .gitignore
|
107
|
+
- .rspec
|
108
|
+
- .travis.yml
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/benchmark.rb
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- dynamic_class.gemspec
|
118
|
+
- lib/dynamic_class.rb
|
119
|
+
- lib/dynamic_class/version.rb
|
120
|
+
homepage: https://github.com/amcaplan/dynamic_class
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.0.14
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Create classes that define themselves... eventually.
|
144
|
+
test_files: []
|