morfo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33e1098bcb8997477f6e2eb34e09e03e17a66522
4
+ data.tar.gz: e8896807f2bd7a9c244d21df27fdd838598632e0
5
+ SHA512:
6
+ metadata.gz: 6fe068a0ee55fee7b9d1bcc399472bde5307b5c7d70433b786ea2420cfcf3f3fd7fd15f23650f0141bee07fb0a62b7c286e0be298eaa472d9c01b207abba58b2
7
+ data.tar.gz: 9dd5b3c0da7fb7b632efab6cd36b6ae66d2d83bb9424fa97709e0f9458f4fd8f3e385e13abe7ef3a1312a5198461042fcc98090530ec699db85f7c0853d6dca6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --tty --color --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ cache: bundler
3
+ env: JRUBY_OPTS=--2.0
4
+ rvm:
5
+ - 2.0.0
6
+ - jruby-1.7.9
7
+ - ruby-head
8
+ - jruby-head
9
+ - rbx-2.2.1
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ - rvm: jruby-head
14
+ - rvm: rbx-2.2.1
15
+ script:
16
+ - rake spec
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in morfo.gemspec
4
+ gemspec
5
+
6
+ group :test, :development do
7
+ gem 'coveralls', require: false
8
+ gem 'guard'
9
+ gem 'guard-rspec'
10
+ gem 'simplecov'
11
+ gem 'pry'
12
+
13
+ gem 'rb-inotify', require: false
14
+ gem 'rb-fsevent', require: false
15
+ gem 'rb-fchange', require: false
16
+ end
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, all_on_start: true do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { 'spec' }
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Leif Gensert
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,104 @@
1
+ # Morfo
2
+
3
+ [![Build Status](https://travis-ci.org/leifg/morfo.png?branch=master)](https://travis-ci.org/leifg/morfo) [![Coverage Status](https://coveralls.io/repos/leifg/morfo/badge.png?branch=master)](https://coveralls.io/r/leifg/morfo) [![Code Climate](https://codeclimate.com/github/leifg/morfo.png)](https://codeclimate.com/github/leifg/morfo) [![Dependency Status](https://gemnasium.com/leifg/morfo.png)](https://gemnasium.com/leifg/morfo) [![Gem Version](https://badge.fury.io/rb/morfo.png)](http://badge.fury.io/rb/morfo)
4
+
5
+ This Gem is inspired by the [active_importer](https://github.com/continuum/active_importer) Gem.
6
+
7
+ But instead of importing spreadsheets into models, you can morf (typo intended) arrays of Hashes into other arrays of hashes.
8
+
9
+ ## Compatibility
10
+
11
+ This gem is currently only tested on Ruby 2.0 (including 2.0 mode of JRuby and RBX).
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'morfo'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install morfo
26
+
27
+ ## Usage
28
+
29
+ In order to morf the hashes you have to provide a class that extends `Morf::Base`
30
+
31
+ Use the `map` method to specify what field you map to another field:
32
+
33
+ class TitleMorfer < Morfo::Base
34
+ map :title, :tv_show_title
35
+ end
36
+
37
+ Afterwards use the `morf` method to morf all hashes in one array to the end result:
38
+
39
+ Title.morf([
40
+ {title: 'The Walking Dead'} ,
41
+ {title: 'Breaking Bad'},
42
+ ])
43
+
44
+ # [
45
+ # {tv_show_title: 'The Walking Dead'},
46
+ # {tv_show_title: 'Breaking Bad'},
47
+ # ]
48
+
49
+ ## Transformations
50
+
51
+ For each mapping you can define a block, that will be called on every input:
52
+
53
+ class AndZombies < Morfo::Base
54
+ map :title, :title do |title|
55
+ "#{title} and Zombies"
56
+ end
57
+ end
58
+
59
+ AndZombies.morf([
60
+ {title: 'Pride and Prejudice'},
61
+ {title: 'Fifty Shades of Grey'},
62
+ ])
63
+
64
+ # [
65
+ # {title: 'Pride and Prejudice and Zombies'},
66
+ # {title: 'Fifty Shades of Grey and Zombies'},
67
+ # ]
68
+
69
+ ## Nested Values
70
+
71
+ You can directly access nested values in the hashes:
72
+
73
+ class Name < Morfo::Base
74
+ map [:name, :firs], :first_name
75
+ map [:name, :last], :last_name
76
+ end
77
+
78
+ Name.morf([
79
+ {
80
+ name: {
81
+ first: 'Clark',
82
+ last: 'Kent',
83
+ },
84
+ },
85
+ {
86
+ name: {
87
+ first: 'Bruce',
88
+ last: 'Wayne',
89
+ },
90
+ },
91
+ ])
92
+
93
+ # [
94
+ # {first_name: 'Clark',last_name: 'Kent'},
95
+ # {first_name: 'Bruce',last_name: 'Wayne'},,
96
+ # ]
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,3 @@
1
+ module Morfo
2
+ VERSION = '0.0.1'
3
+ end
data/lib/morfo.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'morfo/version'
2
+
3
+ module Morfo
4
+ class Base
5
+ def self.map from, to, &transformation
6
+ mapping_actions << [from, to, transformation]
7
+ end
8
+
9
+ def self.morf input
10
+ input.map do |value|
11
+ mapping_actions.inject({}) do |output, (from, to, transformation)|
12
+ resulting_value = apply_transformation(
13
+ extract_value(value, from),
14
+ transformation
15
+ )
16
+ output.merge!(to => resulting_value) if resulting_value
17
+ output
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+ def self.extract_value value, from
24
+ Array(from).inject(value) do |resulting_value, key|
25
+ resulting_value ? resulting_value[key] : nil
26
+ end
27
+ end
28
+
29
+ def self.apply_transformation value, transformation
30
+ transformation ? transformation.call(value) : value
31
+ end
32
+
33
+ def self.mapping_actions
34
+ @actions ||= []
35
+ end
36
+ end
37
+ end
data/morfo.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'morfo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'morfo'
8
+ spec.version = Morfo::VERSION
9
+ spec.authors = ['Leif Gensert']
10
+ spec.email = ['leif@propertybase.com']
11
+ spec.description = %q{This gem provides a DSL for converting one hash into another}
12
+ spec.summary = %q{Inspired by ActiveImporter, this gem generically converts an array of hashes}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '>= 2.14', '< 4.0'
24
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Morfo::Base do
4
+ let(:input) do
5
+ [
6
+ {
7
+ title: 'The Walking Dead',
8
+ channel: 'AMC',
9
+ watchers: 1337,
10
+ status: 'running',
11
+ cast: ['Lincoln, Andrew', 'McBride, Melissa'],
12
+ ratings: {
13
+ imdb: 8.7,
14
+ trakt: 89,
15
+ rotten_tomatoes: 93,
16
+ },
17
+ },
18
+ {
19
+ title: 'Breaking Bad',
20
+ channel: 'AMC',
21
+ watchers: 72891,
22
+ status: 'ended',
23
+ cast: ['Cranston, Bryan', 'Gunn, Anna'],
24
+ ratings: {
25
+ imdb: 9.5,
26
+ trakt: 95,
27
+ rotten_tomatoes: 100,
28
+ },
29
+ }
30
+ ]
31
+ end
32
+
33
+ describe '#morf' do
34
+ context '1 to 1 conversion' do
35
+ subject do
36
+ class TitleMapper < Morfo::Base
37
+ map :title, :tv_show_title
38
+ end
39
+ TitleMapper
40
+ end
41
+
42
+ it 'maps title correctly' do
43
+ expected_output = input.map{|v| {tv_show_title: v[:title]} }
44
+ expect(subject.morf(input)).to eq(expected_output)
45
+ end
46
+
47
+ it 'leaves out nil values in result' do
48
+ expected_output = [{},{}]
49
+ modified_input = input.map{|h| h.reject{|k, v| k == :title}}
50
+ expect(subject.morf(modified_input)).to eq(expected_output)
51
+ end
52
+ end
53
+
54
+ context '1 to 1 conversion with transformation' do
55
+ subject do
56
+ class NumCastMapper < Morfo::Base
57
+ map :cast, :cast_num do |cast|
58
+ cast.size
59
+ end
60
+ end
61
+ NumCastMapper
62
+ end
63
+
64
+ it 'calls transformation correctly' do
65
+ expected_output = input.map{|v| {cast_num: v[:cast].size} }
66
+ expect(subject.morf(input)).to eq(expected_output)
67
+ end
68
+ end
69
+
70
+ context 'nested conversion' do
71
+ subject(:valid_path) do
72
+ class ImdbRatingMapper < Morfo::Base
73
+ map [:ratings, :imdb], :rating
74
+ end
75
+ ImdbRatingMapper
76
+ end
77
+
78
+ subject(:invalid_path) do
79
+ class InvalidImdbRatingMapper < Morfo::Base
80
+ map [:very, :long, :path, :that, :might, :not, :exist], :rating
81
+ end
82
+ InvalidImdbRatingMapper
83
+ end
84
+
85
+ it 'maps nested attributes' do
86
+ expected_output = input.map{|v| {rating: v[:ratings][:imdb]} }
87
+ expect(valid_path.morf(input)).to eq(expected_output)
88
+ end
89
+
90
+ it 'doesn\'t raise error for invalid path' do
91
+ expected_output = [{},{}]
92
+ expect(invalid_path.morf(input)).to eq(expected_output)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,10 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
5
+ SimpleCov.start do
6
+ add_filter 'spec'
7
+ end
8
+
9
+ require 'rspec'
10
+ require 'morfo'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: morfo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Leif Gensert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-20 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '2.14'
48
+ - - <
49
+ - !ruby/object:Gem::Version
50
+ version: '4.0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '2.14'
58
+ - - <
59
+ - !ruby/object:Gem::Version
60
+ version: '4.0'
61
+ description: This gem provides a DSL for converting one hash into another
62
+ email:
63
+ - leif@propertybase.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .rspec
70
+ - .travis.yml
71
+ - Gemfile
72
+ - Guardfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/morfo.rb
77
+ - lib/morfo/version.rb
78
+ - morfo.gemspec
79
+ - spec/lib/morfo_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.14
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Inspired by ActiveImporter, this gem generically converts an array of hashes
105
+ test_files:
106
+ - spec/lib/morfo_spec.rb
107
+ - spec/spec_helper.rb