attribute_helpers 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.overcommit.yml +7 -0
- data/.rubocop.yml +264 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +1 -0
- data/attribute_helpers.gemspec +28 -0
- data/lib/attribute_helpers.rb +65 -0
- data/lib/attribute_helpers/version.rb +3 -0
- data/spec/active_record_class_spec.rb +98 -0
- data/spec/ruby_class_spec.rb +96 -0
- data/spec/spec_helper.rb +48 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6bdcbca82ffba90dff1676ddb79274e4f8bc4db2
|
4
|
+
data.tar.gz: 89a16fdb61922b6927ee0cd636165658cf94255f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6c61be78490f3542860fcbabc30a129978d6f814fa68752939bc94a47567b370c75e6aeb876873adc6710d480c31cbca0a5cd4072c17276fe46d3d1c2835aed4
|
7
|
+
data.tar.gz: 66575f858dbfce49d187893131824aa4c8fa5cc7aca912334c3ca8db234804418fab26348d6dff4d2bb77bd8e600044589e9f33b4dde65ddffd0612211599100
|
data/.gitignore
ADDED
data/.overcommit.yml
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,264 @@
|
|
1
|
+
AccessorMethodName:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Alias:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
Exclude:
|
9
|
+
- "vendor/**/*"
|
10
|
+
- "spec/dummy/**/*"
|
11
|
+
- "db/schema.rb"
|
12
|
+
- "db/migrate/**/*"
|
13
|
+
RunRailsCops: true
|
14
|
+
|
15
|
+
AmbiguousOperator:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
AmbiguousRegexpLiteral:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
ArrayJoin:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
AsciiComments:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
AsciiIdentifiers:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
AssignmentInCondition:
|
31
|
+
Enabled: true
|
32
|
+
|
33
|
+
Attr:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
BlockNesting:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
BracesAroundHashParameters:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
CaseEquality:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
CharacterLiteral:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
ClassLength:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
ClassVars:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
CollectionMethods:
|
55
|
+
PreferredMethods:
|
56
|
+
find: detect
|
57
|
+
reduce: inject
|
58
|
+
collect: map
|
59
|
+
find_all: select
|
60
|
+
|
61
|
+
ColonMethodCall:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
CommentAnnotation:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
ConditionPosition:
|
68
|
+
Enabled: false
|
69
|
+
|
70
|
+
CyclomaticComplexity:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
Delegate:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
DeprecatedClassMethods:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
DeprecatedHashMethods:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
Documentation:
|
83
|
+
Enabled: false
|
84
|
+
|
85
|
+
DotPosition:
|
86
|
+
EnforcedStyle: trailing
|
87
|
+
|
88
|
+
DoubleNegation:
|
89
|
+
Enabled: false
|
90
|
+
|
91
|
+
ElseLayout:
|
92
|
+
Enabled: false
|
93
|
+
|
94
|
+
EmptyLiteral:
|
95
|
+
Enabled: false
|
96
|
+
|
97
|
+
Encoding:
|
98
|
+
Enabled: false
|
99
|
+
|
100
|
+
EvenOdd:
|
101
|
+
Enabled: false
|
102
|
+
|
103
|
+
FileName:
|
104
|
+
Enabled: false
|
105
|
+
|
106
|
+
FlipFlop:
|
107
|
+
Enabled: false
|
108
|
+
|
109
|
+
FormatString:
|
110
|
+
Enabled: false
|
111
|
+
|
112
|
+
GlobalVars:
|
113
|
+
Enabled: false
|
114
|
+
|
115
|
+
HandleExceptions:
|
116
|
+
Enabled: false
|
117
|
+
|
118
|
+
IfUnlessModifier:
|
119
|
+
Enabled: false
|
120
|
+
|
121
|
+
IfWithSemicolon:
|
122
|
+
Enabled: false
|
123
|
+
|
124
|
+
InvalidCharacterLiteral:
|
125
|
+
Enabled: false
|
126
|
+
|
127
|
+
Lambda:
|
128
|
+
Enabled: false
|
129
|
+
|
130
|
+
LambdaCall:
|
131
|
+
Enabled: false
|
132
|
+
|
133
|
+
LineEndConcatenation:
|
134
|
+
Enabled: false
|
135
|
+
|
136
|
+
LineLength:
|
137
|
+
Max: 80
|
138
|
+
|
139
|
+
LiteralInCondition:
|
140
|
+
Enabled: false
|
141
|
+
|
142
|
+
LiteralInInterpolation:
|
143
|
+
Enabled: false
|
144
|
+
|
145
|
+
Loop:
|
146
|
+
Enabled: false
|
147
|
+
|
148
|
+
MethodLength:
|
149
|
+
Enabled: false
|
150
|
+
|
151
|
+
ModuleFunction:
|
152
|
+
Enabled: false
|
153
|
+
|
154
|
+
NegatedIf:
|
155
|
+
Enabled: false
|
156
|
+
|
157
|
+
NegatedWhile:
|
158
|
+
Enabled: false
|
159
|
+
|
160
|
+
Next:
|
161
|
+
Enabled: false
|
162
|
+
|
163
|
+
NilComparison:
|
164
|
+
Enabled: false
|
165
|
+
|
166
|
+
Not:
|
167
|
+
Enabled: false
|
168
|
+
|
169
|
+
NumericLiterals:
|
170
|
+
Enabled: false
|
171
|
+
|
172
|
+
OneLineConditional:
|
173
|
+
Enabled: false
|
174
|
+
|
175
|
+
OpMethod:
|
176
|
+
Enabled: false
|
177
|
+
|
178
|
+
ParameterLists:
|
179
|
+
Enabled: false
|
180
|
+
|
181
|
+
ParenthesesAsGroupedExpression:
|
182
|
+
Enabled: false
|
183
|
+
|
184
|
+
PercentLiteralDelimiters:
|
185
|
+
PreferredDelimiters:
|
186
|
+
'%': '{}'
|
187
|
+
|
188
|
+
PerlBackrefs:
|
189
|
+
Enabled: false
|
190
|
+
|
191
|
+
PredicateName:
|
192
|
+
NamePrefixBlacklist:
|
193
|
+
- is_
|
194
|
+
|
195
|
+
Proc:
|
196
|
+
Enabled: false
|
197
|
+
|
198
|
+
RaiseArgs:
|
199
|
+
Enabled: false
|
200
|
+
|
201
|
+
RedundantReturn:
|
202
|
+
AllowMultipleReturnValues: true
|
203
|
+
|
204
|
+
RegexpLiteral:
|
205
|
+
Enabled: false
|
206
|
+
|
207
|
+
RequireParentheses:
|
208
|
+
Enabled: false
|
209
|
+
|
210
|
+
SelfAssignment:
|
211
|
+
Enabled: false
|
212
|
+
|
213
|
+
SignalException:
|
214
|
+
EnforcedStyle: only_raise
|
215
|
+
|
216
|
+
SingleLineBlockParams:
|
217
|
+
Enabled: false
|
218
|
+
|
219
|
+
SingleLineMethods:
|
220
|
+
Enabled: false
|
221
|
+
|
222
|
+
SpecialGlobalVars:
|
223
|
+
Enabled: false
|
224
|
+
|
225
|
+
StringLiterals:
|
226
|
+
EnforcedStyle: double_quotes
|
227
|
+
|
228
|
+
Style/MultilineBlockChain:
|
229
|
+
Enabled: false
|
230
|
+
|
231
|
+
VariableInterpolation:
|
232
|
+
Enabled: false
|
233
|
+
|
234
|
+
TrailingComma:
|
235
|
+
Enabled: false
|
236
|
+
|
237
|
+
TrivialAccessors:
|
238
|
+
Enabled: false
|
239
|
+
|
240
|
+
UnderscorePrefixedVariableName:
|
241
|
+
Enabled: false
|
242
|
+
|
243
|
+
VariableInterpolation:
|
244
|
+
Enabled: false
|
245
|
+
|
246
|
+
Void:
|
247
|
+
Enabled: false
|
248
|
+
|
249
|
+
WhenThen:
|
250
|
+
Enabled: false
|
251
|
+
|
252
|
+
WhileUntilModifier:
|
253
|
+
Enabled: false
|
254
|
+
|
255
|
+
WordArray:
|
256
|
+
Enabled: false
|
257
|
+
|
258
|
+
GuardClause:
|
259
|
+
Enabled: false
|
260
|
+
|
261
|
+
# Renaming `has_something?` to `something?` obfuscates whether it is a "is-a" or
|
262
|
+
# # a "has-a" relationship.
|
263
|
+
PredicateName:
|
264
|
+
Enabled: false
|
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0.0
|
4
|
+
- 2.1.4
|
5
|
+
script: bundle exec rspec
|
6
|
+
addons:
|
7
|
+
code_climate:
|
8
|
+
repo_token: 349fd22a1560a80e4621bc668f8d1ecf5f302ae19ed146c2dbc6b264525ab9c0
|
9
|
+
notifications:
|
10
|
+
email: false
|
11
|
+
hipchat:
|
12
|
+
rooms:
|
13
|
+
secure: LQf6eRVE1yhDHftcmGuKj27vhyg/NQjTWLcEiv76hs0kZ7jnPKbb7TPoyqg08r3/vGyxU1NMhSaeQtuH8cXznXYXaF7WFMWUeulu/OtsVsAo4+s+GhM2n06rXemY7gxhAQ6lxg2I7nodcWrW6hMg+jnvTRPgUmwmv/cHZxg6qTY=
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Panorama Education
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/panorama-ed/attribute_helpers/badges/gpa.svg)](https://codeclimate.com/github/panorama-ed/attribute_helpers) [![Test Coverage](https://codeclimate.com/github/panorama-ed/attribute_helpers/badges/coverage.svg)](https://codeclimate.com/github/panorama-ed/attribute_helpers) [![Build Status](https://travis-ci.org/panorama-ed/attribute_helpers.svg)](https://travis-ci.org/panorama-ed/attribute_helpers) [![Inline docs](http://inch-ci.org/github/panorama-ed/attribute_helpers.png)](http://inch-ci.org/github/panorama-ed/attribute_helpers) [![Gem Version](https://badge.fury.io/rb/attribute_helpers.svg)](http://badge.fury.io/rb/attribute_helpers)
|
2
|
+
|
3
|
+
# AttributeHelpers
|
4
|
+
|
5
|
+
Provides helper functionality for ruby classes that store various
|
6
|
+
database-unfriendly types as instance variables. It automatically serializes and
|
7
|
+
deserializes things like classes and symbols to interact easily with both the
|
8
|
+
database and your application code.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "attribute_helpers"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install attribute_helpers
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
This gem exposes the `attr_symbol` and `attr_class` class methods, which when
|
29
|
+
called will wrap the related instance method to allow for better database
|
30
|
+
serialization.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require "attribute_helpers"
|
34
|
+
|
35
|
+
class Vehicle < ActiveRecord::Base
|
36
|
+
prepend AttributeHelpers
|
37
|
+
|
38
|
+
attr_class :manufacturer
|
39
|
+
attr_symbol :status
|
40
|
+
end
|
41
|
+
|
42
|
+
car = Vehicle.new
|
43
|
+
car.manufacturer = Tesla # This is a class.
|
44
|
+
car.status = :parked
|
45
|
+
car.save!
|
46
|
+
car = car.reload # After a DB round-trip, typically these fields are strings.
|
47
|
+
car.manufacturer # Tesla (the class) rather than "Tesla" (the string)
|
48
|
+
car.status # :parked (the symbol) rather than "parked" (the string)
|
49
|
+
|
50
|
+
```
|
51
|
+
|
52
|
+
Note: while this gem was written to help with ActiveRecord
|
53
|
+
objects, it has **no dependencies** and works great with any database
|
54
|
+
backend (or none!). **You can prepend it into pure Ruby classes just
|
55
|
+
fine!**
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it (https://github.com/panorama-ed/attribute_helpers/fork)
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create a new Pull Request
|
64
|
+
|
65
|
+
**Make sure your changes have appropriate tests (`bundle exec rspec`)
|
66
|
+
and conform to the Rubocop style specified.** We use
|
67
|
+
[overcommit](https://github.com/causes/overcommit) to enforce good code.
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
AttributeHelpers is released under the
|
72
|
+
[MIT License](https://github.com/panorama-ed/attribute_helpers/blob/master/LICENSE.txt).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "attribute_helpers/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "attribute_helpers"
|
8
|
+
spec.version = AttributeHelpers::VERSION
|
9
|
+
spec.authors = ["Jacob Evelyn"]
|
10
|
+
spec.email = ["jevelyn@panoramaed.com"]
|
11
|
+
spec.summary = "Provides auto-serialization of simple Ruby types that "\
|
12
|
+
"databases do not support."
|
13
|
+
spec.description = "Provides auto-serialization of simple Ruby types that "\
|
14
|
+
"databases do not support, such as symbols and classes."
|
15
|
+
spec.homepage = "https://github.com/panorama-ed/attribute_helpers"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
27
|
+
spec.add_development_dependency "temping", "~> 3.2"
|
28
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "attribute_helpers/version"
|
2
|
+
|
3
|
+
# Provides helper functionality for ruby classes that store various
|
4
|
+
# database-unfriendly types as instance variables. It automatically serializes
|
5
|
+
# and deserializes things like classes and symbols to interact easily with both
|
6
|
+
# the database and your application code.
|
7
|
+
|
8
|
+
module AttributeHelpers
|
9
|
+
# This module needs to be prepended to work in ActiveRecord classes. This is
|
10
|
+
# because ActiveRecord doesn't have accessors/mutators defined until an
|
11
|
+
# instance is created, which means we need to use the prepend + super()
|
12
|
+
# pattern because attempts to use instance_method + binding will fail since
|
13
|
+
# instance_method will not find the method in the class context as it will not
|
14
|
+
# exist until it is dynamically created when an instance is created. Prepend
|
15
|
+
# works for us because it inserts the behavior *below* the class in the
|
16
|
+
# inheritance hierarchy, so we can access the default ActiveRecord accessors/
|
17
|
+
# mutators through the use of super().
|
18
|
+
# More information here: http://stackoverflow.com/a/4471202/1103543
|
19
|
+
def self.prepended(klass)
|
20
|
+
# We need to store the module in a variable for use when we're in the class
|
21
|
+
# context.
|
22
|
+
me = self
|
23
|
+
|
24
|
+
# Marks attributes as storing symbol values, providing setters and getters
|
25
|
+
# for the attributes that will allow the application to use them as symbols
|
26
|
+
# but store them internally as strings.
|
27
|
+
# @param attrs [*Symbol] a list of the attributes that store symbols
|
28
|
+
klass.define_singleton_method :attr_symbol do |*attrs|
|
29
|
+
# Overwrite each attribute's methods.
|
30
|
+
attrs.each do |attr|
|
31
|
+
# Overwrite the accessor.
|
32
|
+
me.send(:define_method, attr) do
|
33
|
+
val = super()
|
34
|
+
val && val.to_sym
|
35
|
+
end
|
36
|
+
|
37
|
+
# Overwrite the mutator.
|
38
|
+
me.send(:define_method, "#{attr}=") do |val|
|
39
|
+
super(val && val.to_s)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Marks attributes as storing class values, providing setters and getters
|
45
|
+
# for the attributes that will allow the application to use them as classes
|
46
|
+
# but store them internally as strings.
|
47
|
+
# @param attrs [*Symbol] a list of the attributes that store classes
|
48
|
+
klass.define_singleton_method :attr_class do |*attrs|
|
49
|
+
# Overwrite each attribute's methods.
|
50
|
+
attrs.each do |attr|
|
51
|
+
# Overwrite the accessor.
|
52
|
+
# @raise [NameError] if the string can't be constantized
|
53
|
+
me.send(:define_method, attr) do
|
54
|
+
val = super()
|
55
|
+
val && Kernel.const_get(val)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Overwrite the mutator.
|
59
|
+
me.send(:define_method, "#{attr}=") do |val|
|
60
|
+
super(val && val.to_s)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.context "in an ActiveRecord class" do
|
4
|
+
Temping.create :active_record_test_class do
|
5
|
+
with_columns do |t|
|
6
|
+
t.string :symbol_attr
|
7
|
+
t.string :class_attr
|
8
|
+
end
|
9
|
+
|
10
|
+
prepend AttributeHelpers
|
11
|
+
|
12
|
+
attr_symbol :symbol_attr
|
13
|
+
attr_class :class_attr
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:test_obj) { ActiveRecordTestClass.new }
|
17
|
+
|
18
|
+
describe ".attr_symbol" do
|
19
|
+
describe "getter" do
|
20
|
+
it "translates string to symbol" do
|
21
|
+
# Give it an intial value to be read.
|
22
|
+
test_obj[:symbol_attr] = "example"
|
23
|
+
|
24
|
+
expect(test_obj.symbol_attr).to eq :example
|
25
|
+
end
|
26
|
+
|
27
|
+
it "reads nil correctly" do
|
28
|
+
# Explicitly set the attribute to nil, though it's already nil by
|
29
|
+
# default.
|
30
|
+
test_obj[:symbol_attr] = nil
|
31
|
+
|
32
|
+
expect(test_obj.symbol_attr).to be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "setter" do
|
37
|
+
it "translates symbol to string" do
|
38
|
+
test_obj.symbol_attr = :example
|
39
|
+
expect(test_obj[:symbol_attr]).to eq "example"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "stores nil when nil is passed" do
|
43
|
+
# Give it an intial value, to make sure the nil set isn't just relying
|
44
|
+
# on default object behavior.
|
45
|
+
test_obj.symbol_attr = :example
|
46
|
+
|
47
|
+
test_obj.symbol_attr = nil
|
48
|
+
expect(test_obj[:symbol_attr]).to be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe ".attr_class" do
|
54
|
+
describe "getter" do
|
55
|
+
context "when string represents a real class" do
|
56
|
+
it "translates string to class" do
|
57
|
+
# Give it an intial value to be read.
|
58
|
+
test_obj[:class_attr] = "Object"
|
59
|
+
|
60
|
+
expect(test_obj.class_attr).to eq Object
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when string does not represent a real class" do
|
65
|
+
it "raises an error" do
|
66
|
+
# Give it an intial value to be read.
|
67
|
+
test_obj[:class_attr] = "BogusClass"
|
68
|
+
|
69
|
+
expect { test_obj.class_attr }.to raise_error(NameError)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it "reads nil correctly" do
|
74
|
+
# Explicitly set the instance variable to nil, though it's already nil
|
75
|
+
# by default.
|
76
|
+
test_obj[:class_attr] = nil
|
77
|
+
|
78
|
+
expect(test_obj.class_attr).to be_nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "setter" do
|
83
|
+
it "translates class to string" do
|
84
|
+
test_obj.class_attr = Object
|
85
|
+
expect(test_obj[:class_attr]).to eq "Object"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "stores nil when nil is passed" do
|
89
|
+
# Give it an intial value, to make sure the nil set isn't just relying
|
90
|
+
# on default object behavior.
|
91
|
+
test_obj.class_attr = Object
|
92
|
+
|
93
|
+
test_obj.class_attr = nil
|
94
|
+
expect(test_obj[:class_attr]).to be_nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.context "in a pure Ruby class" do
|
4
|
+
class RubyTestClass
|
5
|
+
prepend AttributeHelpers
|
6
|
+
|
7
|
+
attr_accessor :symbol_attr
|
8
|
+
attr_accessor :class_attr
|
9
|
+
|
10
|
+
attr_symbol :symbol_attr
|
11
|
+
attr_class :class_attr
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:test_obj) { RubyTestClass.new }
|
15
|
+
|
16
|
+
describe ".attr_symbol" do
|
17
|
+
describe "getter" do
|
18
|
+
it "translates string to symbol" do
|
19
|
+
# Give it an intial value to be read.
|
20
|
+
test_obj.instance_variable_set(:@symbol_attr, "example")
|
21
|
+
|
22
|
+
expect(test_obj.symbol_attr).to eq :example
|
23
|
+
end
|
24
|
+
|
25
|
+
it "reads nil correctly" do
|
26
|
+
# Explicitly set the instance variable to nil, though it's already nil
|
27
|
+
# by default.
|
28
|
+
test_obj.instance_variable_set(:@symbol_attr, nil)
|
29
|
+
|
30
|
+
expect(test_obj.symbol_attr).to be_nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "setter" do
|
35
|
+
it "translates symbol to string" do
|
36
|
+
test_obj.symbol_attr = :example
|
37
|
+
expect(test_obj.instance_variable_get(:@symbol_attr)).to eq "example"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "stores nil when nil is passed" do
|
41
|
+
# Give it an intial value, to make sure the nil set isn't just relying
|
42
|
+
# on default object behavior.
|
43
|
+
test_obj.symbol_attr = :example
|
44
|
+
|
45
|
+
test_obj.symbol_attr = nil
|
46
|
+
expect(test_obj.instance_variable_get(:@symbol_attr)).to be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe ".attr_class" do
|
52
|
+
describe "getter" do
|
53
|
+
context "when string represents a real class" do
|
54
|
+
it "translates string to class" do
|
55
|
+
# Give it an intial value to be read.
|
56
|
+
test_obj.instance_variable_set(:@class_attr, "Object")
|
57
|
+
|
58
|
+
expect(test_obj.class_attr).to eq Object
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when string does not represent a real class" do
|
63
|
+
it "raises an error" do
|
64
|
+
# Give it an intial value to be read.
|
65
|
+
test_obj.instance_variable_set(:@class_attr, "BogusClass")
|
66
|
+
|
67
|
+
expect { test_obj.class_attr }.to raise_error(NameError)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "reads nil correctly" do
|
72
|
+
# Explicitly set the instance variable to nil, though it's already nil
|
73
|
+
# by default.
|
74
|
+
test_obj.instance_variable_set(:@class_attr, nil)
|
75
|
+
|
76
|
+
expect(test_obj.class_attr).to be_nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "setter" do
|
81
|
+
it "translates class to string" do
|
82
|
+
test_obj.class_attr = Object
|
83
|
+
expect(test_obj.instance_variable_get(:@class_attr)).to eq "Object"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "stores nil when nil is passed" do
|
87
|
+
# Give it an intial value, to make sure the nil set isn't just relying
|
88
|
+
# on default object behavior.
|
89
|
+
test_obj.class_attr = Object
|
90
|
+
|
91
|
+
test_obj.class_attr = nil
|
92
|
+
expect(test_obj.instance_variable_get(:@class_attr)).to be_nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
# Connect to an in-memory database for ActiveRecord tests.
|
5
|
+
require "temping"
|
6
|
+
ActiveRecord::Base.
|
7
|
+
establish_connection(adapter: "sqlite3", database: ":memory:")
|
8
|
+
|
9
|
+
require "attribute_helpers"
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
# These two settings work together to allow you to limit a spec run
|
13
|
+
# to individual examples or groups you care about by tagging them with
|
14
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
15
|
+
# get run.
|
16
|
+
config.filter_run :focus
|
17
|
+
config.run_all_when_everything_filtered = true
|
18
|
+
|
19
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
20
|
+
# recommended.
|
21
|
+
# For more details, see:
|
22
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
23
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
24
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
25
|
+
config.disable_monkey_patching!
|
26
|
+
|
27
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
28
|
+
# file, and it's useful to allow more verbose output when running an
|
29
|
+
# individual spec file.
|
30
|
+
if config.files_to_run.one?
|
31
|
+
# Use the documentation formatter for detailed output,
|
32
|
+
# unless a formatter has already been configured
|
33
|
+
# (e.g. via a command-line flag).
|
34
|
+
config.default_formatter = "doc"
|
35
|
+
end
|
36
|
+
|
37
|
+
# Run specs in random order to surface order dependencies. If you find an
|
38
|
+
# order dependency and want to debug it, you can fix the order by providing
|
39
|
+
# the seed, which is printed after each run.
|
40
|
+
# --seed 1234
|
41
|
+
config.order = :random
|
42
|
+
|
43
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
44
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
45
|
+
# test failures related to randomization by passing the same `--seed` value
|
46
|
+
# as the one that triggered the failure.
|
47
|
+
Kernel.srand config.seed
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attribute_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Evelyn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-12 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: codeclimate-test-reporter
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: temping
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.2'
|
83
|
+
description: Provides auto-serialization of simple Ruby types that databases do not
|
84
|
+
support, such as symbols and classes.
|
85
|
+
email:
|
86
|
+
- jevelyn@panoramaed.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .overcommit.yml
|
93
|
+
- .rubocop.yml
|
94
|
+
- .travis.yml
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- attribute_helpers.gemspec
|
100
|
+
- lib/attribute_helpers.rb
|
101
|
+
- lib/attribute_helpers/version.rb
|
102
|
+
- spec/active_record_class_spec.rb
|
103
|
+
- spec/ruby_class_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: https://github.com/panorama-ed/attribute_helpers
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Provides auto-serialization of simple Ruby types that databases do not support.
|
129
|
+
test_files:
|
130
|
+
- spec/active_record_class_spec.rb
|
131
|
+
- spec/ruby_class_spec.rb
|
132
|
+
- spec/spec_helper.rb
|