dynamic_class 0.2.2 → 0.2.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 +4 -4
- data/README.md +17 -1
- data/dynamic_class.gemspec +1 -1
- data/lib/dynamic_class.rb +3 -2
- data/lib/dynamic_class/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 09ca157ddd7ac6d979bd7ac95e233a81fb27153a
|
4
|
+
data.tar.gz: 19c7d1ccb052138fd4c75dbbdc107b3c836d7219
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9acc7471fb928123cddffc577264dbc9820ddebfb4b42d684d83f26f43016aafdce0c6dea392d245c893d3c53c8f51ffb12023d0e8eb025e5f4c2a6a45372df
|
7
|
+
data.tar.gz: 480de50f8d48f532b60962db09064598bf1644651588b41a9039af0ece45b292a32088f3b5a999d3fa84e2afcac79d32cb702d3c11b22dfee116a4c09e631b3e
|
data/README.md
CHANGED
@@ -25,11 +25,20 @@ dog = Animal.new(type: 'dog', sound: 'woof')
|
|
25
25
|
# => #<Animal:0x007fdb2b818ba8 @type="dog", @sound="woof">
|
26
26
|
dog.speak
|
27
27
|
# => The dog makes a woof sound!
|
28
|
+
dog.ears = 'droopy'
|
29
|
+
dog[:nose] = ['cold', 'wet']
|
30
|
+
dog['tail'] = 'waggable'
|
31
|
+
dog
|
32
|
+
# => #<Animal:0x007fc26b1841d0 @type="dog", @sound="woof", @ears="droopy", @nose=["cold", "wet"], @tail="waggable">
|
33
|
+
dog.type
|
34
|
+
# => "dog"
|
35
|
+
dog.tail
|
36
|
+
# => "waggable"
|
28
37
|
|
29
38
|
cat = Animal.new
|
30
39
|
# => #<Animal:0x007fdb2b83b180>
|
31
40
|
cat.to_h
|
32
|
-
# => {:type=>nil, :sound=>nil}
|
41
|
+
# => {:type=>nil, :sound=>nil, :ears=>nil, :nose=>nil, :tail=>nil}
|
33
42
|
# The class has been changed by the dog!
|
34
43
|
```
|
35
44
|
|
@@ -141,6 +150,13 @@ PersistentOpenStruct: 625010.1 i/s - 4.96x slower
|
|
141
150
|
`DynamicClass` is still behind plain old Ruby classes, but it's the best out of
|
142
151
|
the pack when it comes to `OpenStruct` and friends.
|
143
152
|
|
153
|
+
## WARNING!
|
154
|
+
|
155
|
+
This class should only be used to consume trusted APIs, or for similar purposes.
|
156
|
+
It should never be used to take in user input. This will open you up to a memory
|
157
|
+
leak DoS attack, since every new key becomes a new method defined on the class,
|
158
|
+
and is never erased.
|
159
|
+
|
144
160
|
## Installation
|
145
161
|
|
146
162
|
Add this line to your application's Gemfile:
|
data/dynamic_class.gemspec
CHANGED
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "rspec", "~> 3.0"
|
26
26
|
spec.add_development_dependency "ofstruct", "~> 0.2"
|
27
27
|
spec.add_development_dependency "persistent_open_struct", "~> 0.0"
|
28
|
-
spec.add_development_dependency "benchmark-ips", "~> 2.3
|
28
|
+
spec.add_development_dependency "benchmark-ips", "~> 2.3"
|
29
29
|
end
|
data/lib/dynamic_class.rb
CHANGED
@@ -31,7 +31,8 @@ module DynamicClass
|
|
31
31
|
def add_methods!(key)
|
32
32
|
class_exec do
|
33
33
|
mutex.synchronize do
|
34
|
-
|
34
|
+
attr_writer key unless method_defined?("#{key}=")
|
35
|
+
attr_reader key unless method_defined?("#{key}")
|
35
36
|
attributes << key
|
36
37
|
|
37
38
|
# I'm pretty sure this is safe, because attempting to add an attribute
|
@@ -86,7 +87,7 @@ module DynamicClass
|
|
86
87
|
|
87
88
|
def method_missing(mid, *args)
|
88
89
|
len = args.length
|
89
|
-
if mname = mid[/.*(?==\z)/m]
|
90
|
+
if (mname = mid[/.*(?==\z)/m])
|
90
91
|
if len != 1
|
91
92
|
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
92
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_class
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- amcaplan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 2.3
|
89
|
+
version: '2.3'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 2.3
|
96
|
+
version: '2.3'
|
97
97
|
description: Specifically designed as an OpenStruct-like tool for consuming APIs,
|
98
98
|
dynamic_class lets your classes define their own getters and setters at runtime
|
99
99
|
based on the data instances receive at instantiation.
|
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
137
|
version: '0'
|
138
138
|
requirements: []
|
139
139
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.
|
140
|
+
rubygems_version: 2.2.2
|
141
141
|
signing_key:
|
142
142
|
specification_version: 4
|
143
143
|
summary: Create classes that define themselves... eventually.
|