matreska 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: 00944582ede00d17b12fed19e345b2518357927b
4
+ data.tar.gz: 508f11218ca44170c2a5ec1d64551a5b187facba
5
+ SHA512:
6
+ metadata.gz: ad166ed34fd0c9cab36932bd726083377659f0e06bbcae55ac0dd869979737bf1259b9cfcabc1b8c395d97db2f441ba14a6f9d9cf6fc65e1cfb7d8f3e9f1571b
7
+ data.tar.gz: c76a5b442782f86e6df8f811a258ea5f8a31134fe783b002cf8ff82db629ad7f8136b1e824553efc3d778dfa176e99d611540cb80fbb18b4de3f66339038519c
@@ -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,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in matreska.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kyoendo
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,35 @@
1
+ # Matreska
2
+
3
+ Matreska is for building adaptable multi-filter, which is inspired by Rack.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'matreska'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install matreska
18
+
19
+ ## Usage
20
+
21
+ mat = Matreska.build(1)
22
+ Matreska.doll(:Plus) { |base, arg| base + arg }
23
+ Matreska.doll(:Multi) { |base, arg| base * arg }
24
+ mat.set Plus, 3
25
+ mat.set Multi, 2
26
+
27
+ mat.call #=> 8
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( http://github.com/<my-github-username>/matreska/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'matreska'
@@ -0,0 +1,67 @@
1
+ require "matreska/version"
2
+
3
+ module Matreska
4
+ class Builder
5
+ attr_reader :dolls
6
+ def initialize(core)
7
+ @dolls = []
8
+ @core = core.respond_to?(:call) ? core : ->env{ core }
9
+ end
10
+
11
+ def set(doll, *args, &blk)
12
+ @dolls << ->core{ doll.new(core, *args, &blk) }
13
+ self
14
+ end
15
+
16
+ def call(env=nil)
17
+ build.call(env)
18
+ end
19
+ alias :[] :call
20
+
21
+ def build
22
+ @dolls.inject(@core) { |core, doll| doll.call(core) }
23
+ end
24
+ private :build
25
+
26
+ def clear
27
+ @figures.clear
28
+ end
29
+ end
30
+
31
+ class << self
32
+ def build(core)
33
+ Builder.new(core)
34
+ end
35
+
36
+ def doll(name, *meth, &blk)
37
+ if blk
38
+ build_doll(name, &blk)
39
+ else
40
+ build_doll(name) do |core|
41
+ core.send(*meth)
42
+ end
43
+ end
44
+ end
45
+ alias :figure :doll
46
+
47
+ private
48
+ def build_doll(name, &blk)
49
+ klass = Class.new do
50
+ def initialize(core, *args, &blk)
51
+ @core = core
52
+ @args = args
53
+ @blk = blk
54
+ end
55
+
56
+ define_method(:call) do |env|
57
+ core = @core.call(env)
58
+ blk.call(core, *@args, &@blk)
59
+ end
60
+ alias :[] :call
61
+ end
62
+ Object.const_set(name.to_s, klass)
63
+ end
64
+ end
65
+ end
66
+
67
+ M8a = Matreska
@@ -0,0 +1,3 @@
1
+ module Matreska
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'matreska/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "matreska"
8
+ spec.version = Matreska::VERSION
9
+ spec.authors = ["kyoendo"]
10
+ spec.email = ["postagie@gmail.com"]
11
+ spec.summary = %q{Matreska is for building adaptable multi-filter, which is inspired by Rack.}
12
+ spec.homepage = "https://github.com/melborne/matreska"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ class PlusOne
4
+ def initialize(core)
5
+ @core = core
6
+ end
7
+
8
+ def call(env)
9
+ res = @core.call(env)
10
+ res + 1
11
+ end
12
+ end
13
+
14
+ class Plus
15
+ def initialize(core, arg, &blk)
16
+ @core = core
17
+ @arg = arg
18
+ @blk = blk
19
+ end
20
+
21
+ def call(env)
22
+ res = @core.call(env)
23
+ if @blk
24
+ @blk.call(res, @arg)
25
+ else
26
+ res + @arg
27
+ end
28
+ end
29
+ end
30
+
31
+ describe Matreska do
32
+ it 'should have a version number' do
33
+ Matreska::VERSION.should_not be_nil
34
+ end
35
+ end
36
+
37
+ describe Matreska::Builder do
38
+ before do
39
+ @core = ->env{ env }
40
+ @ma = Matreska::Builder.new(@core)
41
+ end
42
+
43
+ describe "#new" do
44
+ it 'takes a core object which has call method' do
45
+ core = @ma.instance_variable_get("@core")
46
+ expect(core.respond_to? :call).to be true
47
+ end
48
+
49
+ context 'when passed object has not call method' do
50
+ it 'builds a proc object which wrap self' do
51
+ ma = Matreska::Builder.new(1)
52
+ expect(ma.call).to eq 1
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "#call" do
58
+ it 'execute call method of core object' do
59
+ expect(@ma.call(1)).to eq 1
60
+ end
61
+
62
+ it 'execute call of core and dolls' do
63
+ @ma.set PlusOne
64
+ expect(@ma.call(1)).to eq 2
65
+ end
66
+
67
+ it 'execute call of core and dolls with its arguments' do
68
+ @ma.set Plus, 2
69
+ expect(@ma.call(1)).to eq 3
70
+ end
71
+ end
72
+
73
+ describe "#set" do
74
+ it 'sets doll objects which takes a core or doll and has call method' do
75
+ @ma.set PlusOne
76
+ dolls = @ma.dolls
77
+ expect(dolls.size).to eq 1
78
+ expect(dolls.first).to respond_to(:call)
79
+ end
80
+
81
+ it 'can receive an argument' do
82
+ expect{ @ma.set Plus, 2 }.not_to raise_error
83
+ expect(@ma.call(1)).to eq 3
84
+ end
85
+
86
+ it 'can receive a block' do
87
+ expect{
88
+ @ma.set Plus, 3 do |core, arg|
89
+ core - arg
90
+ end
91
+ }.not_to raise_error
92
+ expect(@ma.call(5)).to eq 2
93
+ end
94
+ end
95
+
96
+ describe ".doll" do
97
+ context 'with a method' do
98
+ it 'creates doll class which execute the method' do
99
+ Matreska.doll(:Rev, :reverse)
100
+ @ma.set Rev
101
+ expect(@ma.call("olleh")).to eq "hello"
102
+ end
103
+
104
+ it 'creates doll class which execute the method with an argument' do
105
+ Matreska.doll(:MulByTwo, :*, 2)
106
+ @ma.set MulByTwo
107
+ expect(@ma.call(3)).to eq 6
108
+ end
109
+ end
110
+
111
+ context 'with a block' do
112
+ it 'creates a doll class which execute the block' do
113
+ Matreska.doll(:RevUp) { |core| core.reverse.upcase }
114
+ @ma.set RevUp
115
+ expect(@ma.call("olleh")).to eq "HELLO"
116
+ end
117
+
118
+ it 'creates a doll class with arguments' do
119
+ Matreska.doll(:Plus) { |core, operand, &blk| core + operand }
120
+ @ma.set Plus, 4
121
+ expect(@ma.call(1)).to eq 5
122
+ end
123
+ end
124
+ end
125
+
126
+ describe ".build" do
127
+ it 'calls Builder.new' do
128
+ expect(Matreska.build(->env{ env })).to be_instance_of Matreska::Builder
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'matreska'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matreska
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kyoendo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-14 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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:
56
+ email:
57
+ - postagie@gmail.com
58
+ executables:
59
+ - matreska
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/matreska
71
+ - lib/matreska.rb
72
+ - lib/matreska/version.rb
73
+ - matreska.gemspec
74
+ - spec/matreska_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/melborne/matreska
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.0
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Matreska is for building adaptable multi-filter, which is inspired by Rack.
100
+ test_files:
101
+ - spec/matreska_spec.rb
102
+ - spec/spec_helper.rb
103
+ has_rdoc: