carbide 0.1.0

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: 06ce8acac40e8afc9f310a497e8da1a952f5f140
4
+ data.tar.gz: 272731eabfb8da0855ec1d497cf1681a3f3629e8
5
+ SHA512:
6
+ metadata.gz: b333f8663c0f0a19d24e9211265c607e52996543ade638da5ec9750cf35d8edb4f31f38f5b69d5b4c1105f5ab6652c740420c7932482f6a9c0fd6754f8940b58
7
+ data.tar.gz: 0947861a7de646a2bd8495ef01545648307645a797ebc9d768c6a85edd716a2e9605917c37773ac97af1623b3902dfd72fbeab6571dbf94e2d7b55ab9b66935d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ - 2.1.7
5
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carbide.gemspec
4
+ gemspec
5
+
6
+ gem "guard"
7
+ gem "guard-minitest"
@@ -0,0 +1,5 @@
1
+ guard :minitest, all_on_start: false do
2
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
@@ -0,0 +1,167 @@
1
+ # Carbide
2
+
3
+ [![Build Status](https://travis-ci.org/a2ikm/carbide.svg)](https://travis-ci.org/a2ikm/carbide)
4
+
5
+ Carbide is a runtime framework like Rake.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'carbide'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install carbide
22
+
23
+ ## Usage
24
+
25
+ ### Simple
26
+
27
+ ```ruby
28
+ class SomeService
29
+ include Carbide
30
+ carbide
31
+
32
+ def initialize
33
+ task :hello do
34
+ puts "Hello"
35
+ end
36
+ task :world do
37
+ puts "World"
38
+ end
39
+ task :hello_world do
40
+ invoke :hello
41
+ invoke :world
42
+ end
43
+ end
44
+
45
+ def hello_world
46
+ invoke :hello_world
47
+ end
48
+
49
+ private
50
+
51
+ def carbide_manager
52
+ @carbide_manager ||= Carbide::Manager.new
53
+ end
54
+ end
55
+
56
+ SomeService.new.hello_world
57
+ #=> Hello
58
+ World
59
+ ```
60
+
61
+ ### Before and After Hooks
62
+
63
+ ```ruby
64
+ task :core do
65
+ puts "Processing core task"
66
+ end
67
+
68
+ before :core, :setup do
69
+ puts "Processing setup"
70
+ end
71
+
72
+ after :core, :teardown do
73
+ puts "Processing teardown"
74
+ end
75
+
76
+ invoke :core
77
+ #=> Processing setup
78
+ Processing core task
79
+ Processing teardown
80
+ ```
81
+
82
+ ### Breaking execution
83
+
84
+ You can break task execution by throwing `:berak`, not `return`.
85
+
86
+ ```ruby
87
+ task :some_task do
88
+ throw :break
89
+ puts "Hello" # not printed
90
+ end
91
+ ```
92
+
93
+ You can define multi blocks into one task, but `:break` breaks just the block
94
+ from which it was thrown. Other blocks don't break.
95
+
96
+ ```ruby
97
+ task :some_task do
98
+ throw :break
99
+ puts "Hello" # not printed
100
+ end
101
+
102
+ task :some_task do
103
+ puts "Bye" # printed
104
+ end
105
+ ```
106
+
107
+ ### Options
108
+
109
+ #### :manager option
110
+
111
+ You can specify `Carbide::Manager`'s name with `:manager` option like:
112
+
113
+ ```ruby
114
+ class SomeClass
115
+ include Carbide
116
+ carbide manager: :fantastic_manager
117
+
118
+ def fantastic_manager
119
+ @manager ||= Carbide::Manager
120
+ end
121
+ end
122
+ ```
123
+
124
+ You can use an instance variable like:
125
+
126
+ ```ruby
127
+ class SomeClass
128
+ include Carbide
129
+ carbide manager: :@carbide_manager
130
+
131
+ def initialize
132
+ @carbide_manager = Carbide::Manager.new
133
+ end
134
+ end
135
+ ```
136
+
137
+ #### :prefix option
138
+
139
+ You can specify DSL methods' prefix with `:prefix` option like:
140
+
141
+ ```ruby
142
+ class SomeClass
143
+ include Carbide
144
+ carbide prefix: :foo
145
+
146
+ def define_tasks
147
+ foo_task :my_task1
148
+ foo_before :my_task1, :my_task2
149
+ foo_after :my_task1, :my_task3
150
+ end
151
+
152
+ def carbide_manager
153
+ @carbide_manager ||= Carbide::Manager.new
154
+ end
155
+ end
156
+ ```
157
+
158
+ ## Development
159
+
160
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
161
+
162
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
163
+
164
+ ## Contributing
165
+
166
+ Bug reports and pull requests are welcome on GitHub at https://github.com/a2ikm/carbide.
167
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "carbide"
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
@@ -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,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carbide/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carbide"
8
+ spec.version = Carbide::VERSION
9
+ spec.authors = ["Masato Ikeda"]
10
+ spec.email = ["masato.ikeda@gmail.com"]
11
+
12
+ spec.summary = %q{Carbide is a runtime framework like Rake.}
13
+ spec.description = %q{Carbide is a runtime framework like Rake.}
14
+ spec.homepage = "https://github.com/a2ikm/carbide"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
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
@@ -0,0 +1,52 @@
1
+ require "carbide/version"
2
+ require "carbide/action"
3
+ require "carbide/manager"
4
+ require "carbide/task"
5
+ require "carbide/builder"
6
+
7
+ module Carbide
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ DEFAULT_OPTIONS = {
14
+ manager: :carbide_manager,
15
+ prefix: nil,
16
+ }.freeze
17
+
18
+ RUBY_RESERVED_WORDS = %w(
19
+ alias and BEGIN begin break case class def defined? do else elsif END end
20
+ ensure false for if in module next nil not or redo rescue retry return
21
+ self super then true undef unless until when while yield
22
+ ).freeze
23
+
24
+ def carbide(options = nil)
25
+ options = DEFAULT_OPTIONS.merge(options || {})
26
+
27
+ manager = options[:manager]
28
+ manager = "self.#{manager}" if RUBY_RESERVED_WORDS.include?(manager)
29
+
30
+ prefix = options[:prefix]
31
+ prefix = "#{prefix}_" if prefix && !prefix.to_s.end_with?("_")
32
+
33
+ class_eval <<-RUBY, __FILE__, __LINE__+1
34
+ def #{prefix}invoke(name, *args)
35
+ task = #{manager}[name]
36
+ if task
37
+ task.invoke(*args)
38
+ end
39
+ end
40
+ def #{prefix}task(name, &block)
41
+ Carbide::Builder.new(#{manager}).build_task(name, self, &block)
42
+ end
43
+ def #{prefix}before(name, pre_name, &block)
44
+ Carbide::Builder.new(#{manager}).build_pre_task(name, pre_name, self, &block)
45
+ end
46
+ def #{prefix}after(name, post_name, &block)
47
+ Carbide::Builder.new(#{manager}).build_post_task(name, post_name, self, &block)
48
+ end
49
+ RUBY
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,16 @@
1
+ module Carbide
2
+ class Action
3
+ attr_reader :context, :block
4
+
5
+ def initialize(context, &block)
6
+ @context = context
7
+ @block = block
8
+ end
9
+
10
+ def execute(*args)
11
+ catch(:break) do
12
+ context.instance_exec(*args, &block)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ module Carbide
2
+ class Builder
3
+ attr_reader :manager
4
+
5
+ def initialize(manager)
6
+ @manager = manager
7
+ end
8
+
9
+ def build_task(name, context, &block)
10
+ task = manager[name]
11
+ if task.nil?
12
+ task = Task.new(manager, name)
13
+ manager.register(task)
14
+ end
15
+
16
+ if block_given?
17
+ action = Action.new(context, &block)
18
+ task.enhance(action)
19
+ end
20
+
21
+ task
22
+ end
23
+
24
+ def build_pre_task(name, pre_name, context, &block)
25
+ pre_task = manager[pre_name]
26
+ if pre_task.nil?
27
+ pre_task = Task.new(manager, pre_name)
28
+ manager.register(pre_task)
29
+ end
30
+
31
+ if block_given?
32
+ action = Action.new(context, &block)
33
+ pre_task.enhance(action)
34
+ end
35
+
36
+ task = manager[name]
37
+ task.prepend(pre_task)
38
+ pre_task
39
+ end
40
+
41
+ def build_post_task(name, post_name, context, &block)
42
+ post_task = manager[post_name]
43
+ if post_task.nil?
44
+ post_task = Task.new(manager, post_name)
45
+ manager.register(post_task)
46
+ end
47
+
48
+ if block_given?
49
+ action = Action.new(context, &block)
50
+ post_task.enhance(action)
51
+ end
52
+
53
+ task = manager[name]
54
+ task.append(post_task)
55
+ post_task
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,21 @@
1
+ module Carbide
2
+ class Manager
3
+ attr_reader :tasks
4
+
5
+ def initialize
6
+ @tasks = {}
7
+ end
8
+
9
+ def [](name)
10
+ tasks[name.to_sym]
11
+ end
12
+
13
+ def []=(name, task)
14
+ tasks[name.to_sym] = task
15
+ end
16
+
17
+ def register(task)
18
+ self[task.name] = task
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,70 @@
1
+ module Carbide
2
+ class Task
3
+ attr_reader :manager, :name, :actions, :pre_tasks, :post_tasks
4
+
5
+ def initialize(manager, name)
6
+ @manager = manager
7
+ @name = name.to_sym
8
+ @actions = []
9
+ @pre_tasks = []
10
+ @post_tasks = []
11
+ end
12
+
13
+ alias to_sym name
14
+
15
+ def ==(other)
16
+ self.class === other &&
17
+ name == other.name
18
+ end
19
+
20
+ def enhance(action)
21
+ actions << action
22
+ self
23
+ end
24
+
25
+ def prepend(tasks)
26
+ @pre_tasks |= Array(tasks).map(&:name)
27
+ self
28
+ end
29
+
30
+ def append(tasks)
31
+ @post_tasks |= Array(tasks).map(&:name)
32
+ self
33
+ end
34
+
35
+ def execute(*args)
36
+ actions.each do |action|
37
+ action.execute(*args)
38
+ end
39
+ self
40
+ end
41
+
42
+ def invoke(*args)
43
+ invoke_pre_tasks(*args)
44
+ execute(*args)
45
+ invoke_post_tasks(*args)
46
+ self
47
+ end
48
+
49
+ def invoke_pre_tasks(*args)
50
+ pre_tasks.each do |pre_task|
51
+ task = manager[pre_task]
52
+ task.invoke(*args)
53
+ end
54
+ self
55
+ end
56
+
57
+ def invoke_post_tasks(*args)
58
+ post_tasks.each do |post_task|
59
+ task = manager[post_task]
60
+ task.invoke(*args)
61
+ end
62
+ self
63
+ end
64
+
65
+ def clear_actions
66
+ actions.clear
67
+ self
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Carbide
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carbide
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masato Ikeda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-01 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: Carbide is a runtime framework like Rake.
56
+ email:
57
+ - masato.ikeda@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - Guardfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - carbide.gemspec
71
+ - lib/carbide.rb
72
+ - lib/carbide/action.rb
73
+ - lib/carbide/builder.rb
74
+ - lib/carbide/manager.rb
75
+ - lib/carbide/task.rb
76
+ - lib/carbide/version.rb
77
+ homepage: https://github.com/a2ikm/carbide
78
+ licenses: []
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.4.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Carbide is a runtime framework like Rake.
100
+ test_files: []