attrocity 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +30 -10
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile.lock +37 -0
- data/LICENSE +22 -0
- data/README.md +32 -2
- data/Rakefile +14 -0
- data/TODO.md +36 -0
- data/attrocity.gemspec +5 -2
- data/lib/attrocity.rb +91 -2
- data/lib/attrocity/attributes/attribute.rb +10 -0
- data/lib/attrocity/attributes/attribute_methods_builder.rb +47 -0
- data/lib/attrocity/attributes/attribute_set.rb +24 -0
- data/lib/attrocity/attributes/attribute_template.rb +28 -0
- data/lib/attrocity/attributes/attribute_template_set.rb +16 -0
- data/lib/attrocity/attributes/attributes_hash.rb +9 -0
- data/lib/attrocity/attributes/model_attribute.rb +14 -0
- data/lib/attrocity/attributes/model_attribute_set.rb +6 -0
- data/lib/attrocity/builders/model_builder.rb +60 -0
- data/lib/attrocity/builders/object_extension_builder.rb +18 -0
- data/lib/attrocity/coercer_registry.rb +38 -0
- data/lib/attrocity/coercers/boolean.rb +12 -0
- data/lib/attrocity/coercers/integer.rb +13 -0
- data/lib/attrocity/coercers/string.rb +14 -0
- data/lib/attrocity/mappers/key_mapper.rb +14 -0
- data/lib/attrocity/model.rb +10 -0
- data/lib/attrocity/value_extractor.rb +23 -0
- data/lib/attrocity/version.rb +1 -1
- data/notes.md +253 -0
- data/spec/attrocity/attributes/attribute_methods_builder_spec.rb +57 -0
- data/spec/attrocity/attributes/attribute_set_spec.rb +24 -0
- data/spec/attrocity/attributes/attribute_spec.rb +4 -0
- data/spec/attrocity/attributes/attribute_template_set_spec.rb +6 -0
- data/spec/attrocity/attributes/attribute_template_spec.rb +25 -0
- data/spec/attrocity/attributes/model_attribute_spec.rb +26 -0
- data/spec/attrocity/attrocity_spec.rb +83 -0
- data/spec/attrocity/coercer_registry_spec.rb +14 -0
- data/spec/attrocity/coercers/boolean_spec.rb +32 -0
- data/spec/attrocity/coercers/coercer_with_args_spec.rb +9 -0
- data/spec/attrocity/coercers/integer_spec.rb +25 -0
- data/spec/attrocity/coercers/string_spec.rb +13 -0
- data/spec/attrocity/default_value_spec.rb +22 -0
- data/spec/attrocity/mappers/key_mapper_spec.rb +22 -0
- data/spec/attrocity/mapping_spec.rb +47 -0
- data/spec/attrocity/model_spec.rb +34 -0
- data/spec/attrocity/object_extension_spec.rb +53 -0
- data/spec/attrocity/value_extractor_spec.rb +23 -0
- data/spec/spec_helper.rb +93 -0
- data/spec/support/examples.rb +79 -0
- metadata +99 -8
@@ -0,0 +1,53 @@
|
|
1
|
+
module Attrocity
|
2
|
+
RSpec.describe 'Module extension' do
|
3
|
+
before(:context) do
|
4
|
+
module Examples
|
5
|
+
module Spotlight
|
6
|
+
include Attrocity.object_extension
|
7
|
+
attribute :spotlight, coercer: :boolean
|
8
|
+
end
|
9
|
+
|
10
|
+
module MobileSpotlight
|
11
|
+
include Attrocity.object_extension
|
12
|
+
attribute :spotlight, coercer: :boolean, from: :mobilespotlight
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'without attribute mapping' do
|
18
|
+
let(:listing) {
|
19
|
+
Examples::Listing.new({ listingid: '1234', spotlight: 0 })
|
20
|
+
}
|
21
|
+
|
22
|
+
it 'adds module attributes to object' do
|
23
|
+
expect {
|
24
|
+
listing.extend(Examples::Spotlight)
|
25
|
+
}.to change { listing.respond_to?(:spotlight) }.from(false).to(true)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns correct attribute value' do
|
29
|
+
pending
|
30
|
+
listing.extend(Examples::Spotlight)
|
31
|
+
expect(listing.spotlight).to be false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'with attribute mapping' do
|
36
|
+
let(:listing) {
|
37
|
+
Examples::Listing.new({ listingid: '1234', mobilespotlight: 1 })
|
38
|
+
}
|
39
|
+
|
40
|
+
it 'adds module attributes to object' do
|
41
|
+
expect {
|
42
|
+
listing.extend(Examples::MobileSpotlight)
|
43
|
+
}.to change { listing.respond_to?(:spotlight) }.from(false).to(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns correct attribute value' do
|
47
|
+
listing.extend(Examples::MobileSpotlight)
|
48
|
+
expect(listing.spotlight).to be true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Attrocity
|
2
|
+
RSpec.describe ValueExtractor do
|
3
|
+
|
4
|
+
let(:data) { { foo: 'bar' } }
|
5
|
+
let(:mapper) { KeyMapper.new(:foo) }
|
6
|
+
let(:coercer) { Coercers::String.new }
|
7
|
+
subject(:extractor) {
|
8
|
+
ValueExtractor.new(data, mapper: mapper, coercer: coercer)
|
9
|
+
}
|
10
|
+
|
11
|
+
describe '#value' do
|
12
|
+
it 'extracts value from hash' do
|
13
|
+
expect(extractor.value).to eq('bar')
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'coerces value' do
|
17
|
+
allow(mapper).to receive(:call).and_return(1)
|
18
|
+
expect(extractor.value).to eq('1')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
|
18
|
+
require 'attrocity'
|
19
|
+
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
# rspec-expectations config goes here. You can use an alternate
|
23
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
24
|
+
# assertions if you prefer.
|
25
|
+
config.expect_with :rspec do |expectations|
|
26
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
27
|
+
# and `failure_message` of custom matchers include text for helper methods
|
28
|
+
# defined using `chain`, e.g.:
|
29
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
30
|
+
# # => "be bigger than 2 and smaller than 4"
|
31
|
+
# ...rather than:
|
32
|
+
# # => "be bigger than 2"
|
33
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
34
|
+
end
|
35
|
+
|
36
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
37
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
38
|
+
config.mock_with :rspec do |mocks|
|
39
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
40
|
+
# a real object. This is generally recommended, and will default to
|
41
|
+
# `true` in RSpec 4.
|
42
|
+
mocks.verify_partial_doubles = true
|
43
|
+
end
|
44
|
+
|
45
|
+
# The settings below are suggested to provide a good initial experience
|
46
|
+
# with RSpec, but feel free to customize to your heart's content.
|
47
|
+
=begin
|
48
|
+
# These two settings work together to allow you to limit a spec run
|
49
|
+
# to individual examples or groups you care about by tagging them with
|
50
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
51
|
+
# get run.
|
52
|
+
config.filter_run :focus
|
53
|
+
config.run_all_when_everything_filtered = true
|
54
|
+
|
55
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
56
|
+
# For more details, see:
|
57
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
58
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
59
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
60
|
+
config.disable_monkey_patching!
|
61
|
+
|
62
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
63
|
+
# be too noisy due to issues in dependencies.
|
64
|
+
config.warnings = true
|
65
|
+
|
66
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
67
|
+
# file, and it's useful to allow more verbose output when running an
|
68
|
+
# individual spec file.
|
69
|
+
if config.files_to_run.one?
|
70
|
+
# Use the documentation formatter for detailed output,
|
71
|
+
# unless a formatter has already been configured
|
72
|
+
# (e.g. via a command-line flag).
|
73
|
+
config.default_formatter = 'doc'
|
74
|
+
end
|
75
|
+
|
76
|
+
# Print the 10 slowest examples and example groups at the
|
77
|
+
# end of the spec run, to help surface which specs are running
|
78
|
+
# particularly slow.
|
79
|
+
config.profile_examples = 10
|
80
|
+
|
81
|
+
# Run specs in random order to surface order dependencies. If you find an
|
82
|
+
# order dependency and want to debug it, you can fix the order by providing
|
83
|
+
# the seed, which is printed after each run.
|
84
|
+
# --seed 1234
|
85
|
+
config.order = :random
|
86
|
+
|
87
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
88
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
89
|
+
# test failures related to randomization by passing the same `--seed` value
|
90
|
+
# as the one that triggered the failure.
|
91
|
+
Kernel.srand config.seed
|
92
|
+
=end
|
93
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'attrocity'
|
2
|
+
|
3
|
+
module Attrocity
|
4
|
+
module Examples
|
5
|
+
|
6
|
+
class Preferences
|
7
|
+
include Attrocity.model
|
8
|
+
attribute :publish_email, coercer: :boolean, default: false
|
9
|
+
end
|
10
|
+
|
11
|
+
class Person
|
12
|
+
include Attrocity.model
|
13
|
+
attribute :age, coercer: :integer
|
14
|
+
model_attribute :preferences, model: Examples::Preferences
|
15
|
+
end
|
16
|
+
|
17
|
+
class Address
|
18
|
+
include Attrocity.model
|
19
|
+
attribute :street, coercer: :string, from: :addressline1
|
20
|
+
attribute :zip, coercer: :string
|
21
|
+
end
|
22
|
+
|
23
|
+
class Listing
|
24
|
+
include Attrocity.model
|
25
|
+
attribute :id, coercer: :string, from: :listingid
|
26
|
+
model_attribute :address, model: Examples::Address
|
27
|
+
end
|
28
|
+
|
29
|
+
class ExampleCoercer
|
30
|
+
def coerce(value)
|
31
|
+
String(value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class NullValuesCoercer
|
36
|
+
attr_reader :null_values
|
37
|
+
|
38
|
+
def initialize(params={})
|
39
|
+
@null_values = params.fetch(:null_values, [])
|
40
|
+
end
|
41
|
+
|
42
|
+
def coerce(value)
|
43
|
+
if null_values.include?(value)
|
44
|
+
nil
|
45
|
+
else
|
46
|
+
String(value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
CoercerRegistry.register do
|
52
|
+
add :null_values, NullValuesCoercer
|
53
|
+
end
|
54
|
+
|
55
|
+
class DepositRange
|
56
|
+
include Attrocity.model
|
57
|
+
|
58
|
+
attribute :low, coercer: { name: :null_values, null_values: ['0'] },
|
59
|
+
from: :depositlow
|
60
|
+
attribute :high, coercer: { name: :null_values, null_values: ['999999'] },
|
61
|
+
from: :deposithigh
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.integer_attribute(name=:an_integer)
|
65
|
+
Attrocity::Attribute.new(name,
|
66
|
+
Attrocity::Coercers::Integer.new, default_mapper(name))
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.string_attribute(name=:a_string)
|
70
|
+
Attrocity::Attribute.new(name,
|
71
|
+
Attrocity::Coercers::String.new, default_mapper(name))
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.default_mapper(key)
|
75
|
+
Attrocity.default_mapper(key)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
metadata
CHANGED
@@ -1,43 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attrocity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Stankus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
33
|
+
version: '1.11'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
40
|
+
version: '1.11'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '10.
|
47
|
+
version: '10.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.4'
|
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.4'
|
34
62
|
type: :development
|
35
63
|
prerelease: false
|
36
64
|
version_requirements: !ruby/object:Gem::Requirement
|
37
65
|
requirements:
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
68
|
+
version: '3.4'
|
41
69
|
description: You should totally be using Virtus instead of this
|
42
70
|
email:
|
43
71
|
- tjstankus@gmail.com
|
@@ -46,13 +74,56 @@ extensions: []
|
|
46
74
|
extra_rdoc_files: []
|
47
75
|
files:
|
48
76
|
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
49
79
|
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE
|
50
82
|
- LICENSE.txt
|
51
83
|
- README.md
|
52
84
|
- Rakefile
|
85
|
+
- TODO.md
|
53
86
|
- attrocity.gemspec
|
54
87
|
- lib/attrocity.rb
|
88
|
+
- lib/attrocity/attributes/attribute.rb
|
89
|
+
- lib/attrocity/attributes/attribute_methods_builder.rb
|
90
|
+
- lib/attrocity/attributes/attribute_set.rb
|
91
|
+
- lib/attrocity/attributes/attribute_template.rb
|
92
|
+
- lib/attrocity/attributes/attribute_template_set.rb
|
93
|
+
- lib/attrocity/attributes/attributes_hash.rb
|
94
|
+
- lib/attrocity/attributes/model_attribute.rb
|
95
|
+
- lib/attrocity/attributes/model_attribute_set.rb
|
96
|
+
- lib/attrocity/builders/model_builder.rb
|
97
|
+
- lib/attrocity/builders/object_extension_builder.rb
|
98
|
+
- lib/attrocity/coercer_registry.rb
|
99
|
+
- lib/attrocity/coercers/boolean.rb
|
100
|
+
- lib/attrocity/coercers/integer.rb
|
101
|
+
- lib/attrocity/coercers/string.rb
|
102
|
+
- lib/attrocity/mappers/key_mapper.rb
|
103
|
+
- lib/attrocity/model.rb
|
104
|
+
- lib/attrocity/value_extractor.rb
|
55
105
|
- lib/attrocity/version.rb
|
106
|
+
- notes.md
|
107
|
+
- spec/attrocity/attributes/attribute_methods_builder_spec.rb
|
108
|
+
- spec/attrocity/attributes/attribute_set_spec.rb
|
109
|
+
- spec/attrocity/attributes/attribute_spec.rb
|
110
|
+
- spec/attrocity/attributes/attribute_template_set_spec.rb
|
111
|
+
- spec/attrocity/attributes/attribute_template_spec.rb
|
112
|
+
- spec/attrocity/attributes/model_attribute_spec.rb
|
113
|
+
- spec/attrocity/attrocity_spec.rb
|
114
|
+
- spec/attrocity/coercer_registry_spec.rb
|
115
|
+
- spec/attrocity/coercers/boolean_spec.rb
|
116
|
+
- spec/attrocity/coercers/coercer_with_args_spec.rb
|
117
|
+
- spec/attrocity/coercers/integer_spec.rb
|
118
|
+
- spec/attrocity/coercers/string_spec.rb
|
119
|
+
- spec/attrocity/default_value_spec.rb
|
120
|
+
- spec/attrocity/mappers/key_mapper_spec.rb
|
121
|
+
- spec/attrocity/mapping_spec.rb
|
122
|
+
- spec/attrocity/model_spec.rb
|
123
|
+
- spec/attrocity/object_extension_spec.rb
|
124
|
+
- spec/attrocity/value_extractor_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/support/examples.rb
|
56
127
|
homepage: https://github.com/tjstankus/attrocity
|
57
128
|
licenses:
|
58
129
|
- MIT
|
@@ -73,8 +144,28 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
144
|
version: '0'
|
74
145
|
requirements: []
|
75
146
|
rubyforge_project:
|
76
|
-
rubygems_version: 2.
|
147
|
+
rubygems_version: 2.4.5.1
|
77
148
|
signing_key:
|
78
149
|
specification_version: 4
|
79
150
|
summary: Declarative attributes in Ruby
|
80
|
-
test_files:
|
151
|
+
test_files:
|
152
|
+
- spec/attrocity/attributes/attribute_methods_builder_spec.rb
|
153
|
+
- spec/attrocity/attributes/attribute_set_spec.rb
|
154
|
+
- spec/attrocity/attributes/attribute_spec.rb
|
155
|
+
- spec/attrocity/attributes/attribute_template_set_spec.rb
|
156
|
+
- spec/attrocity/attributes/attribute_template_spec.rb
|
157
|
+
- spec/attrocity/attributes/model_attribute_spec.rb
|
158
|
+
- spec/attrocity/attrocity_spec.rb
|
159
|
+
- spec/attrocity/coercer_registry_spec.rb
|
160
|
+
- spec/attrocity/coercers/boolean_spec.rb
|
161
|
+
- spec/attrocity/coercers/coercer_with_args_spec.rb
|
162
|
+
- spec/attrocity/coercers/integer_spec.rb
|
163
|
+
- spec/attrocity/coercers/string_spec.rb
|
164
|
+
- spec/attrocity/default_value_spec.rb
|
165
|
+
- spec/attrocity/mappers/key_mapper_spec.rb
|
166
|
+
- spec/attrocity/mapping_spec.rb
|
167
|
+
- spec/attrocity/model_spec.rb
|
168
|
+
- spec/attrocity/object_extension_spec.rb
|
169
|
+
- spec/attrocity/value_extractor_spec.rb
|
170
|
+
- spec/spec_helper.rb
|
171
|
+
- spec/support/examples.rb
|