hash_serializer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +12 -0
- data/.github/ISSUE_TEMPLATE +5 -0
- data/.github/PULL_REQUEST_TEMPLATE +9 -0
- data/.gitignore +37 -0
- data/.rubocop.yml +13 -0
- data/.travis.yml +12 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +5 -0
- data/Guardfile +19 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +6 -0
- data/hash_serializer.gemspec +33 -0
- data/lib/hash_serializer.rb +7 -0
- data/lib/hash_serializer/helpers.rb +89 -0
- data/lib/hash_serializer/serializer.rb +36 -0
- data/lib/hash_serializer/version.rb +5 -0
- data/spec/hash_serializer/helpers_spec.rb +98 -0
- data/spec/hash_serializer/serializer_spec.rb +26 -0
- data/spec/hash_serializer/version_spec.rb +7 -0
- data/spec/hash_serializer_spec.rb +15 -0
- data/spec/spec_helper.rb +13 -0
- metadata +214 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cc12773f570edf8d97c2bfdc0d08745fbcc6db16
|
4
|
+
data.tar.gz: 7463e0b25e75a54684937ec11eacc6a634849436
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69aadb9d3ee99467918b27fbb2329e462c651a1e057aa2ccd327665895836ac3304bcc9d590767854e93376deddca80f8182be34e6e0f2acde768664073e8809
|
7
|
+
data.tar.gz: d6c20d1bcb489e345e305f082a8071670569d0ab83f2eed0a42791edb6841d9510eb15ca351d63e0cc6b3f565e62dad23aa4874eb549757766c0cc12f1fd9d65
|
data/.codeclimate.yml
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalization:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
Gemfile.lock
|
32
|
+
.ruby-version
|
33
|
+
.ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
37
|
+
.DS_Store
|
data/.rubocop.yml
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 johncblandii@gmail.com. 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/Guardfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
guard :rubocop, cli: '-R -D -S' do
|
2
|
+
watch(/.+\.rb$/)
|
3
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
4
|
+
end
|
5
|
+
|
6
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
7
|
+
require 'guard/rspec/dsl'
|
8
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
9
|
+
|
10
|
+
# RSpec files
|
11
|
+
rspec = dsl.rspec
|
12
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
13
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
14
|
+
watch(rspec.spec_files)
|
15
|
+
|
16
|
+
# Ruby files
|
17
|
+
ruby = dsl.ruby
|
18
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
19
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 John C. Bland II
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 John C. Bland II
|
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,105 @@
|
|
1
|
+
# HashSerializer
|
2
|
+
|
3
|
+
[![Build Status](https://api.travis-ci.org/johncblandii/hash_serializer.svg?branch=master)](http://travis-ci.org/johncblandii/hash_serializer)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/johncblandii/hash_serializer/badges/gpa.svg)](https://codeclimate.com/github/johncblandii/hash_serializer)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/johncblandii/hash_serializer/badges/coverage.svg)](https://codeclimate.com/github/johncblandii/hash_serializer/coverage)
|
6
|
+
[![Issue Count](https://codeclimate.com/github/johncblandii/hash_serializer/badges/issue_count.svg)](https://codeclimate.com/github/johncblandii/hash_serializer)
|
7
|
+
|
8
|
+
A simple Hash to JSON serializer with some helpers to improve JSON model columns.
|
9
|
+
|
10
|
+
## Back Story
|
11
|
+
|
12
|
+
`HashSerializer` was birthed from the need of converting a `Hash` to and from JSON for ActiveRecord JSON columns, specifically tested on Postgres. The need grew to monitoring `Hash` changes in Rails models and creating dynamic methods for accessing the hash keys as if they were model properties. The need to prefix the columns came apparent when [Stripe](https://stripe.com) data was used in a hash and it contained keys like `id` and `object`.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'hash_serializer'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install hash_serializer
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
In a model with a JSON column (ex - `billing_fields`) and serialize it:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class Customer < ActiveRecord::Base
|
36
|
+
serialize :billing_fields, HashSerializer
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
$ customer = Customer.new(billing_fields: { name: 'John C. Bland II' })
|
41
|
+
$ customer.billing_fields[:name]
|
42
|
+
=> John C. Bland II
|
43
|
+
|
44
|
+
This does leave the column open to any keys so it is great for development while equally terrible for security. You can utilize the `HashSerializer::Helpers` to add key validation:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Customer < ActiveRecord::Base
|
48
|
+
serialize :billing_fields, HashSerializer
|
49
|
+
|
50
|
+
validate :validate_billing_fields
|
51
|
+
|
52
|
+
def validate_billing_fields
|
53
|
+
invalid_keys = validate_hash_serializer :billing_hash, %w(name)
|
54
|
+
errors.add(:billing_fields, 'has invalid keys: #{invalid_fields.join(', ')}') unless invalid_keys.empty?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
$ customer = Customer.new(billing_fields: { name: 'John C. Bland II' })
|
60
|
+
$ customer.billing_fields[:dumb_stuff] = true
|
61
|
+
$ customer.valid?
|
62
|
+
=> false
|
63
|
+
|
64
|
+
Since some JSON keys may be best served with conflicting names to the housed model, you can also generate custom methods for each key for direct access to the hash without using hash syntax. It also allows for determining if a value has changed.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class Customer < ActiveRecord::Base
|
68
|
+
serialize :billing_fields, HashSerializer
|
69
|
+
|
70
|
+
store_accessor_with_prefix :billing_fields, 'billing', %w(name)
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
$ customer = Customer.new(billing_fields: { name: 'John C. Bland II' })
|
75
|
+
$ customer.billing_name
|
76
|
+
=> 'John C. Bland II'
|
77
|
+
|
78
|
+
$ customer.billing_name = 'John Bland III'
|
79
|
+
$ customer.billing_name
|
80
|
+
=> 'John Bland III'
|
81
|
+
|
82
|
+
$ customer.billing_fields[:name]
|
83
|
+
=> John Bland III
|
84
|
+
|
85
|
+
$ customer.billing_name_changed?
|
86
|
+
=> true
|
87
|
+
|
88
|
+
$ customer.billing_fields_changed?
|
89
|
+
=> true
|
90
|
+
|
91
|
+
**NOTE:** To hide the methods, you can include them in a Concern and call the included methods within the `included` block.
|
92
|
+
|
93
|
+
## Development
|
94
|
+
|
95
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
96
|
+
|
97
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
98
|
+
|
99
|
+
## Contributing
|
100
|
+
|
101
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/johncblandii/hash_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
102
|
+
|
103
|
+
## License
|
104
|
+
|
105
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hash_serializer"
|
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,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_serializer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hash_serializer'
|
8
|
+
spec.version = HashSerializer::VERSION
|
9
|
+
spec.authors = ['John C. Bland II']
|
10
|
+
spec.email = ['johncblandii@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'A serializer and helpers for Postgres JSON columns.'
|
13
|
+
spec.description = 'Stuff happens'
|
14
|
+
spec.homepage = 'https://github.com/johncblandii/hash_serializer'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.test_files = `git ls-files -- spec/*`.split("\n")
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
spec.required_ruby_version = '>= 2.2.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
25
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.8'
|
26
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6.5'
|
27
|
+
spec.add_development_dependency 'guard-rubocop', '~> 1.2', '>= 1.2.0'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_development_dependency 'rubocop', '~> 0.39.0'
|
31
|
+
spec.add_development_dependency 'simplecov', '~> 0.11.2'
|
32
|
+
spec.add_development_dependency 'yard', '~> 0.8.7.6'
|
33
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module HashSerializer
|
2
|
+
# Helper methods for generating methods from hash keys and validating keys
|
3
|
+
module Helpers
|
4
|
+
# Validates a Postgres JSON hash on an ActiveRecord model does not include extra keys. It prevents user created
|
5
|
+
# data on JSON column types.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# >> validate_hash_serializer :billing_hash, %w(name address city state)
|
9
|
+
#
|
10
|
+
# @param hash_name [Symbol, String]
|
11
|
+
# @param valid_keys [Array]
|
12
|
+
#
|
13
|
+
# @return [Array] a sorted Array of the invalid keys
|
14
|
+
def validate_hash_serializer_keys(hash_name, valid_keys)
|
15
|
+
return if send(hash_name).nil? # || !send("#{hash_name}_changed?")
|
16
|
+
|
17
|
+
invalid_keys = send(hash_name).keys.map(&:to_s) - valid_keys.map(&:to_s)
|
18
|
+
return if invalid_keys.empty?
|
19
|
+
|
20
|
+
invalid_keys.sort
|
21
|
+
end
|
22
|
+
|
23
|
+
# Creates ActiveRecord model methods for a Postgres JSON hash
|
24
|
+
#
|
25
|
+
# Example:
|
26
|
+
# >> store_accessor_with_prefix :stripe_oauth_fields, 'stripe_connect', VALID_STRIPE_OAUTH_FIELDS
|
27
|
+
#
|
28
|
+
# @param hash [Hash]
|
29
|
+
# @param prefix [String] prefix for the generated methods
|
30
|
+
# @param keys [Array] array of strings to create methods
|
31
|
+
def hash_accessor_with_prefix(hash, prefix, *keys)
|
32
|
+
Array(keys).flatten.each do |key|
|
33
|
+
prefixed_key = "#{prefix}_#{key}"
|
34
|
+
|
35
|
+
# Ex - billing_token=
|
36
|
+
create_setter_methods(hash, prefixed_key, key)
|
37
|
+
|
38
|
+
# Ex - billing_token
|
39
|
+
create_getters(hash, prefixed_key, key)
|
40
|
+
|
41
|
+
# Ex - billing_token_changed?
|
42
|
+
create_changed_methods(prefixed_key)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def create_method(name, &block)
|
49
|
+
self.class.send(:define_method, name, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Creates setter methods with the prefixed name and adds @*_changed properties, if the value changed,
|
53
|
+
# and calls [hash]_will_change! to signify the hash has updated
|
54
|
+
#
|
55
|
+
# @param hash [Hash]
|
56
|
+
# @param prefixed_key [String] the hash key with the desired prefix
|
57
|
+
# @param key [String] the non-prefixed hash key
|
58
|
+
def create_setter_methods(hash, prefixed_key, key)
|
59
|
+
create_method("#{prefixed_key}=") do |value|
|
60
|
+
# Set a variable to track whether the value changed
|
61
|
+
instance_variable_set("@#{prefixed_key}_changed", true) if send(hash)[key] != value
|
62
|
+
|
63
|
+
# Store the value
|
64
|
+
send(hash)[key] = value
|
65
|
+
send("#{hash}_will_change!") if respond_to? "#{hash}_will_change!".to_sym
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Creates prefixed getter methods to access the hash value
|
70
|
+
#
|
71
|
+
# @param hash [Hash]
|
72
|
+
# @param prefixed_key [String] the hash key with the desired prefix
|
73
|
+
# @param key [String] the non-prefixed hash key
|
74
|
+
def create_getters(hash, prefixed_key, key)
|
75
|
+
create_method(prefixed_key) do
|
76
|
+
send(hash)[key]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Creates *_changed? methods referencing the @*_changed property created in create_setter_methods
|
81
|
+
#
|
82
|
+
# @param prefixed_key [String] the hash key with the desired prefix
|
83
|
+
def create_changed_methods(prefixed_key)
|
84
|
+
create_method("#{prefixed_key}_changed?") do
|
85
|
+
instance_variable_get("@#{prefixed_key}_changed") == true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_support/core_ext/hash'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module HashSerializer
|
5
|
+
# Serializes Ruby objects to JSON for storage in Postgres tables
|
6
|
+
module Serializer
|
7
|
+
# Dump the contents of hash to JSON
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# >> HashSerializer.dump({name: 'John'})
|
11
|
+
# => "{'name': 'John'}"
|
12
|
+
#
|
13
|
+
# @param hash [Hash]
|
14
|
+
def self.dump(hash)
|
15
|
+
hash.to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
# Loads the contents of hash from JSON if hash is a String or returns the array otherwise
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# >> HashSerializer.load("{name: 'John'}")
|
22
|
+
# => {'name': 'John'}
|
23
|
+
#
|
24
|
+
# >> HashSerializer.load({name: 'John'})
|
25
|
+
# => {'name': 'John'}
|
26
|
+
#
|
27
|
+
# >> HashSerializer.load(nil)
|
28
|
+
# => {}
|
29
|
+
#
|
30
|
+
# @param hash [String, Hash]
|
31
|
+
def self.load(hash)
|
32
|
+
hash = JSON.parse(hash) if hash.is_a?(String)
|
33
|
+
(hash || {}).with_indifferent_access
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Helper model for testing purposes
|
4
|
+
class HelpersTestModel
|
5
|
+
include HashSerializer::Helpers
|
6
|
+
|
7
|
+
def initialize(hash = {})
|
8
|
+
@hash = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def billing=(hash)
|
12
|
+
@hash = hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def billing
|
16
|
+
@hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def billing_will_change!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe HashSerializer::Helpers do
|
24
|
+
let(:hash) { { name: 'John C. Bland II', zipcode: 78_377 } }
|
25
|
+
|
26
|
+
subject { HelpersTestModel.new }
|
27
|
+
let(:valid_keys) { %w(name zipcode) }
|
28
|
+
let(:invalid_keys) { %w(invalid keys) }
|
29
|
+
|
30
|
+
context 'validate_hash_serializer_keys' do
|
31
|
+
it 'should return invalid keys' do
|
32
|
+
hash['invalid'] = true
|
33
|
+
hash['keys'] = false
|
34
|
+
|
35
|
+
subject.billing = hash
|
36
|
+
expect(subject.validate_hash_serializer_keys(:billing, valid_keys)).to eq(invalid_keys)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return nil if no invalid keys exist' do
|
40
|
+
expect(subject.validate_hash_serializer_keys(:billing, valid_keys)).to be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'hash_accessor_with_prefix' do
|
45
|
+
let(:prefix) { 'billing_stuff' }
|
46
|
+
|
47
|
+
context 'not called' do
|
48
|
+
it 'setters do not exist' do
|
49
|
+
expect(subject.respond_to?(:billing_stuff_name=)).to be_falsey
|
50
|
+
expect(subject.respond_to?(:billing_stuff_zipcode=)).to be_falsey
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'getters do not exist' do
|
54
|
+
expect(subject.respond_to?(:billing_stuff_name)).to be_falsey
|
55
|
+
expect(subject.respond_to?(:billing_stuff_zipcode)).to be_falsey
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'changed? methods exist' do
|
59
|
+
expect(subject.respond_to?(:billing_stuff_name_changed?)).to be_falsey
|
60
|
+
expect(subject.respond_to?(:billing_stuff_zipcode_changed?)).to be_falsey
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'called' do
|
65
|
+
before(:each) { subject.hash_accessor_with_prefix(:billing, prefix, valid_keys) }
|
66
|
+
|
67
|
+
it 'setters exist' do
|
68
|
+
expect(subject.respond_to?(:billing_stuff_name=)).to be_truthy
|
69
|
+
expect(subject.respond_to?(:billing_stuff_zipcode=)).to be_truthy
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'getters exist' do
|
73
|
+
expect(subject.respond_to?(:billing_stuff_name)).to be_truthy
|
74
|
+
expect(subject.respond_to?(:billing_stuff_zipcode)).to be_truthy
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'changed? methods exist' do
|
78
|
+
expect(subject.respond_to?(:billing_stuff_name_changed?)).to be_truthy
|
79
|
+
expect(subject.respond_to?(:billing_stuff_zipcode_changed?)).to be_truthy
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'methods works' do
|
83
|
+
new_name = 'John Bland III'
|
84
|
+
new_zipcode = 85_008
|
85
|
+
|
86
|
+
expect(subject).to receive(:billing_will_change!).twice
|
87
|
+
|
88
|
+
subject.billing_stuff_name = new_name
|
89
|
+
subject.billing_stuff_zipcode = new_zipcode
|
90
|
+
|
91
|
+
expect(subject.billing_stuff_name).to eq(new_name)
|
92
|
+
expect(subject.billing_stuff_name_changed?).to be_truthy
|
93
|
+
expect(subject.billing_stuff_zipcode).to eq(new_zipcode)
|
94
|
+
expect(subject.billing_stuff_zipcode_changed?).to be_truthy
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashSerializer::Serializer do
|
4
|
+
let(:hash) { { name: 'John' } }
|
5
|
+
let(:indifferent_hash) { hash.with_indifferent_access }
|
6
|
+
|
7
|
+
it 'dump' do
|
8
|
+
expect(HashSerializer::Serializer.dump(hash)).to eq(hash.to_json)
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'load' do
|
12
|
+
it 'hash' do
|
13
|
+
expect(hash).to receive(:with_indifferent_access).and_return(indifferent_hash)
|
14
|
+
|
15
|
+
expect(HashSerializer::Serializer.load(hash)).to eq(indifferent_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'JSON string' do
|
19
|
+
expect(HashSerializer::Serializer.load(hash.to_json)).to eq(indifferent_hash)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'nil' do
|
23
|
+
expect(HashSerializer::Serializer.load(nil)).to eq({})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashSerializer do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(HashSerializer::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'has the serializer included' do
|
9
|
+
expect(HashSerializer::Serializer).not_to be nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'has the helpers included' do
|
13
|
+
expect(HashSerializer::Helpers).not_to be nil
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
SimpleCov.start
|
4
|
+
SimpleCov.minimum_coverage 99
|
5
|
+
SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
|
6
|
+
|
7
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
8
|
+
require 'codeclimate-test-reporter'
|
9
|
+
CodeClimate::TestReporter.start
|
10
|
+
end
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
13
|
+
require 'hash_serializer'
|
metadata
ADDED
@@ -0,0 +1,214 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John C. Bland II
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-26 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: codeclimate-test-reporter
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.4.8
|
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.8
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.6'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 4.6.5
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '4.6'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 4.6.5
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: guard-rubocop
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.2'
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 1.2.0
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.2'
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 1.2.0
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rake
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '10.0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '10.0'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rspec
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3.0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '3.0'
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: rubocop
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: 0.39.0
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 0.39.0
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: simplecov
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 0.11.2
|
130
|
+
type: :development
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - "~>"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: 0.11.2
|
137
|
+
- !ruby/object:Gem::Dependency
|
138
|
+
name: yard
|
139
|
+
requirement: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - "~>"
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: 0.8.7.6
|
144
|
+
type: :development
|
145
|
+
prerelease: false
|
146
|
+
version_requirements: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - "~>"
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: 0.8.7.6
|
151
|
+
description: Stuff happens
|
152
|
+
email:
|
153
|
+
- johncblandii@gmail.com
|
154
|
+
executables: []
|
155
|
+
extensions: []
|
156
|
+
extra_rdoc_files: []
|
157
|
+
files:
|
158
|
+
- ".codeclimate.yml"
|
159
|
+
- ".github/ISSUE_TEMPLATE"
|
160
|
+
- ".github/PULL_REQUEST_TEMPLATE"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rubocop.yml"
|
163
|
+
- ".travis.yml"
|
164
|
+
- CODE_OF_CONDUCT.md
|
165
|
+
- Gemfile
|
166
|
+
- Gemfile.lock
|
167
|
+
- Guardfile
|
168
|
+
- LICENSE
|
169
|
+
- LICENSE.txt
|
170
|
+
- README.md
|
171
|
+
- Rakefile
|
172
|
+
- bin/console
|
173
|
+
- bin/setup
|
174
|
+
- hash_serializer.gemspec
|
175
|
+
- lib/hash_serializer.rb
|
176
|
+
- lib/hash_serializer/helpers.rb
|
177
|
+
- lib/hash_serializer/serializer.rb
|
178
|
+
- lib/hash_serializer/version.rb
|
179
|
+
- spec/hash_serializer/helpers_spec.rb
|
180
|
+
- spec/hash_serializer/serializer_spec.rb
|
181
|
+
- spec/hash_serializer/version_spec.rb
|
182
|
+
- spec/hash_serializer_spec.rb
|
183
|
+
- spec/spec_helper.rb
|
184
|
+
homepage: https://github.com/johncblandii/hash_serializer
|
185
|
+
licenses:
|
186
|
+
- MIT
|
187
|
+
metadata: {}
|
188
|
+
post_install_message:
|
189
|
+
rdoc_options: []
|
190
|
+
require_paths:
|
191
|
+
- lib
|
192
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
193
|
+
requirements:
|
194
|
+
- - ">="
|
195
|
+
- !ruby/object:Gem::Version
|
196
|
+
version: 2.2.0
|
197
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
requirements: []
|
203
|
+
rubyforge_project:
|
204
|
+
rubygems_version: 2.5.1
|
205
|
+
signing_key:
|
206
|
+
specification_version: 4
|
207
|
+
summary: A serializer and helpers for Postgres JSON columns.
|
208
|
+
test_files:
|
209
|
+
- spec/hash_serializer/helpers_spec.rb
|
210
|
+
- spec/hash_serializer/serializer_spec.rb
|
211
|
+
- spec/hash_serializer/version_spec.rb
|
212
|
+
- spec/hash_serializer_spec.rb
|
213
|
+
- spec/spec_helper.rb
|
214
|
+
has_rdoc:
|