fife 0.0.1.alpha → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43c985295f282009483540caf7df104753ccbc59
4
- data.tar.gz: aa20d1317ca9286b8eb0937cb4d3b56df0eee3cd
3
+ metadata.gz: 7ba441e8b0b9a3491cb9878ab9d6791e1b9789f3
4
+ data.tar.gz: 30d4bbb2a0dd03dd70a845f8d3f952a432979a14
5
5
  SHA512:
6
- metadata.gz: 09931e0e7810f6ecb565887186f6336adeb4b16c11161ddbf68622e8fe3618d8d826b6fda0b59ebc9a28291907e13cb348f265bd1e6129aab900ddaad1037ae4
7
- data.tar.gz: 65b51126ee185b42ce488d678837fcd6ec5f8309dac05ee02e74562668f9f17a0170b50488bafcfd424e5898b415cfd5f3bc89f018954fde2426f3352895f344
6
+ metadata.gz: c88d307cd22d1ee490a579d947e833ef246cc17abb2fe33f7ac9dc8c509da15b11101f1260262e085e715d6ca6ff39daef642f4a3b626936d49333b9faa6dab7
7
+ data.tar.gz: 767cbfd8fc3cc569df285c339c772562f9d3dc072fbb833579cac47b223b39e74ed42cd5637111e6923087e6b00afff7da88ec8be5627f92f474ea8f5c22d39a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fife (0.0.1.alpha)
4
+ fife (0.1.0)
5
5
  activesupport (~> 4.2)
6
6
  net-sftp (~> 2.1)
7
7
 
data/README.md CHANGED
@@ -69,7 +69,7 @@ Currently, `Fife` ships with 4 operations: `:noop`, `:name`, `:store` and `:clos
69
69
  * noop
70
70
  Performs no operation on the IO, and returns the IO directly.
71
71
  * close
72
- Closes the
72
+ Closes the IO if not already closed.
73
73
  * store
74
74
  Stores the content of the IO.
75
75
  * name
@@ -89,7 +89,7 @@ If you feel lambdas are not enough for your job,
89
89
  you can easily define your own operations.
90
90
 
91
91
  ```ruby
92
- class Fife::Operations::MyOperation
92
+ class MyOperation
93
93
  def initialize(arg1, arg2)
94
94
  # Initialize the Operation
95
95
  end
@@ -98,10 +98,15 @@ class Fife::Operations::MyOperation
98
98
  # Handle the io and return some IO instances
99
99
  end
100
100
  end
101
+
102
+ # Register it
103
+ Fife::Operations.register(:my_op, MyOperation)
101
104
  ```
105
+
102
106
  Then you can use it like
107
+
103
108
  ```ruby
104
- Fife(io_ary).pipe(:my_operation, 1, 2)
109
+ Fife(io_ary).pipe(:my_op, 1, 2)
105
110
  ```
106
111
 
107
112
  ### Storage
@@ -120,10 +125,28 @@ the remote filename depends on the name of the IO.
120
125
  (I know it's nasty, but it's still under developing)
121
126
 
122
127
  ```ruby
123
- storage = Fife::Storage::Sftp.new('localhost', 'me', '/path/to/remote/dir', password: 'P@ssw0rd')
128
+ storage = Fife::Storage::Sftp.new do
129
+ host 'localhost'
130
+ user 'me'
131
+ remote_dir '/path/to/my/remote/dir'
132
+ naming -> io {"lorem_ipsum_#{rand}"}
133
+ ssh_options password: 's3cret'
134
+ end
124
135
  Fife(io_ary).pipe(:store, storage)
125
136
  ```
126
137
 
138
+ ### Abort on fail
139
+ By default, **Fife** ignores all failures and keeps on executing further operations on succeeded operations.
140
+ This behavior can be changed by passing `abort_on_fail: true` to `Kernel#Fife`:
141
+
142
+ ```ruby
143
+ Fife(io_ary, abort_on_fail: true)
144
+ ```
145
+
146
+ When `abort_on_fail` is turned on, if some operation raises error, no further operation will be executed,
147
+ and an `Fife::Pipe::Aborted` will be raised.
148
+ You can get the real causes by accessing `Fife::Pipe::Aborted#causes`
149
+
127
150
  ## Development
128
151
 
129
152
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,5 +1,6 @@
1
1
  class Fife::Operations::Close
2
2
  def call(io)
3
3
  io.close unless io.closed?
4
+ io
4
5
  end
5
6
  end
@@ -5,4 +5,14 @@ module Fife::Operations
5
5
  autoload :Close
6
6
  autoload :Name
7
7
  autoload :Store
8
+
9
+ @registered = ActiveSupport::HashWithIndifferentAccess.new
10
+
11
+ def self.register(name, operation_class)
12
+ @registered[name] = operation_class
13
+ end
14
+
15
+ def self.[](name)
16
+ @registered[name] ||= const_get(name.to_s.camelize)
17
+ end
8
18
  end
data/lib/fife/pipe.rb CHANGED
@@ -1,22 +1,45 @@
1
1
  class Fife::Pipe
