acts_as_shellscript_executable 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 722aca7c7a8f0e82959146778aca5747f639e3f5
4
- data.tar.gz: 7d7676df80465c0edb8f14a4c09a301612aebafb
3
+ metadata.gz: 190be56029b641566b7aad1e32156a01f482df48
4
+ data.tar.gz: a81c819d7d98661b72fde5ba7b39963506eabd5d
5
5
  SHA512:
6
- metadata.gz: 4d809017d6ff92571c6541f34124520e43013586f31ccfbb30b5fbe33e1486a1860fc054f6f7410dfe548637436da1e44dd7384e4d3e4a5ec63ba7a329649a8c
7
- data.tar.gz: bd773b89ce5a3dd93e347d089f83a80c00b4b51d5e28b70f9a01b59bb88413263923840f4770f8bd1676180f7bc1e18b308710631e9c3d25fcae0749ba4b9360
6
+ metadata.gz: 8acbe54b17e19cabb50f8aacac224bbc66cc69e70b428d090f8db334bcc3a7a8a154180a6bf480b30ee4174f557ee96ecc22891e44d14bb2cf603ced3373bd45
7
+ data.tar.gz: 5b0554b894a5467f6a9a3c038b61e9c7dc4a07395d61c538a097786c0486a733b7ff4405c83899990c32a854e7f9c39ddbdd3c357f847e0e87d15d5afe0fd2ef
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --format documentation
3
+ --order rand
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Documentation:
2
+ Enabled: false
3
+ AllCops:
4
+ Exclude:
5
+ - acts_as_shellscript_executable.gemspec
data/.travis.yml CHANGED
@@ -1,7 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.3
3
4
  - 2.1.2
4
5
  - 2.1.0
5
6
  - 2.0.0
6
7
  - 1.9.3
7
8
  - 'ruby-head'
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'coveralls', require: false
7
+ gem 'rubocop', require: false
data/README.md CHANGED
@@ -1,31 +1,55 @@
1
1
  # ActsAsShellscriptExecutable
2
2
 
