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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c08822eea8d17e5cfefb8132a98bf2bb19d44a9b
4
- data.tar.gz: 68a4b1e5c93fd38c30fae67001ac8bde1fcd9e5f
3
+ metadata.gz: 09ca157ddd7ac6d979bd7ac95e233a81fb27153a
4
+ data.tar.gz: 19c7d1ccb052138fd4c75dbbdc107b3c836d7219
5
5
  SHA512:
6
- metadata.gz: 13265b5a7e56561dae170a0caa2384f010f15aab69bd23b1bf56c9f2c54ffdf8ab4340eeb4b31598f9b0b7a25ee89a5919721a4a52e22d1b732f96cbff22020e
7
- data.tar.gz: 34c73c85e38d3130c959ea52eac46cf3c2415fbbd937c21c90e24662b559b659377ad6d8d9a6ab184270e55ff3717d5776727a59fe60fec3d1688ed8c7ca09df
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:
@@ -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.0"
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
- attr_accessor key
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
@@ -1,3 +1,3 @@
1
1
  module DynamicClass
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  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.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-01 00:00:00.000000000 Z
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.0
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.0
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.4.3
140
+ rubygems_version: 2.2.2
141
141
  signing_key:
142
142
  specification_version: 4
143
143
  summary: Create classes that define themselves... eventually.