shapeshifter 0.0.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/.rspec +2 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +14 -0
- data/lib/shapeshifter.rb +8 -0
- data/lib/shapeshifter/null_shifter.rb +7 -0
- data/lib/shapeshifter/shift_chain.rb +36 -0
- data/lib/shapeshifter/shifter.rb +31 -0
- data/lib/shapeshifter/version.rb +3 -0
- data/shapeshifter.gemspec +26 -0
- data/spec/fixtures/test_shifter.rb +3 -0
- data/spec/fixtures/test_shifter2.rb +3 -0
- data/spec/shapeshifter/shifter_spec.rb +55 -0
- data/spec/spec_helper.rb +71 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 24ea440ce974a1b51f22398d4e9d11948208a12c
|
4
|
+
data.tar.gz: 4317404d75d4ee54caddff085b1cec73d4bf5a61
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a1480841cec56239fc71df4b2bd9ca3cc861ee4be9a005c6e446105fb40d43f8421bf18160b2c714bfed9f2c3f5b36f0b4e412b86728dabd5bb88d00d9cc4b0
|
7
|
+
data.tar.gz: b64cc78d67df860e33c7918060f08c416c262cd46c33293b10574833635d269cba2a297d03dc2a84a1102af5ddd9be4a3406423fe56a2bc6dc18fbb17724a65a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Max
|
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,31 @@
|
|
1
|
+
# Shapeshifter
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'shapeshifter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install shapeshifter
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/shapeshifter/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'lib'
|
8
|
+
t.libs << 'spec'
|
9
|
+
t.pattern = 'spec/**/*_spec.rb'
|
10
|
+
t.verbose = false
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
task default: :spec
|
data/lib/shapeshifter.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Shapeshifter
|
2
|
+
class ShiftChain
|
3
|
+
def initialize(first)
|
4
|
+
@shifters = [first]
|
5
|
+
end
|
6
|
+
|
7
|
+
def chain(shifter)
|
8
|
+
shifters << shifter
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def shift(source_object, target_object)
|
13
|
+
can_be_dupped = source_object.respond_to?(:dup)
|
14
|
+
shifters.each do |shifter_class|
|
15
|
+
so = can_be_dupped ? source_object.dup : source_object
|
16
|
+
shifter = shifter_class.new(so)
|
17
|
+
target_object = shifter.shift(target_object)
|
18
|
+
end
|
19
|
+
target_object
|
20
|
+
end
|
21
|
+
|
22
|
+
def revert(source_object, target_object)
|
23
|
+
can_be_dupped = source_object.respond_to?(:dup)
|
24
|
+
shifters.reverse.each do |shifter_class|
|
25
|
+
so = can_be_dupped ? source_object.dup : source_object
|
26
|
+
shifter = shifter_class.new(so)
|
27
|
+
target_object = shifter.revert(target_object)
|
28
|
+
end
|
29
|
+
target_object
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :shifters
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Shapeshifter
|
2
|
+
class Shifter
|
3
|
+
attr_reader :source_object
|
4
|
+
|
5
|
+
def initialize(source_object)
|
6
|
+
@source_object = source_object
|
7
|
+
end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def chain(shifter)
|
11
|
+
ShiftChain.new(self).chain(shifter)
|
12
|
+
end
|
13
|
+
|
14
|
+
def shift(source_object, target_object)
|
15
|
+
ShiftChain.new(self).shift(source_object, target_object)
|
16
|
+
end
|
17
|
+
|
18
|
+
def revert(source_object, target_object)
|
19
|
+
ShiftChain.new(self).revert(source_object, target_object)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def shift(_)
|
24
|
+
raise NoMethodError.new('Should be overridden')
|
25
|
+
end
|
26
|
+
|
27
|
+
def revert(_)
|
28
|
+
raise NoMethodError.new('Should be overridden')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'shapeshifter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "shapeshifter"
|
8
|
+
spec.version = Shapeshifter::VERSION
|
9
|
+
spec.authors = ["Max"]
|
10
|
+
spec.email = ["max@driftrock.com"]
|
11
|
+
spec.summary = %q{Shapeshifter allows you to transform data types}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "simplecov"
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require './lib/shapeshifter'
|
2
|
+
require 'spec_helper'
|
3
|
+
require './lib/shapeshifter'
|
4
|
+
require './spec/fixtures/test_shifter'
|
5
|
+
require './spec/fixtures/test_shifter2'
|
6
|
+
|
7
|
+
module Shapeshifter
|
8
|
+
RSpec.describe Shifter do
|
9
|
+
it 'should allow me to chain shifters' do
|
10
|
+
expect do
|
11
|
+
TestShifter.chain(TestShifter2)
|
12
|
+
end.to_not raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '::shift' do
|
16
|
+
it 'should allow me to call shift and have it call shift on all shifters' do
|
17
|
+
expect_any_instance_of(TestShifter).to receive(:shift).and_return({})
|
18
|
+
expect_any_instance_of(TestShifter2).to receive(:shift).and_return({})
|
19
|
+
TestShifter.chain(TestShifter2).shift({}, {})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '::revert' do
|
24
|
+
it 'should allow me to call revert and have it call revert on all shifters' do
|
25
|
+
expect_any_instance_of(TestShifter).to receive(:revert).and_return({})
|
26
|
+
expect_any_instance_of(TestShifter2).to receive(:revert).and_return({})
|
27
|
+
TestShifter.chain(TestShifter2).revert({}, {})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when a shift is made' do
|
32
|
+
before do
|
33
|
+
test_shifter = double(TestShifter)
|
34
|
+
allow(TestShifter).to receive(:new).and_return(test_shifter)
|
35
|
+
allow(test_shifter).to receive(:shift) do |target|
|
36
|
+
target[:a] = Array(target[:a]).concat(target[:b])
|
37
|
+
target
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should change the passed target' do
|
42
|
+
target = { a: 1, b: [2, 3] }
|
43
|
+
expect do
|
44
|
+
TestShifter.shift({}, target)
|
45
|
+
end.to change { target }.to eq ({ a: [1, 2, 3], b: [2, 3] })
|
46
|
+
end
|
47
|
+
end
|
48
|
+
#
|
49
|
+
#class TestShifter < Shifter
|
50
|
+
# def shift(old_object, new_object)
|
51
|
+
# new_oib
|
52
|
+
# end
|
53
|
+
#end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
RSpec.configure do |config|
|
4
|
+
# rspec-expectations config goes here. You can use an alternate
|
5
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
6
|
+
# assertions if you prefer.
|
7
|
+
config.expect_with :rspec do |expectations|
|
8
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
9
|
+
# and `failure_message` of custom matchers include text for helper methods
|
10
|
+
# defined using `chain`, e.g.:
|
11
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
12
|
+
# # => "be bigger than 2 and smaller than 4"
|
13
|
+
# ...rather than:
|
14
|
+
# # => "be bigger than 2"
|
15
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
16
|
+
end
|
17
|
+
|
18
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
19
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
20
|
+
config.mock_with :rspec do |mocks|
|
21
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
22
|
+
# a real object. This is generally recommended, and will default to
|
23
|
+
# `true` in RSpec 4.
|
24
|
+
mocks.verify_partial_doubles = true
|
25
|
+
end
|
26
|
+
|
27
|
+
# The settings below are suggested to provide a good initial experience
|
28
|
+
# with RSpec, but feel free to customize to your heart's content.
|
29
|
+
=begin
|
30
|
+
# These two settings work together to allow you to limit a spec run
|
31
|
+
# to individual examples or groups you care about by tagging them with
|
32
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
33
|
+
# get run.
|
34
|
+
config.filter_run :focus
|
35
|
+
config.run_all_when_everything_filtered = true
|
36
|
+
|
37
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
38
|
+
# For more details, see:
|
39
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
40
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
41
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
42
|
+
config.disable_monkey_patching!
|
43
|
+
|
44
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
45
|
+
# file, and it's useful to allow more verbose output when running an
|
46
|
+
# individual spec file.
|
47
|
+
if config.files_to_run.one?
|
48
|
+
# Use the documentation formatter for detailed output,
|
49
|
+
# unless a formatter has already been configured
|
50
|
+
# (e.g. via a command-line flag).
|
51
|
+
config.default_formatter = 'doc'
|
52
|
+
end
|
53
|
+
|
54
|
+
# Print the 10 slowest examples and example groups at the
|
55
|
+
# end of the spec run, to help surface which specs are running
|
56
|
+
# particularly slow.
|
57
|
+
config.profile_examples = 10
|
58
|
+
|
59
|
+
# Run specs in random order to surface order dependencies. If you find an
|
60
|
+
# order dependency and want to debug it, you can fix the order by providing
|
61
|
+
# the seed, which is printed after each run.
|
62
|
+
# --seed 1234
|
63
|
+
config.order = :random
|
64
|
+
|
65
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
66
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
67
|
+
# test failures related to randomization by passing the same `--seed` value
|
68
|
+
# as the one that triggered the failure.
|
69
|
+
Kernel.srand config.seed
|
70
|
+
=end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shapeshifter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-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: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: ''
|
84
|
+
email:
|
85
|
+
- max@driftrock.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/shapeshifter.rb
|
97
|
+
- lib/shapeshifter/null_shifter.rb
|
98
|
+
- lib/shapeshifter/shift_chain.rb
|
99
|
+
- lib/shapeshifter/shifter.rb
|
100
|
+
- lib/shapeshifter/version.rb
|
101
|
+
- shapeshifter.gemspec
|
102
|
+
- spec/fixtures/test_shifter.rb
|
103
|
+
- spec/fixtures/test_shifter2.rb
|
104
|
+
- spec/shapeshifter/shifter_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: ''
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.2.2
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Shapeshifter allows you to transform data types
|
130
|
+
test_files:
|
131
|
+
- spec/fixtures/test_shifter.rb
|
132
|
+
- spec/fixtures/test_shifter2.rb
|
133
|
+
- spec/shapeshifter/shifter_spec.rb
|
134
|
+
- spec/spec_helper.rb
|