minicontext 0.1.0

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: 3921cd064245ac91d6458de058aae081017e91d0
4
+ data.tar.gz: 072368db78a1b5e9907feec2750fbd6d8620528f
5
+ SHA512:
6
+ metadata.gz: dc9af6210e1cb9d3ef79387b8aae07fd8a8a55db807fe401b66d8934a9a44db495b678275483177fad44a730eb02745830cd0949b759777a20604ba604a8734d
7
+ data.tar.gz: 0b61e2812b9fffe72ed7a9450288b15fd7bfa77c7fa63d54f2cf2f8ab52ca2d9a27ae76111e27a4d46247a2d9e78fca9a96517340e266ecb5e2a0d21cd0dd1f8
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ *.swp
3
+ .ruby-version
4
+ .bundle
5
+ .DS_Store
6
+ Gemfile.lock
7
+ coverage
8
+ pkg
9
+ vendor/bundle
10
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in minicontext.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 daisuko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Minicontext
2
+
3
+ Mimicontext is a DSL kit.
4
+
5
+ ## Installation
6
+
7
+ ` Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'minicontext'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install minicontext
20
+
21
+ ## License
22
+
23
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
24
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "spec"
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "minicontext"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,86 @@
1
+ module Minicontext
2
+ class Context
3
+ KEYWORD = %i(before after)
4
+ COMPOSE = -> (f, g) { -> (env) { f.(g.(env)) } }
5
+ RELAY = -> (env) { env }
6
+
7
+ def initialize(parent = nil)
8
+ @vars = {}
9
+ @parent = parent
10
+ end
11
+
12
+ def context(name, &block)
13
+ raise AssignError, name if KEYWORD.include?(name)
14
+ raise AssignError, name if (val = @vars[name]) && Proc === val
15
+
16
+ val ? val.action(&block) : define_context(name, &block)
17
+ end
18
+
19
+ def task(name, &block)
20
+ self[name] = -> (env) { env.(self, &block) }
21
+ end
22
+
23
+ def let(name, &block)
24
+ raise AssignError, name if KEYWORD.include?(name)
25
+
26
+ self[name] = -> (env) do
27
+ env.memoized.fetch(name) do
28
+ env.memoized[name] = env.(self, &block)
29
+ end
30
+ end
31
+ end
32
+
33
+ %i(before after).each do |name|
34
+ define_method(name) {|&block| task name, &block}
35
+ end
36
+
37
+ def action(&block)
38
+ instance_eval &block
39
+ end
40
+
41
+ def load(filename)
42
+ instance_eval open(filename) {|f| f.read}, filename
43
+ end
44
+
45
+ def call(name)
46
+ Proc === (val = @vars[name]) ? apply(name) : val
47
+ end
48
+
49
+ def lookup(name)
50
+ Proc === (val = @vars[name]) ? val : (@parent && @parent.lookup(name))
51
+ end
52
+
53
+ def [](name)
54
+ router[name]
55
+ end
56
+
57
+ def []=(name, val)
58
+ raise AssignError, name if @vars[name]
59
+
60
+ @vars[name] = val
61
+ end
62
+
63
+ private
64
+
65
+ def define_context(name, &block)
66
+ self.class.new(self).tap do |ctx|
67
+ self[name] = ctx
68
+ ctx.action &block
69
+ end
70
+ end
71
+
72
+ def apply(name)
73
+ f = compose(@vars[name])
74
+
75
+ @parent ? @parent.send(:compose, f) : f
76
+ end
77
+
78
+ def compose(f)
79
+ COMPOSE.(@vars[:after] || RELAY, COMPOSE.(f, @vars[:before] || RELAY))
80
+ end
81
+
82
+ def router
83
+ Router::Maybe.new(self)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,46 @@
1
+ module Minicontext
2
+ class Environment
3
+ attr_reader :request, :response, :memoized
4
+
5
+ def initialize(request, response = nil)
6
+ @request = request
7
+ @response = response
8
+ @memoized = {}
9
+ end
10
+
11
+ def bind(val)
12
+ dup.tap { |env| env.response = val }
13
+ end
14
+
15
+ def response=(val)
16
+ raise AssignError, 'response' if response
17
+
18
+ @response = val
19
+ end
20
+
21
+ def halt(*args)
22
+ Halt.new(*args)
23
+ end
24
+
25
+ def fetch(name)
26
+ (f = @context.lookup(name)) && f.(self).response
27
+ end
28
+
29
+ def action(&block)
30
+ instance_eval(&block)
31
+ end
32
+
33
+ def call(context, &block)
34
+ @context = context
35
+
36
+ bind(Halt === @response ? @response : action(&block))
37
+ end
38
+
39
+ private
40
+
41
+ def initialize_copy(obj)
42
+ @context = nil
43
+ @response = nil
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ module Minicontext
2
+ class Halt
3
+ def initialize(*args)
4
+ @args = args
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ module Minicontext
2
+ class Router
3
+ class Maybe
4
+ def initialize(val)
5
+ @val = val
6
+ end
7
+
8
+ def value
9
+ @val
10
+ end
11
+
12
+ def [](name)
13
+ klass = case val = @val.(name)
14
+ when Context then Maybe
15
+ when Proc then Just
16
+ else Noting
17
+ end
18
+
19
+ klass.new(val)
20
+ end
21
+ end
22
+
23
+ class Just < Maybe
24
+ def [](name)
25
+ Noting.new
26
+ end
27
+ end
28
+
29
+ class Noting < Maybe
30
+ def initialize(val = nil)
31
+ @val = val
32
+ end
33
+
34
+ def [](name)
35
+ self
36
+ end
37
+
38
+ def value
39
+ nil
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Minicontext
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "minicontext/version"
2
+
3
+ module Minicontext
4
+ class AssignError < StandardError; end
5
+ end
6
+
7
+ require "minicontext/context"
8
+ require "minicontext/environment"
9
+ require "minicontext/router"
10
+ require "minicontext/halt"
@@ -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 'minicontext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "minicontext"
8
+ spec.version = Minicontext::VERSION
9
+ spec.authors = ["daisuko"]
10
+ spec.email = ["striker.daisuko@gmail.com"]
11
+
12
+ spec.summary = %q{Minicontext is a DSL Kit}
13
+ spec.homepage = "https://github.com/daisuko/minicontext"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest"
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minicontext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - daisuko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: minitest
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
+ - striker.daisuko@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/minicontext.rb
72
+ - lib/minicontext/context.rb
73
+ - lib/minicontext/environment.rb
74
+ - lib/minicontext/halt.rb
75
+ - lib/minicontext/router.rb
76
+ - lib/minicontext/version.rb
77
+ - minicontext.gemspec
78
+ homepage: https://github.com/daisuko/minicontext
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.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Minicontext is a DSL Kit
102
+ test_files: []