3
- [![travis-batch](https://travis-ci.org/hoshinotsuyoshi/acts_as_shellscript_executable.svg)](https://travis-ci.org/hoshinotsuyoshi/acts_as_shellscript_executable)
3
+ [![travis-batch](https://travis-ci.org/hoshinotsuyoshi/acts_as_shellscript_executable.svg?branch=master)](https://travis-ci.org/hoshinotsuyoshi/acts_as_shellscript_executable)
4
4
  [![Coverage Status](https://coveralls.io/repos/hoshinotsuyoshi/acts_as_shellscript_executable/badge.png)](https://coveralls.io/r/hoshinotsuyoshi/acts_as_shellscript_executable)
5
5
  [![Code Climate](https://codeclimate.com/github/hoshinotsuyoshi/acts_as_shellscript_executable/badges/gpa.svg)](https://codeclimate.com/github/hoshinotsuyoshi/acts_as_shellscript_executable)
6
6
 
7
7
 
8
- class Script < ActiveRecord::Base
9
- acts_as_shellscript_executable script: :script, stdout: :result
10
- end
11
-
12
8
  ### before:
13
9
 
14
- | id | name | script | result |
15
- | :------|:------ |:---------------|:-----|
16
- | 1 | foo | echo 'lalala' | |
17
-
10
+ | id | name | script | result |
11
+ | :---|:----- |:--------------|:-------|
12
+ | 1 | foo | echo 'lalala' | |
18
13
 
19
14
  ### execute:
20
15
 
21
- script = Script.find(1)
22
- script.execute!
16
+ ```ruby
17
+ class Script < ActiveRecord::Base
18
+ acts_as_shellscript_executable script: :script
19
+ end
20
+ ```
21
+
22
+ ```ruby
23
+ script = Script.find(1)
24
+ script.result = script.execute!
25
+ script.save!
26
+ ```
23
27
 
24
28
  ### after:
25
-
26
- | id | name | script | result |
27
- | :------|:------ |:---------------|:-----|
28
- | 1 | foo | echo 'lalala' | lalala |
29
+
30
+ | id | name | script | result |
31
+ | :---|:----- |:--------------|:-------|
32
+ | 1 | foo | echo 'lalala' | lalala |
33
+
34
+ ## #execute!
35
+
36
+ * `#execute!(nil)`
37
+ * returns scripts stdout of whole of the shellscript
38
+
39
+ * `#execute!(block)`
40
+ * returns `nil`, yields the shellscript's stdout st each line(splited by `\n`)
41
+
42
+ ## Options of `.acts_as_shellscript_executable`
43
+
44
+ * `script:` (default: `:script`)
45
+ * if `Symbol`, the same name column's value will be evaluated as shellscript
46
+ * if `String`, the string will be evaluated as shellscript
47
+
48
+ * `parallel:` (default: `false`)
49
+ * if true, the script will run in parallel by using `thread`
50
+
51
+ * `method:` (default: `execute!`)
52
+ * the execute method's name
29
53
 
30
54
  ## Installation
31
55
 
@@ -43,7 +67,7 @@ Or install it yourself as:
43
67
 
44
68
  ## Contributing
45
69
 
46
- 1. Fork it ( http://github.com/<my-github-username>/acts_as_shellscript_executable/fork )
70
+ 1. Fork it ( http://github.com/hoshinotsuyoshi/acts_as_shellscript_executable/fork )
47
71
  2. Create your feature branch (`git checkout -b my-new-feature`)
48
72
  3. Commit your changes (`git commit -am 'Add some feature'`)
49
73
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,5 +1,6 @@
1
1
  require 'acts_as_shellscript_executable/version'
2
- require 'acts_as_shellscript_executable/active_record/acts/shellscript_executable'
2
+ require 'acts_as_shellscript_executable/active_record/' \
3
+ 'acts/shellscript_executable'
3
4
 
4
5
  module ActsAsShellscriptExecutable
5
6
  if defined? Rails::Railtie
@@ -17,7 +18,8 @@ module ActsAsShellscriptExecutable
17
18
  def self.insert
18
19
  if defined?(ActiveRecord)
19
20
  require 'active_record'
20
- ActiveRecord::Base.send(:include, ActiveRecord::Acts::ShellscriptExecutable)
21
+ ActiveRecord::Base.send \
22
+ :include, ActiveRecord::Acts::ShellscriptExecutable
21
23
  end
22
24
  end
23
25
  end
@@ -1,3 +1,5 @@
1
+ require 'thread'
2
+
1
3
  module ActiveRecord
2
4
  module Acts
3
5
  module ShellscriptExecutable
@@ -7,44 +9,44 @@ module ActiveRecord
7
9
 
8
10
  module ClassMethods
9
11
  def acts_as_shellscript_executable(options = {})
10
- @@configuration = { script: :script, stdout: nil }
11
- @@configuration.update(options) if options.is_a?(Hash)
12
+ configuration = { method: :execute!, script: :script }
13
+ configuration.update(options) if options.is_a?(Hash)
12
14
 
13
15
  class_eval <<-EOV
14
- include ::ActiveRecord::Acts::ShellscriptExecutable::InstanceMethods
15
-
16
+ def #{configuration[:method]}(&block)
17
+ script = @@__configuration__[:script]
18
+ answer = ''
19
+ if @@__configuration__[:parallel]
20
+ Thread.new do
21
+ __execute__(script, answer, block)
22
+ end
23
+ block_given? ? nil : answer
24
+ else
25
+ __execute__(script, answer, block)
26
+ block_given? ? nil : answer
27
+ end
28
+ end
16
29
  EOV
17
- end
18
30
 
19
- def configuration
20
- @@configuration
31
+ class_variable_set(:@@__configuration__, configuration)
32
+ include ::ActiveRecord::Acts::ShellscriptExecutable::InstanceMethods
21
33
  end
22
34
  end
23
35
 
24
36
  module InstanceMethods
25
- def execute!
26
- script = configuration[:script]
27
- stdout = configuration[:stdout]
28
- if configuration[:fork]
29
- fork { execute(script, stdout) }
30
- # for test
31
- Process.wait if ENV['test']
32
- else
33
- execute(script, stdout)
34
- end
35
- end
36
-
37
37
  private
38
- def configuration
39
- self.class.configuration
40
- end
41
38
 
42
- def execute(script, stdout)
43
- retval = %x(#{send script})
44
- if stdout && respond_to?("#{stdout}=".to_sym)
45
- send("#{stdout}=".to_sym, retval)
39
+ def __execute__(script, answer, block = nil)
40
+ script = send script if script.is_a? Symbol
41
+ retval = []
42
+ script.split("\n").each do |line|
43
+ if block
44
+ block.call `#{line}`
45
+ else
46
+ retval << `#{line}`
47
+ end
46
48
  end
47
- save!
49
+ answer.replace retval.join
48
50
  end
49
51
  end
50
52
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Acts
3
3
  module ShellscriptExecutable
4
- VERSION = '0.0.0'
4
+ VERSION = '0.0.1'
5
5
  end
6
6
  end
7
7
  end
@@ -1,14 +1,16 @@
1
1
  require 'spec_helper'
2
- require 'tmpdir'
3
2
 
4
3
  describe ActiveRecord::Base do
5
4
  describe '.acts_as_shellscript_executable' do
6
5
  def db_setup!
7
- # use not-in-memory-sqlite-db. because of fork
8
- db_dir = Dir.mktmpdir('db')
9
- ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "#{db_dir}/db.sqlite3")
6
+ ActiveRecord::Base.establish_connection \
7
+ adapter: 'sqlite3', database: ':memory:'
10
8
  ActiveRecord::Schema.verbose = false
11
9
  ActiveRecord::Base.connection.schema_cache.clear!
10
+ db_schema_define!
11
+ end
12
+
13
+ def db_schema_define!
12
14
  ActiveRecord::Schema.define(version: 1) do
13
15
  create_table :scripts do |t|
14
16
  t.column :script, :string
@@ -32,67 +34,48 @@ describe ActiveRecord::Base do
32
34
  it do
33
35
  script = Script.create
34
36
  script.script = 'echo "lalala"'
35
- expect{script.execute!}.to_not \
36
- change{script.result}
37
+ expect(script.execute!).to eq "lalala\n"
37
38
  end
38
39
  end
39
40
 
40
- context 'given option {script: :script, stdout: :result}' do
41
- before do
42
- class Script < ActiveRecord::Base
43
- acts_as_shellscript_executable script: :script, stdout: :result
44
- end
45
- end
46
-
47
- it do
48
- script = Script.create
49
- script.script = 'echo "lalala"'
50
- expect{script.execute!}.to \
51
- change{script.result}.from(nil).to("lalala\n")
52
- end
53
- end
54
-
55
- describe 'given option fork' do
56
- context 'given option {script: :script, stdout: :result, fork: false}' do
41
+ describe 'given option parallel' do
42
+ context 'given option {script: :script, parallel: false}' do
57
43
  before do
58
44
  class Script < ActiveRecord::Base
59
- acts_as_shellscript_executable script: :script, stdout: :result, fork: false
45
+ acts_as_shellscript_executable script: :script, parallel: false
60
46
  end
61
47
  end
62
48
 
63
49
  it do
64
50
  script = Script.create
65
- pid = Process.pid
66
- script.script = 'echo $PPID' # this $PPID is equal to $$
67
- expect{script.execute!}.to \
68
- change{script.result}.from(nil).to("#{Process.pid}\n")
51
+ script.script = 'echo $PPID'
52
+ expect(script.execute!).to eq "#{Process.pid}\n"
69
53
  end
70
54
  end
71
55
 
72
- context '[forking] given option {script: :script, stdout: :result, fork: true}' do
56
+ context 'given option {script: :script, parallel: true}' do
73
57
  before do
74
58
  class Script < ActiveRecord::Base
75
- acts_as_shellscript_executable script: :script, stdout: :result, fork: true
59
+ acts_as_shellscript_executable script: :script, parallel: true
76
60
  end
77
-
78
- @script = Script.create
79
- @script.script = 'echo $PPID' # when fork, this $PPID is not equal to $$
80
- @script.execute!
81
61
  end
82
62
 
83
63
  it do
84
- # reset object because of fork
85
- @script = Script.find @script.id
86
- expect(@script.result).not_to equal nil
87
- expect(@script.result).not_to eq("#{Process.pid}\n")
64
+ script = Script.create
65
+ script.script = "sleep 1\necho $PPID"
66
+ result = script.execute!
67
+
68
+ expect(result).to eq('')
69
+ sleep 1.5
70
+ expect(result).to eq("#{Process.pid}\n")
88
71
  end
89
72
  end
90
73
  end
91
74
 
92
- context 'given option {script: :script2, stdout: :result}' do
75
+ context 'given option { script: :script2 }' do
93
76
  before do
94
77
  class Script < ActiveRecord::Base
95
- acts_as_shellscript_executable script: :script2, stdout: :result
78
+ acts_as_shellscript_executable script: :script2
96
79
  end
97
80
  end
98
81
 
@@ -100,8 +83,109 @@ describe ActiveRecord::Base do
100
83
  script = Script.create
101
84
  script.script = 'echo "hehehe"'
102
85
  script.script2 = 'echo "lalala"'
103
- expect{script.execute!}.to \
104
- change{script.result}.from(nil).to("lalala\n")
86
+ expect(script.execute!).to eq "lalala\n"
87
+ end
88
+ end
89
+
90
+ context 'given option { script: "echo 1;" }' do
91
+ before do
92
+ class Script < ActiveRecord::Base
93
+ acts_as_shellscript_executable script: 'echo 1;'
94
+ end
95
+ end
96
+
97
+ it do
98
+ script = Script.create
99
+ script.script = 'echo "hehehe"'
100
+ expect(script.execute!).to eq "1\n"
101
+ end
102
+ end
103
+
104
+ context 'given option {script: "echo 1\necho 2"}' do
105
+ before do
106
+ class Script < ActiveRecord::Base
107
+ acts_as_shellscript_executable script: "echo 1\necho 2"
108
+ end
109
+ end
110
+
111
+ it do
112
+ script = Script.create
113
+ script.script = 'echo "hehehe"'
114
+ result = script.execute!
115
+ expect(result).to eq "1\n2\n"
116
+ end
117
+ end
118
+
119
+ context 'given option {script: "echo 1\nsleep 1\necho 2" parallel: true}' do
120
+ before do
121
+ class Script < ActiveRecord::Base
122
+ acts_as_shellscript_executable \
123
+ script: "echo 1\nsleep 1\necho 2", parallel: true
124
+ end
125
+ end
126
+
127
+ describe 'block given' do
128
+ it do
129
+ script = Script.create
130
+ watcher = []
131
+
132
+ retval = script.execute! do |each_line_result|
133
+ watcher << each_line_result
134
+ end
135
+
136
+ sleep 2
137
+
138
+ expect(retval).to be_nil
139
+ expect(watcher).to eq ["1\n", '', "2\n"]
140
+ end
141
+ end
142
+ end
143
+
144
+ context 'given option {script: "echo 1\necho 2"}' do
145
+ before do
146
+ class Script < ActiveRecord::Base
147
+ acts_as_shellscript_executable \
148
+ script: "echo 1\necho 2"
149
+ end
150
+ end
151
+
152
+ describe 'block given' do
153
+ it do
154
+ script = Script.create
155
+ watcher = []
156
+
157
+ retval = script.execute! do |each_line_result|
158
+ watcher << each_line_result
159
+ end
160
+
161
+ expect(retval).to be_nil
162
+ expect(watcher).to eq ["1\n", "2\n"]
163
+ end
164
+ end
165
+ end
166
+
167
+ context 'given option {method: :awesome!}' do
168
+ before do
169
+ class Script < ActiveRecord::Base
170
+ acts_as_shellscript_executable \
171
+ method: :awesome!, script: "echo 1\nsleep 1\necho 2", parallel: true
172
+ end
173
+ end
174
+
175
+ describe 'block given' do
176
+ it do
177
+ script = Script.create
178
+ watcher = []
179
+
180
+ retval = script.awesome! do |each_line_result|
181
+ watcher << each_line_result
182
+ end
183
+
184
+ sleep 2
185
+
186
+ expect(retval).to be_nil
187
+ expect(watcher).to eq ["1\n", '', "2\n"]
188
+ end
105
189
  end
106
190
  end
107
191
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_shellscript_executable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hoshinotsuyoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-21 00:00:00.000000000 Z
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,6 +103,7 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
105
  - ".rspec"
106
+ - ".rubocop.yml"
106
107
  - ".ruby-version"
107
108
  - ".travis.yml"
108
109
  - Gemfile