serialized-hashie 0.0.0.dev

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b305ccfa0e54e4a808259f967ed9d4ebc7cc95b7fa52b8930196ae97d6db5a39
4
+ data.tar.gz: 77edb806bc4dd9f7b425ddacc4c566925debc1d5e5fd02add53b7b883fcccf4d
5
+ SHA512:
6
+ metadata.gz: b417aa6c94e37ca8eb989faf359ffea46d145f66174f9560a7e4ecd1b4f02909bc86190c79da913c76e388c63971dbe0a24f68db9b2c204ad846cf593c6f49ab
7
+ data.tar.gz: 6c486630d660930d70e7bfd740d53013c7c35ea4d683309e4d7277fd30efd76b5b24ca4f34fb2367a91f0b8b8e4455733aa91e23290b4b9e1144e77a23caf43f
@@ -0,0 +1,21 @@
1
+ name: CI
2
+ on: [push]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ ruby_version:
10
+ - 2.5
11
+ - 2.6
12
+ - 2.7
13
+ steps:
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby_version }}
17
+ - uses: actions/checkout@v2
18
+ - name: Install dependencies
19
+ run: bundle install && bundle exec appraisal install
20
+ - name: Run tests
21
+ run: bundle exec appraisal rspec
@@ -0,0 +1,43 @@
1
+ name: Build Package
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "*"
7
+
8
+ jobs:
9
+ build:
10
+ name: Build + Publish
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@master
15
+ - name: Set up Ruby 2.6
16
+ uses: actions/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.6.x
19
+
20
+ - name: Build Gem
21
+ run: gem build *.gemspec
22
+
23
+ - name: Export version from tag name
24
+ run: echo "${GITHUB_REF/refs\/tags\//}" > VERSION
25
+
26
+ - name: Setup credentials
27
+ run: |
28
+ mkdir -p $HOME/.gem
29
+ touch $HOME/.gem/credentials
30
+ chmod 0600 $HOME/.gem/credentials
31
+ printf -- "---\n:rubygems_api_key: ${RUBYGEMS_API_KEY}\n" > $HOME/.gem/credentials
32
+ printf -- ":github: Bearer ${GITHUB_API_KEY}\n" >> $HOME/.gem/credentials
33
+ env:
34
+ RUBYGEMS_API_KEY: ${{secrets.KRYSTAL_RUBYGEMS_API_KEY}}
35
+ GITHUB_API_KEY: ${{secrets.GITHUB_TOKEN}}
36
+
37
+ - name: Publish to RubyGems
38
+ run: |
39
+ gem push *.gem
40
+
41
+ - name: Publish to GPR
42
+ run: |
43
+ gem push --key github --host https://rubygems.pkg.github.com/krystal *.gem
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # Appraisals generated Gemfiles
12
+ /gemfiles/
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
16
+ .rubocop-http*
17
+ VERSION
@@ -0,0 +1,2 @@
1
+ inherit_from:
2
+ - https://dev.k.io/rubocop/rubocop.yml
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'json-1.8' do
4
+ gem 'json', '~> 1.8.0'
5
+ end
6
+
7
+ appraise 'json-2' do
8
+ gem 'json', '~> 2.0'
9
+ end
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in serialized_hashie.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'rubocop'
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Krystal Hosting Ltd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # Serialized Hashie
2
+
3
+ This is a library that allows you to serialize a Hashie::Mash object (as JSON) into a database. It also features a couple of extra extensions to allow you to alter how objects are serialized/deserialized.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ source 'https://rubygems.pkg.github.com/krystal' do
11
+ gem 'serialized-hashie', '~> 1.0'
12
+ end
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ This is designed to be used a serializer for ActiveRecord models.
18
+
19
+ ```ruby
20
+ class User < ApplicationRecord
21
+ serialize :properties, SerializedHashie::Hash
22
+ end
23
+
24
+ user = User.new
25
+ user.properties.something = 'Hello world!'
26
+ user.save
27
+
28
+ user = User.last
29
+ user.properties.something #=> 'Hello world!'
30
+ ```
31
+
32
+ ### Extensions
33
+
34
+ The loading & dumping can also be extended to handle how individual entries in any hashes are handled.
35
+
36
+ ```ruby
37
+ # Add an extension to dump all stings in uppercase
38
+ SerializedHashie.dump_extensions.add(:upcase) do |value|
39
+ value.is_a?(String) ? value.upcase : value
40
+ end
41
+
42
+ # Add a load extension to always return them again in lowercase
43
+ SerializedHashie.load_extensions.add(:downcase) do |value|
44
+ value.is_a?(String) ? value.downcase : value
45
+ end
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/krystal/serialized-hashie.
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "serialized_hashie"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'serialized_hashie/version'
4
+ require 'serialized_hashie/hash'
5
+ require 'serialized_hashie/extensions'
6
+
7
+ module SerializedHashie
8
+
9
+ class Error < StandardError
10
+ end
11
+
12
+ class << self
13
+
14
+ def load_extensions
15
+ @load_extensions ||= Extensions.new
16
+ end
17
+
18
+ def dump_extensions
19
+ @dump_extensions ||= Extensions.new
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SerializedHashie
4
+ class Extensions
5
+
6
+ def initialize
7
+ reset
8
+ end
9
+
10
+ def reset
11
+ @extensions = {}
12
+ end
13
+
14
+ def add(name, &block)
15
+ if has?(name)
16
+ raise Error, "Extension already defined named '#{name}'"
17
+ end
18
+
19
+ @extensions[name.to_sym] = block
20
+ end
21
+
22
+ def has?(name)
23
+ @extensions.key?(name.to_sym)
24
+ end
25
+
26
+ def run(value)
27
+ @extensions.each_value do |block|
28
+ value = block.call(value)
29
+ end
30
+ value
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'serialized_hashie'
4
+ require 'hashie/mash'
5
+ require 'json'
6
+
7
+ module SerializedHashie
8
+ class Hash < Hashie::Mash
9
+
10
+ class << self
11
+
12
+ def dump(obj)
13
+ hash = dump_hash(obj)
14
+ hash.to_json
15
+ end
16
+
17
+ def load(raw_hash)
18
+ hash = JSON.parse(presence(raw_hash) || '{}')
19
+ hash = load_hash(hash)
20
+ new(hash)
21
+ end
22
+
23
+ private
24
+
25
+ def blank?(value)
26
+ return true if value.nil?
27
+ return true if value.is_a?(String) && value.empty?
28
+
29
+ false
30
+ end
31
+
32
+ def presence(value)
33
+ blank?(value) ? nil : value
34
+ end
35
+
36
+ def dump_hash(hash)
37
+ hash = hash.transform_values do |value|
38
+ dump_value(value)
39
+ end
40
+ hash.reject { |_, v| blank?(v) }
41
+ end
42
+
43
+ def dump_value(value)
44
+ if blank?(value)
45
+ return nil
46
+ end
47
+
48
+ if value.is_a?(::Hash)
49
+ return dump_hash(value)
50
+ end
51
+
52
+ if value.is_a?(::Array)
53
+ return value.map { |v| dump_value(v) }.compact
54
+ end
55
+
56
+ SerializedHashie.dump_extensions.run(value)
57
+ end
58
+
59
+ def load_hash(hash)
60
+ hash.transform_values do |value|
61
+ load_value(value)
62
+ end
63
+ end
64
+
65
+ def load_value(value)
66
+ return load_hash(value) if value.is_a?(::Hash)
67
+ return value.map { |v| load_value(v) } if value.is_a?(Array)
68
+
69
+ SerializedHashie.load_extensions.run(value)
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SerializedHashie
4
+
5
+ VERSION_FILE_ROOT = File.expand_path('../../VERSION', __dir__)
6
+ if File.file?(VERSION_FILE_ROOT)
7
+ VERSION = File.read(VERSION_FILE_ROOT).strip.delete_prefix('v')
8
+ else
9
+ VERSION = '0.0.0.dev'
10
+ end
11
+
12
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/serialized_hashie/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'serialized-hashie'
7
+ spec.version = SerializedHashie::VERSION
8
+ spec.authors = ['Adam Cooke']
9
+ spec.email = ['adam@krystal.uk']
10
+ spec.summary = 'Helpers to serialize data into ActiveRecord models as JSON and returning a Hashie::Mash'
11
+ spec.description = spec.summary
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+ spec.licenses = ['MIT']
14
+ spec.homepage = 'https://github.com/krystal/serialized-hashie'
15
+
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(/^(test|spec|features)\//) }
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'appraisal', '~> 2.3'
24
+
25
+ spec.add_runtime_dependency 'hashie', '>= 4.0', '< 5.0'
26
+ spec.add_runtime_dependency 'json', '>= 1.8', '< 3.0'
27
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialized-hashie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.dev
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: appraisal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '4.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '1.8'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '3.0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '1.8'
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '3.0'
67
+ description: Helpers to serialize data into ActiveRecord models as JSON and returning
68
+ a Hashie::Mash
69
+ email:
70
+ - adam@krystal.uk
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".github/workflows/ci.yml"
76
+ - ".github/workflows/package.yml"
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - Appraisals
80
+ - Gemfile
81
+ - MIT-LICENSE
82
+ - README.md
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/serialized_hashie.rb
86
+ - lib/serialized_hashie/extensions.rb
87
+ - lib/serialized_hashie/hash.rb
88
+ - lib/serialized_hashie/version.rb
89
+ - serialized-hashie.gemspec
90
+ homepage: https://github.com/krystal/serialized-hashie
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.3.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">"
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.1
108
+ requirements: []
109
+ rubygems_version: 3.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Helpers to serialize data into ActiveRecord models as JSON and returning
113
+ a Hashie::Mash
114
+ test_files: []