data_smash 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: f0212746bd2d2d910f17b9aeb87942f652717950
4
+ data.tar.gz: 777451f5199b4f3bbdd252354641a4348423c5f3
5
+ SHA512:
6
+ metadata.gz: e274a0163799355ffdfd3eb98f64629ad4deae0075a6ec27880a0ed9d61eb2b2ab7b5e272ac779148bbe526e82d277c2dbb5c7089c9643a2bf5dd7ef21bad43a
7
+ data.tar.gz: efb1571aa550848efe33416ed48af2c3b8be3dd9be7db262f44d21313845cee38e3b2b0a923eee3c3df7b327694c5cbc53f538040aac14f0bce5cfb679814020
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Asa Calow
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,59 @@
1
+ # DataSmash
2
+
3
+ DataSmash is an elegant picoframework for working with data – converting from one form to another, or importing into a database/service. No assumptions have been made about how data is parsed, processed or handled to make it as straightforward to use as possible.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'data_smash'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install data_smash
18
+
19
+ ## Usage
20
+
21
+ Using DataSmash is supersimple. You need a convertor and, optionally, a processor e.g.
22
+
23
+ class TestConvertor
24
+ include DataSmash::Convertor
25
+
26
+ convert ->(item, data) {
27
+ data[:bar] = item[:foo]
28
+ data
29
+ }
30
+ end
31
+
32
+ class TestProcessor
33
+ include DataSmash::Processor
34
+
35
+ process_with TestConvertor
36
+
37
+ smash_in -> {
38
+ [{foo: 'ding'}, {foo: 'dong'}].each {|d| process d }
39
+ }
40
+
41
+ smash_out ->(data) {
42
+ # do a thing with your data outputted here
43
+ }
44
+ end
45
+
46
+ You can just use the convertors on their own, by creating one and calling convert on it with some data e.g.
47
+
48
+ convertor = TestConvertor.new
49
+ converted_data = convertor.convert({foo: 'bar'})
50
+
51
+ And that's it!
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'data_smash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data_smash"
8
+ spec.version = DataSmash::VERSION
9
+ spec.authors = ["Asa Calow"]
10
+ spec.email = ["asacalow@gmail.com"]
11
+ spec.description = "An elegant picoframework for working with data"
12
+ spec.summary = ""
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"
24
+ end
@@ -0,0 +1,28 @@
1
+ module DataSmash
2
+ module Convertor
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def convert(convertor, opts={})
9
+ @CONVERTORS ||= {}
10
+ @CONVERTORS[convertor] ||= []
11
+ @CONVERTORS[convertor] << opts
12
+ end
13
+
14
+ def convertors
15
+ @CONVERTORS
16
+ end
17
+ end
18
+
19
+ def convert(item)
20
+ data = {}
21
+ self.class.convertors.each do |convertor, opts|
22
+ data = convertor.call(item, data)
23
+ return unless data
24
+ end
25
+ data
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ module DataSmash
2
+ module Processor
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def smash_in(in_proc)
9
+ @IN_PROC = in_proc
10
+ end
11
+
12
+ def smash_out(out_proc)
13
+ @OUT_PROC = out_proc
14
+ end
15
+
16
+ def process_with(klass)
17
+ @PROCESSOR_KLASS = klass
18
+ end
19
+
20
+ def process!
21
+ @IN_PROC.call
22
+ end
23
+
24
+ def process(item)
25
+ convertor = @PROCESSOR_KLASS.new
26
+ data = convertor.convert(item)
27
+ @OUT_PROC.call(data)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module DataSmash
2
+ VERSION = "0.0.1"
3
+ end
data/lib/data_smash.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "data_smash/version"
2
+ require "data_smash/convertor"
3
+ require "data_smash/processor"
4
+
5
+ module DataSmash
6
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ class TestConvertor
4
+ include DataSmash::Convertor
5
+
6
+ convert ->(item, data) {
7
+ return false unless item[:foo]
8
+ data
9
+ }
10
+
11
+ # from foo to bar
12
+ convert ->(item, data) {
13
+ data[:bar] = item[:foo]
14
+ data
15
+ }
16
+
17
+ # from ping to pong
18
+ convert ->(item, data) {
19
+ data[:pong] = item[:ping]
20
+ data
21
+ }
22
+ end
23
+
24
+ describe DataSmash::Convertor do
25
+ let(:convertor) { TestConvertor.new }
26
+ let(:item) {
27
+ {
28
+ foo: 'baz',
29
+ ping: 'ding'
30
+ }
31
+ }
32
+
33
+ describe '#convert' do
34
+ it 'should run the first convertor' do
35
+ data = convertor.convert(item)
36
+ data[:bar].should == item[:foo]
37
+ end
38
+
39
+ it 'should run the second convertor' do
40
+ data = convertor.convert(item)
41
+ data[:pong].should == item[:ping]
42
+ end
43
+
44
+ context 'given data which causes a convertor to stop' do
45
+ let(:invalid_item) { {ping: 'dong'} }
46
+
47
+ it 'should cease processing' do
48
+ data = convertor.convert(invalid_item)
49
+ data.should be_nil
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ class MyConvertor
4
+ include DataSmash::Convertor
5
+
6
+ convert ->(item, data) {
7
+ data[:bar] = item[:foo]
8
+ data
9
+ }
10
+ end
11
+
12
+ class TestProcessor
13
+ include DataSmash::Processor
14
+
15
+ process_with MyConvertor
16
+
17
+ smash_in -> {
18
+ [{foo: 'ding'}, {foo: 'dong'}].each {|d| process d }
19
+ }
20
+
21
+ smash_out ->(data) {
22
+ TestProcessor.PROCESSED_DATA ||= []
23
+ TestProcessor.PROCESSED_DATA << data
24
+ }
25
+
26
+ class << self
27
+ attr_accessor :PROCESSED_DATA
28
+ end
29
+ end
30
+
31
+ describe DataSmash::Processor do
32
+ describe '#process!' do
33
+ it 'should process each of the things yielded by the in method' do
34
+ TestProcessor.process!
35
+ TestProcessor.PROCESSED_DATA.should =~ [{bar: 'ding'}, {bar: 'dong'}]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ require 'data_smash'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_smash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Asa Calow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-07 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: '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
+ description: An elegant picoframework for working with data
56
+ email:
57
+ - asacalow@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - data_smash.gemspec
68
+ - lib/data_smash.rb
69
+ - lib/data_smash/convertor.rb
70
+ - lib/data_smash/processor.rb
71
+ - lib/data_smash/version.rb
72
+ - spec/lib/convertor_spec.rb
73
+ - spec/lib/processor_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.1.11
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: ''
99
+ test_files:
100
+ - spec/lib/convertor_spec.rb
101
+ - spec/lib/processor_spec.rb
102
+ - spec/spec_helper.rb