2
2
 
3
- attr_reader :ios
3
+ class Aborted < RuntimeError
4
+ attr_reader :causes
4
5
 
5
- def initialize(ios)
6
- @ios = ios.flatten.tap(&:compact!)
6
+ def initialize(causes=[])
7
+ @causes = causes
8
+ end
9
+ end
10
+
11
+ attr_reader :ios, :abort_on_fail
12
+
13
+ def initialize(ios, abort_on_fail)
14
+ @ios = ios
15
+ @abort_on_fail = abort_on_fail
7
16
  end
8
17
 
9
18
  def pipe(op, *args)
10
- op = Fife::Operations.const_get(op.to_s.camelize.to_sym).new(*args) unless op.respond_to? :call
19
+ op = Fife::Operations[op].new(*args) unless op.respond_to? :call
11
20
  output = []
12
21
 
13
22
  ios.map { |io|
14
23
  Thread.new do
15
- output << op.call(io.tap(&:rewind))
24
+ output << catch(:halt) do
25
+ begin
26
+ op.call(io.tap(&:rewind))
27
+ rescue => e
28
+ throw :halt, e
29
+ end
30
+ end
16
31
  end
17
32
  }.each(&:join)
18
33
 
19
- self.class.new(output)
34
+ errors, output = output.tap(&:flatten!).partition{|o| o.is_a?(StandardError)}
35
+
36
+ should_abort = abort_on_fail && (errors.any? || output.any?(&:nil?))
37
+ if should_abort
38
+ ios.each { |io| io.close unless io.closed? }
39
+ raise Aborted, errors
40
+ end
41
+
42
+ self.class.new(output.tap(&:compact!), abort_on_fail)
20
43
  end
21
44
 
22
45
  end
@@ -2,20 +2,47 @@ require 'net/sftp'
2
2
  require 'pathname'
3
3
  require 'fileutils'
4
4
 
5
+ class Net::SFTP::Session
6
+ def mkdir_p!(pathname)
7
+ pathname = Pathname(pathname) unless pathname.is_a? Pathname
8
+ pathname.descend do |path|
9
+ mkdir!(path) rescue nil
10
+ end
11
+ end
12
+ end
13
+
5
14
  class Fife::Storage::Sftp
6
- attr_reader :host, :user, :remote_dir, :ssh_options
7
15
 
8
- def initialize(host, user, remote_dir, ssh_options)
9
- @host, @user, @ssh_options = host, user, ssh_options
10
- @remote_dir = Pathname(remote_dir)
11
- ensure_remote_dir_presence!
16
+ class UninitializedAttributes < RuntimeError; end
17
+
18
+ REQUIRED_ATTRIBUTES = [:host, :user, :naming, :ssh_options, :remote_dir]
19
+
20
+ def remote_dir(*value)
21
+ return @remote_dir if value.empty?
22
+ @remote_dir = Pathname(value[0])
23
+ end
24
+
25
+ (REQUIRED_ATTRIBUTES - [:remote_dir]).each do |attr|
26
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
27
+ def #{attr}(*value)
28
+ return @#{attr} if value.empty?
29
+ @#{attr} = value[0]
30
+ end
31
+ RUBY
32
+ end
33
+
34
+ def initialize(&block)
35
+ instance_eval &block
36
+ uninitialized_attributes = REQUIRED_ATTRIBUTES.select{|attr| send(attr).nil?}
37
+ raise UninitializedAttributes, uninitialized_attributes unless uninitialized_attributes.empty?
12
38
  end
13
39
 
14
40
  def store(io)
15
- raise UnnamedIO unless io.respond_to? :name
16
- remote_path = remote_dir.join(io.name)
41
+ remote_path = remote_dir.join(@naming.call(io))
42
+ dir = remote_path.dirname
17
43
  Net::SFTP.start(host, user, ssh_options) do |sftp|
18
- sftp.file.open(remote_path, 'w') { |f| IO.copy_stream(io, f) }
44
+ sftp.mkdir_p!(dir)
45
+ sftp.file.open(remote_path, 'w') { |f| IO.copy_stream(io.tap(&:rewind), f) }
19
46
  end
20
47
  io.close
21
48
  end
data/lib/fife/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fife
2
- VERSION = "0.0.1.alpha"
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/fife.rb CHANGED
@@ -11,7 +11,7 @@ module Fife
11
11
  end
12
12
 
13
13
  module Kernel
14
- def Fife(*ios)
15
- Fife::Pipe.new(ios)
14
+ def Fife(*ios, abort_on_fail: false)
15
+ Fife::Pipe.new(ios.tap(&:flatten!).tap(&:compact!), abort_on_fail)
16
16
  end
17
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fife
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - aetherus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2015-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -124,9 +124,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - ">"
127
+ - - ">="
128
128
  - !ruby/object:Gem::Version
129
- version: 1.3.1
129
+ version: '0'
130
130
  requirements: []
131
131
  rubyforge_project:
132
132
  rubygems_version: 2.4.8