transproc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47014d88c8dfcb44ab8eb1fd2b3ca43824e9ce12
4
+ data.tar.gz: 22c7d90980ecb0c92c978d38661d948929babaa8
5
+ SHA512:
6
+ metadata.gz: 9f80c2970dfd31a3e8d275855a5b927b32d85725f2339f9839ae8946d839a775629d11d5cb9e99bfae6b7051b0dd78dedfbd81de7641a79555bf73462896961b
7
+ data.tar.gz: 32f0e59dca2b63c9224112d07efab3ff24d26da84a01bf624553b862df0c09350ac50e88d2ac5cc571d555d367d036524df2e11b5128de4764dba5ca84bad667
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transproc.gemspec
4
+ gemspec
@@ -0,0 +1,16 @@
1
+ guard :rspec, cmd: "rspec" do
2
+ # run all specs if configuration is modified
3
+ watch('Guardfile') { 'spec' }
4
+ watch('Gemfile.lock') { 'spec' }
5
+ watch('spec/spec_helper.rb') { 'spec' }
6
+
7
+ # run all specs if supporting files files are modified
8
+ watch(%r{\Aspec/(?:lib|support|shared)/.+\.rb\z}) { 'spec' }
9
+
10
+ watch(%r{\Alib/(.+)\.rb\z}) { |_m| 'spec' }
11
+
12
+ # run a spec if it is modified
13
+ watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z})
14
+
15
+ notification :tmux, display_message: true
16
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Piotr Solnica
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.
@@ -0,0 +1,42 @@
1
+ # Transproc
2
+
3
+ Experimental functional transformations for Ruby.
4
+
5
+ ![I have no idea what I'm doing](http://thumbpress.com/wp-content/uploads/2013/05/I-Have-No-Idea-What-Im-Doing-1.jpg)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'transproc'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install transproc
22
+
23
+ ## Usage
24
+
25
+ ``` ruby
26
+ require 'transproc/hash'
27
+
28
+ # compose transformation functions
29
+ transformation = Transproc(:symbolize_keys) + Transproc(:map, user_name: :name))
30
+
31
+ # call the function
32
+ transformation['user_name' => 'Jane']
33
+ # => {:name=>"Jane"}
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/[my-github-username]/transproc/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,42 @@
1
+ require "transproc/version"
2
+
3
+ module Transproc
4
+ def self.register(*args, &block)
5
+ name, fn = *args
6
+ functions[name] = fn || block
7
+ end
8
+
9
+ def self.functions
10
+ @_functions ||= {}
11
+ end
12
+
13
+ def self.[](name)
14
+ functions.fetch(name)
15
+ end
16
+
17
+ class Function
18
+ attr_reader :fn, :args
19
+
20
+ def initialize(fn, args = [])
21
+ @fn = fn
22
+ @args = args
23
+ end
24
+
25
+ def call(value)
26
+ fn[value, *args]
27
+ end
28
+ alias_method :[], :call
29
+
30
+ def compose(other)
31
+ self.class.new(-> value { other[fn[value]] })
32
+ end
33
+ alias_method :+, :compose
34
+ end
35
+ end
36
+
37
+ def Transproc(fn, *args)
38
+ case fn
39
+ when Proc then Transproc::Function.new(fn, args)
40
+ when Symbol then Transproc::Function.new(Transproc[fn], args)
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ require 'date'
2
+
3
+ module Transproc
4
+ register(:to_string) do |value|
5
+ value.to_s
6
+ end
7
+
8
+ register(:to_integer) do |value|
9
+ value.to_i
10
+ end
11
+
12
+ register(:to_float) do |value|
13
+ value.to_f
14
+ end
15
+
16
+ register(:to_date) do |value|
17
+ Date.parse(value)
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Transproc
2
+ register(:symbolize_keys) do |hash|
3
+ Hash[hash.map { |k, v| [k.to_sym, v] }]
4
+ end
5
+
6
+ register(:map) do |hash, mapping|
7
+ Hash[hash.map { |k, v| [mapping[k], v] }]
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Transproc
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ require 'transproc/coercions'
4
+
5
+ describe 'Transproc / Coercions' do
6
+ describe 'to_string' do
7
+ it 'turns integer into a string' do
8
+ expect(Transproc(:to_string)[1]).to eql('1')
9
+ end
10
+ end
11
+
12
+ describe 'to_integer' do
13
+ it 'turns string into an integer' do
14
+ expect(Transproc(:to_integer)['1']).to eql(1)
15
+ end
16
+ end
17
+
18
+ describe 'to_float' do
19
+ it 'turns string into a float' do
20
+ expect(Transproc(:to_float)['1']).to eql(1.0)
21
+ end
22
+
23
+ it 'turns integer into a float' do
24
+ expect(Transproc(:to_float)[1]).to eql(1.0)
25
+ end
26
+ end
27
+
28
+ describe 'to_date' do
29
+ it 'turns string into a date' do
30
+ date = Date.new(1983, 11, 18)
31
+ expect(Transproc(:to_date)['18th, November 1983']).to eql(date)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ require 'transproc/hash'
4
+
5
+ describe 'Hash mapping with Transproc' do
6
+ describe 'symbolize_keys' do
7
+ it 'returns a new hash with symbolized keys' do
8
+ symbolize_keys = Transproc(:symbolize_keys)
9
+
10
+ input = { 'foo' => 'bar' }
11
+ output = { foo: 'bar' }
12
+
13
+ expect(symbolize_keys[input]).to eql(output)
14
+ end
15
+ end
16
+
17
+ describe 'map' do
18
+ it 'returns a new hash with applied functions' do
19
+ map = Transproc(:map, 'foo' => :foo)
20
+
21
+ input = { 'foo' => 'bar' }
22
+ output = { foo: 'bar' }
23
+
24
+ expect(map[input]).to eql(output)
25
+ end
26
+ end
27
+
28
+ describe 'combining transformations' do
29
+ it 'applies functions to the hash' do
30
+ symbolize_keys = Transproc(:symbolize_keys)
31
+ map = Transproc(:map, user_name: :name, user_email: :email)
32
+
33
+ transformation = symbolize_keys + map
34
+
35
+ input = { 'user_name' => 'Jade', 'user_email' => 'jade@doe.org' }
36
+ output = { name: 'Jade', email: 'jade@doe.org' }
37
+
38
+ result = transformation[input]
39
+
40
+ expect(result).to eql(output)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Transproc do
4
+ describe 'composition' do
5
+ it 'allows composing two transformation functions' do
6
+ input = '1'
7
+ output = 1.0
8
+
9
+ to_i = Transproc(-> value { value.to_i })
10
+ to_f = Transproc(-> value { value.to_f })
11
+
12
+ result = to_i + to_f
13
+
14
+ expect(result[input]).to eql(output)
15
+ end
16
+ end
17
+
18
+ describe 'function registration' do
19
+ it 'allows registering functions by name' do
20
+ Transproc.register(:to_boolean, -> value { value == 'true' })
21
+
22
+ result = Transproc(-> value { value.to_s }) + Transproc(:to_boolean)
23
+
24
+ expect(result[:true]).to be(true)
25
+ expect(result[:false]).to be(false)
26
+ end
27
+
28
+ it 'allows registering function by passing a block' do
29
+ Transproc.register(:to_boolean) { |value| value == 'true' }
30
+
31
+ result = Transproc(-> value { value.to_s }) + Transproc(:to_boolean)
32
+
33
+ expect(result[:true]).to be(true)
34
+ expect(result[:false]).to be(false)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,92 @@
1
+ require 'transproc'
2
+
3
+ #
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ =begin
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
55
+ # For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
@@ -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 'transproc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transproc"
8
+ spec.version = Transproc::VERSION
9
+ spec.authors = ["Piotr Solnica"]
10
+ spec.email = ["piotr.solnica@gmail.com"]
11
+ spec.summary = %q{Experimental functional transformations for Ruby}
12
+ spec.description = spec.summary
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", "~> 3.1"
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transproc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Solnica
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-24 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: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Experimental functional transformations for Ruby
56
+ email:
57
+ - piotr.solnica@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - Guardfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/transproc.rb
70
+ - lib/transproc/coercions.rb
71
+ - lib/transproc/hash.rb
72
+ - lib/transproc/version.rb
73
+ - spec/integration/coercions_spec.rb
74
+ - spec/integration/hash_mapping_spec.rb
75
+ - spec/integration/transproc_spec.rb
76
+ - spec/spec_helper.rb
77
+ - transproc.gemspec
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Experimental functional transformations for Ruby
102
+ test_files:
103
+ - spec/integration/coercions_spec.rb
104
+ - spec/integration/hash_mapping_spec.rb
105
+ - spec/integration/transproc_spec.rb
106
+ - spec/spec_helper.rb
107
+ has_rdoc: