acts_as_shellscript_executable 0.0.3 → 0.0.4

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: 4b5b36e986f4ca4ff2c46a909bd2432362c377c8
4
- data.tar.gz: 14abb316f1683d19d0cf4258de69e0f3024fe50b
3
+ metadata.gz: 0b465054cb5119511d8b8738e3d78b542f07da1e
4
+ data.tar.gz: 77f65e6a73801397000f4dd4521780ffeb2fa4f7
5
5
  SHA512:
6
- metadata.gz: f28ccb2e65d36b4d8505e3b18d238fcb20e76848f85bb4d56392a2e686c0e0de2526472f021e97c42f2483c3c7f90873ce025561233e8a6645cfdf7b6ff93d55
7
- data.tar.gz: 6d1fd98c72e34accf3c3a974473fe0c86991c65cc830cd7e6c371cdb0577ee017d1ba3ff1340ea564aa500b2239c3ed5b068c1693c6a6ad2d4c5cdd0f0defd02
6
+ metadata.gz: 61f07132577ee040f24022ba2f5265fda7b6bb2fe6b80d1738d203fc5c2e6ff4896ed35c4932797e86349805b1ed4d905acc712ee8ef4f315012c062135d55ee
7
+ data.tar.gz: d8f373854a11636078f781304aac40f7d5beb1ae5ed5c18ce3fc9de79ef892d8f55e56425f1fc0acfb21f39d5c3e8e331029298a80c64f2dea4e7760ba1fd463
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1
1
+ 2.1.3
data/.travis.yml CHANGED
@@ -4,7 +4,6 @@ rvm:
4
4
  - 2.1.2
5
5
  - 2.1.0
6
6
  - 2.0.0
7
- - 1.9.3
8
7
  - 'ruby-head'
9
8
  matrix:
10
9
  allow_failures:
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gemspec
5
5
 
6
6
  gem 'coveralls', require: false
7
7
  gem 'rubocop', require: false
8
+ gem 'pry'
data/README.md CHANGED
@@ -34,7 +34,7 @@ script.save!
34
34
  ## #execute!
35
35
 
36
36
  * `#execute!(no args)`
37
- * returns scripts stdout of whole of the shellscript
37
+ * returns the shellscript's stdout of whole of the shellscript
38
38
 
39
39
  * `#execute!(block)`
40
40
  * returns `nil`, yields the shellscript's stdout st each line(splited by `\n`)
@@ -48,6 +48,20 @@ script.save!
48
48
  * `method:` (default: `execute!`)
49
49
  * the execute method's name
50
50
 
51
+ * `command:` (default: `'/bin/sh'`)
52
+
53
+ ## Options of `.acts_as_rubyscript_executable`
54
+
55
+ * `script:` (default: `:script`)
56
+ * if `Symbol`, the same name column's value will be evaluated as shellscript
57
+ * if `String`, the string will be evaluated as shellscript
58
+
59
+ * `method:` (default: `ruby_execute!`)
60
+ * the execute method's name
61
+
62
+ * `command:` (default: `'ruby'`)
63
+ * Set path like `'/usr/bin/ruby'`, or array to set option like `['ruby', '-c']`, `['bundle exec rails', 'r']`
64
+
51
65
  ## Installation
52
66
 
53
67
  Add this line to your application's Gemfile:
@@ -21,10 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "pry"
25
24
  spec.add_development_dependency "sqlite3"
26
25
 
27
- spec.required_ruby_version = '>= 1.9.3'
26
+ spec.required_ruby_version = '>= 2.0.0'
28
27
 
29
28
  spec.add_dependency("activerecord", [">= 4.1.6"])
30
29
  end
@@ -1,3 +1,5 @@
1
+ require 'tempfile'
2
+
1
3
  module ActiveRecord
2
4
  module Acts
3
5
  module ShellscriptExecutable
@@ -6,26 +8,41 @@ module ActiveRecord
6
8
  end
7
9
 
8
10
  module ClassMethods
9
- def acts_as_shellscript_executable(options = {})
10
- configuration = { method: :execute!, script: :script }
11
- configuration.update(options) if options.is_a?(Hash)
12
- method = configuration[:method]
11
+ def __define_execute_method(method, type)
13
12
  class_eval <<-EOV
14
13
  def #{method}(&block)
15
- script = @@__configurations__[:#{method}][:script]
14
+ script = @@__executable_methods__[:#{method}][:script]
15
+ command = @@__executable_methods__[:#{method}][:command]
16
16
  answer = ''
17
- __execute__(script, answer, block)
17
+ __execute__(script, :#{type}, answer, command, block)
18
18
  block_given? ? nil : answer
19
19
  end
20
20
  EOV
21
+ end
21
22
 
23
+ def __configs_set(command, script, method)
22
24
  configurations = begin
23
- class_variable_get(:@@__configurations__)
25
+ class_variable_get(:@@__executable_methods__)
24
26
  rescue NameError
25
27
  {}
26
28
  end
27
- configurations[method] = configuration
28
- class_variable_set(:@@__configurations__, configurations)
29
+ configurations[method] = { command: command, script: script }
30
+ class_variable_set(:@@__executable_methods__, configurations)
31
+ end
32
+
33
+ def acts_as_shellscript_executable(opt)
34
+ __add_method(:shell, opt)
35
+ end
36
+
37
+ def acts_as_rubyscript_executable(opt)
38
+ __add_method(:ruby, opt)
39
+ end
40
+
41
+ def __add_method(type, method: nil, script: :script, command: nil)
42
+ method ||= (type == :ruby ? :ruby_execute! : :execute!)
43
+ command ||= (type == :ruby ? 'ruby' : '/bin/sh')
44
+ __define_execute_method method, type
45
+ __configs_set command, script, method
29
46
  include ::ActiveRecord::Acts::ShellscriptExecutable::InstanceMethods
30
47
  end
31
48
  end
@@ -33,18 +50,24 @@ module ActiveRecord
33
50
  module InstanceMethods
34
51
  private
35
52
 
36
- def __execute__(script, answer, block = nil)
53
+ def __execute__(script, type, answer, command, block = nil)
37
54
  script = send script if script.is_a? Symbol
55
+ path = __temp_script_path(script, type)
56
+
38
57
  retval = []
39
- script.split("\n").each do |line|
40
- if block
41
- block.call `#{line}`
42
- else
43
- retval << `#{line}`
44
- end
58
+ IO.popen([*command, path], err: [:child, :out]).each do |io|
59
+ block ? block.call(io) : retval << io
45
60
  end
46
61
  answer.replace retval.join
47
62
  end
63
+
64
+ def __temp_script_path(script, type)
65
+ Tempfile.open('') do |temp|
66
+ temp.puts 'STDOUT.sync = true' if type == :ruby
67
+ temp.puts script
68
+ temp.path
69
+ end
70
+ end
48
71
  end
49
72
  end
50
73
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Acts
3
3
  module ShellscriptExecutable
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
6
6
  end
7
7
  end
@@ -1,29 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveRecord::Base do
4
- describe '.acts_as_shellscript_executable' do
5
- def db_setup!
6
- ActiveRecord::Base.establish_connection \
7
- adapter: 'sqlite3', database: ':memory:'
8
- ActiveRecord::Schema.verbose = false
9
- ActiveRecord::Base.connection.schema_cache.clear!
10
- db_schema_define!
11
- end
4
+ def db_setup!
5
+ ActiveRecord::Base.establish_connection \
6
+ adapter: 'sqlite3', database: ':memory:'
7
+ ActiveRecord::Schema.verbose = false
8
+ ActiveRecord::Base.connection.schema_cache.clear!
9
+ db_schema_define!
10
+ end
12
11
 
13
- def db_schema_define!
14
- ActiveRecord::Schema.define(version: 1) do
15
- create_table :scripts do |t|
16
- t.column :script, :string
17
- t.column :script2, :string
18
- t.column :result, :string
19
- end
12
+ def db_schema_define!
13
+ ActiveRecord::Schema.define(version: 1) do
14
+ create_table :scripts do |t|
15
+ t.column :script, :string
16
+ t.column :script2, :string
17
+ t.column :result, :string
20
18
  end
21
19
  end
20
+ end
22
21
 
23
- before do
24
- db_setup!
25
- end
22
+ before do
23
+ db_setup!
24
+ end
26
25
 
26
+ describe '.acts_as_shellscript_executable' do
27
27
  context 'given option {script: :script}' do
28
28
  before do
29
29
  class Script < ActiveRecord::Base
@@ -104,7 +104,7 @@ describe ActiveRecord::Base do
104
104
  end
105
105
  end
106
106
 
107
- context 'given option {script: "echo 1\necho 2"}' do
107
+ context 'given 2 options' do
108
108
  before do
109
109
  class Script < ActiveRecord::Base
110
110
  acts_as_shellscript_executable \
@@ -124,4 +124,131 @@ describe ActiveRecord::Base do
124
124
  end
125
125
  end
126
126
  end
127
+
128
+ describe '.acts_as_rubyscript_executable' do
129
+ context 'given option {script: :script}' do
130
+ before do
131
+ class Script < ActiveRecord::Base
132
+ acts_as_rubyscript_executable script: :script
133
+ end
134
+ end
135
+
136
+ it do
137
+ script = Script.create
138
+ script.script = 'puts "lalala"'
139
+ expect(script.ruby_execute!).to eq "lalala\n"
140
+ end
141
+ end
142
+
143
+ context 'given option {script: "puts 1\nputs 2"}' do
144
+ before do
145
+ class Script < ActiveRecord::Base
146
+ acts_as_rubyscript_executable script: "puts 1\nputs 2"
147
+ end
148
+ end
149
+
150
+ describe 'block given' do
151
+ it do
152
+ script = Script.create
153
+ watcher = []
154
+
155
+ retval = script.ruby_execute! do |each_line_result|
156
+ watcher << each_line_result
157
+ end
158
+
159
+ expect(retval).to be_nil
160
+ expect(watcher).to eq ["1\n", "2\n"]
161
+ end
162
+ end
163
+ end
164
+
165
+ context 'given option {command: ["ruby", "-c"]}' do
166
+ before do
167
+ class Script < ActiveRecord::Base
168
+ acts_as_rubyscript_executable \
169
+ script: "puts 1\nputs 2", command: ['ruby', '-c']
170
+ end
171
+ end
172
+
173
+ describe 'block given' do
174
+ it do
175
+ script = Script.create
176
+ watcher = []
177
+
178
+ retval = script.ruby_execute! do |each_line_result|
179
+ watcher << each_line_result
180
+ end
181
+
182
+ expect(retval).to be_nil
183
+ expect(watcher).to eq ["Syntax OK\n"]
184
+ end
185
+ end
186
+ end
187
+
188
+ context 'given option {command: "ruby"}' do
189
+ before do
190
+ class Script < ActiveRecord::Base
191
+ acts_as_rubyscript_executable \
192
+ script: "puts 1\nputs 2", command: 'ruby'
193
+ end
194
+ end
195
+
196
+ describe 'block given' do
197
+ it do
198
+ script = Script.create
199
+ watcher = []
200
+
201
+ retval = script.ruby_execute! do |each_line_result|
202
+ watcher << each_line_result
203
+ end
204
+
205
+ expect(retval).to be_nil
206
+ expect(watcher).to eq ["1\n", "2\n"]
207
+ end
208
+ end
209
+ end
210
+
211
+ context 'given option {command: "ruby"}, magic comment with non-ascii' do
212
+ before do
213
+ class Script < ActiveRecord::Base
214
+ acts_as_rubyscript_executable \
215
+ script: "#! coding:utf-8\nputs 1\nputs '日本語'", command: 'ruby'
216
+ end
217
+ end
218
+
219
+ describe 'block given' do
220
+ it do
221
+ script = Script.create
222
+ watcher = []
223
+
224
+ retval = script.ruby_execute! do |each_line_result|
225
+ watcher << each_line_result
226
+ end
227
+
228
+ expect(retval).to be_nil
229
+ expect(watcher).to eq ["1\n", "日本語\n"]
230
+ end
231
+ end
232
+ end
233
+
234
+ context 'given 2 options' do
235
+ before do
236
+ class Script < ActiveRecord::Base
237
+ acts_as_rubyscript_executable \
238
+ script: "puts 1\nputs 2"
239
+ acts_as_rubyscript_executable \
240
+ script: "puts 3\nputs 4", method: :awesome!
241
+ end
242
+ end
243
+
244
+ it do
245
+ script = Script.create
246
+ result = script.ruby_execute!
247
+ awesome_result = script.awesome!
248
+
249
+ expect(result).to eq "1\n2\n"
250
+ expect(awesome_result).to eq "3\n4\n"
251
+ end
252
+ end
253
+ end
127
254
  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.3
4
+ version: 0.0.4
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-23 00:00:00.000000000 Z
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: pry
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: sqlite3
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -128,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
114
  requirements:
129
115
  - - ">="
130
116
  - !ruby/object:Gem::Version
131
- version: 1.9.3
117
+ version: 2.0.0
132
118
  required_rubygems_version: !ruby/object:Gem::Requirement
133
119
  requirements:
134
120
  - - ">="