rmtools 2.1.1 → 2.2.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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTk3ZDI3YzQ2NTBlZGE1ZjMzMTNhNmUyMjMxMjBlMzkzOTY5ZGE4Mg==
5
+ data.tar.gz: !binary |-
6
+ Yzc5ZWVlOTFmNzIwOTEyM2Q2YzAwNjcxZWRlMTlhYzJhNTg2ZDMxYQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NmE5NDE0ZDhjYjNkODU1MmFkMTkwMjZiM2MzMzVmMTk1ZDdkOGI0MmExMjIz
10
+ YjEyYmZmMWQwYmQ5MGViMjE1NTBmMDMyZGU2NjUwNWRhNmUwM2U5NDQ1MGY5
11
+ YThiZTY2ZTRiNmFmMDg2MzYyMjZlMTU3MjdjMThkNDk1YjQ3YjM=
12
+ data.tar.gz: !binary |-
13
+ YmRkZmE2NDhhZGRjNDJmOGMzM2E0ZjBkYTIyMmU2MDBkZGEyYzg3YTQxZDk5
14
+ ZjNjOWEzNmE3NDhjODIwMzY3MWRhNjA4ZmE5NmM3YmYzYTc0OWM2MDZhMmIy
15
+ MDQ2Y2IwMTczY2VkOWFmODU4YWM0OWE2ZDE2MjhjMDQzZjkzMzI=
data/README.md CHANGED
@@ -27,6 +27,28 @@ It's still randomly documented since it's just my working tool.
27
27
 
28
28
  ### CHANGES
29
29
 
30
+ ##### Version 2.2.1
31
+
32
+ * ::RMLogger
33
+ * Aliased #info -> #puts to be able to set RMLogger as default logger wherever it's possible
34
+ * Added #get_config
35
+
36
+ * ::Painter
37
+ * #method_missing now accepts :"_<effect key>" as well as :"<color key>[_<effect key>]"
38
+
39
+ ##### Version 2.2.0
40
+
41
+ * Added ::FileWatcher
42
+ * Constantly checks files updated in a directory and does some action whenever a update has been met
43
+ * A subclass, ::ScpHelper copies updated files to specified host
44
+
45
+ ##### Version 2.1.1
46
+
47
+ * Array
48
+ * Added #any and #none to iterators pattern
49
+ * Hash
50
+ * Fixed #any? (and removed it for Ruby>=1.9)
51
+
30
52
  ##### Version 2.1.0
31
53
 
32
54
  * Array
@@ -77,7 +77,7 @@ module RMTools
77
77
  #
78
78
  # Actually, transparent coloring is slower
79
79
  def method_missing(m, str, transparent=false)
80
- paint str, transparent, *(m.to_s/"_")
80
+ paint str, transparent, *(m.to_s/"_").bs
81
81
  end
82
82
 
83
83
  def clean str
@@ -53,6 +53,13 @@ module RMTools
53
53
  def tick!
54
54
  print %W{|\b /\b -\b \\\b +\b X\b}.rand
55
55
  end
56
+
57
+ def tick_while
58
+ ticker = thread {loop {RMTools::tick!}}
59
+ res = yield
60
+ ticker.kill
61
+ res
62
+ end
56
63
 
57
- module_function :tick!
64
+ module_function :tick!, :tick_while
58
65
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
+ RMTools::require 'core'
2
3
  RMTools::require 'console/coloring'
3
4
  RMTools::require 'text/string_parse'
4
- RMTools::require 'b'
5
5
 
6
6
  module RMTools
7
7
 
@@ -46,7 +46,7 @@ module RMTools
46
46
  end
47
47
 
48
48
  def defaults
49
- puts %{ # #{@c.y 'common options:'}
49
+ Kernel::puts %{ # #{@c.y 'common options:'}
50
50
  :q => false, # not print
51
51
  :out => false, # output to file, may contain strftime's %H%M%Y etc for filename
52
52
  :time => ["%H:%M:%S", "%03d"], # strftime, [msecs]
@@ -127,14 +127,18 @@ module RMTools
127
127
  str << "\n" if opts&INLINE==0
128
128
  log_str = cfg.color_out ? str : @c.clean(str)
129
129
  RMTools.write out, log_str if log_
130
- Kernel.print str if print_
130
+ Kernel::print str if print_
131
131
  end
132
132
 
133
133
  def get_config!
134
134
  @file_formats.empty? ? @default_format : @file_formats[File.expand_path((@current_caller = caller)[1].till ':')]
135
135
  end
136
136
 
137
- # controllers:
137
+ def get_config(file=nil)
138
+ @file_formats[file && File.expand_path(file)]
139
+ end
140
+
141
+ # controls:
138
142
  # - $panic: print debug messages
139
143
  # - $verbose: print log messages
140
144
  # - $quiet: print only warn messages regardless of other globals
@@ -189,6 +193,11 @@ module RMTools
189
193
  alias :<= :debug
190
194
  alias :<< :info
191
195
  alias :< :warn
196
+ alias :puts :info
197
+
198
+ def print text
199
+ info text, caller: 1, mute: INLINE
200
+ end
192
201
 
193
202
  Modes.each {|m| define_method("#{m}=") {|mute| send :"mute_#{m}=", !mute}}
194
203
 
@@ -0,0 +1,156 @@
1
+ # encoding: utf-8
2
+ module RMTools
3
+
4
+ class FileWatcher
5
+ cattr_reader :threads
6
+ attr_reader :thread
7
+ @@threads = {}
8
+
9
+ def initialize(params={})
10
+ @debug ||= params[:debug]
11
+ @pwd ||= params[:pwd] || Dir.pwd
12
+ @thread_name ||= params[:thread_name] || "#{self.class.name.underscore}:#@host"
13
+ @interval ||= params[:interval] || 1
14
+ end
15
+
16
+ def files_stats(*stat_params)
17
+ Dir(@pwd || Dir.pwd).recursive_content.flatten.map_hash {|fn|
18
+ if File.file? fn
19
+ if stat_params
20
+ stats = stat_params.map_hash {|param|
21
+ [param, File.__send__(param, fn)]
22
+ }
23
+ else
24
+ stats = File.stats fn
25
+ end
26
+ [fn, stats]
27
+ end
28
+ }
29
+ end
30
+
31
+ def kill
32
+ @@threads[@thread_name].kill
33
+ end
34
+
35
+ def temp_string(str, color=nil)
36
+ # space is needed because cursor is on the left
37
+ str = " " + str.to_s
38
+ backspace = "\b"*str.size
39
+ if color
40
+ str = Painter.send(color, str)
41
+ end
42
+ str << backspace
43
+ end
44
+
45
+ def print_temp(str, color=nil)
46
+ print temp_string str, color
47
+ end
48
+
49
+ def puts_temp(str, color=nil)
50
+ print_temp(str.to_s+"\n", color)
51
+ end
52
+
53
+ def watch(opts={})
54
+ if @@threads[@thread_name]
55
+ kill
56
+ end
57
+ @@threads[@thread_name] = @thread = Thread.new {
58
+ loop {watch_cycle}
59
+ }
60
+ end
61
+
62
+ # Памятка про printf для чисел:
63
+ # precision = минимальное число цифр;
64
+ # %f -> справа || 6
65
+ # %d -> слева, заполняется нулями || 1
66
+ # len = минимальная длина строки; если начинается с 0, заполняется нулями, иначе пробелами || 0
67
+ # "%[<len>.][<precision>]d"
68
+ # "%[<len>][.<precision>]f"
69
+ def print_time(seconds)
70
+ minutes, seconds = seconds.to_i.divmod 60
71
+ hours, minutes = minutes.divmod 60
72
+ diff = "#{"#{hours}:" if hours.b}#{"%2d:"%minutes if hours.b or minutes.b}#{"%2d"%seconds}"
73
+ print_temp(diff, :b_b)
74
+ end
75
+
76
+ def print_idle_time
77
+ print_time(Time.now - @cur_time)
78
+ end
79
+
80
+ def watch_cycle
81
+ @files = select_files
82
+ process
83
+ wait
84
+ end
85
+
86
+ # => [pathname, ...] or {pathname => [action, ...]} or {pathname => action}
87
+ def select_files
88
+ {}
89
+ end
90
+
91
+ def process
92
+ raise NotImplementedError, "do something with @files"
93
+ end
94
+
95
+ def wait
96
+ sleep @interval
97
+ end
98
+
99
+ end
100
+
101
+ class ScpHelper < FileWatcher
102
+
103
+ def initialize(params={})
104
+ @pwd = params[:pwd] || (params[:host] ? File.join(Dir.pwd, params[:host]) : Dir.pwd)
105
+ @host = params[:host] || File.basename(@pwd)
106
+ super params
107
+ end
108
+
109
+ def files_mtimes
110
+ files_stats :mtime
111
+ end
112
+
113
+ # @ fpath : "/<relative path>"
114
+ def scp(fpath)
115
+ fullpath = File.join @pwd, fpath
116
+ cmd = "scp #{fullpath.inspect} #{[@host, fpath].join(':').inspect} 2>&1"
117
+ print "`#{cmd}`: " if @debug
118
+ if res = RMTools::tick_while {`#{cmd}`}.b
119
+ puts "[ #{Painter.rb('Error')} ]: #{res}"
120
+ else
121
+ print "[ #{Painter.g('OK')} ]"
122
+ end
123
+ end
124
+
125
+ def watch
126
+ @cur_time, @prev_time = Time.now, nil
127
+ super
128
+ end
129
+
130
+ def select_files
131
+ @cur_time, @prev_time = Time.now, @cur_time
132
+ files_mtimes.select {|f, s| s.mtime >= @prev_time}.to_a.firsts
133
+ .map {|fpath| fpath.sub(@pwd, '')}
134
+ end
135
+
136
+ def process
137
+ if @files.b
138
+ $log.debug {[prev_time.to_f, File.mtime(@pwd+@files[0]).to_f]}
139
+ puts Painter.w("\nMODIFIED IN #@host: ") + @files*', '
140
+ @files.each {|fpath| scp fpath}
141
+ else
142
+ $log.debug {[@prev_time.to_f, File.mtime('var/rails/nzm/app/assets/stylesheets/app.css').to_f]}
143
+ @cur_time = @prev_time
144
+ end
145
+ end
146
+
147
+ def wait
148
+ (@interval*10).times {
149
+ sleep 0.1
150
+ print_idle_time
151
+ }
152
+ end
153
+
154
+ end
155
+
156
+ end
@@ -125,12 +125,14 @@ class Range
125
125
  # and (0...1.0).include? 1.0
126
126
  # => false
127
127
  def include?(number_or_range)
128
- if Numeric === number_or_range
128
+ if Numeric === number_or_range or String === number_or_range
129
129
  include_number? number_or_range
130
130
  elsif XRange === number_or_range
131
131
  number_or_range.include? self
132
- else
132
+ elsif Range === number_or_range
133
133
  include_number? number_or_range.first and include_number? number_or_range.last
134
+ else
135
+ raise TypeError, "can not find #{number_or_range.class} in Range"
134
136
  end
135
137
  end
136
138
 
@@ -9,8 +9,8 @@ module Enumerable
9
9
  reduce(m) {|m, i| m ? o.call(m, i) : i}
10
10
  when Symbol
11
11
  block_given? ?
12
- reduce(m && yield(m)) {|m, i| m ? m.send(o, yield(i)) : yield(i)} :
13
- reduce(m) {|m, i| m ? m.send(o, i) : i}
12
+ reduce(m && yield(m)) {|m, i| m ? m.__send__(o, yield(i)) : yield(i)} :
13
+ reduce(m) {|m, i| m ? m.__send__(o, i) : i}
14
14
  else TypeError! o, Proc, Symbol
15
15
  end
16
16
  end
@@ -24,8 +24,8 @@ module Enumerable
24
24
  reverse.reduce(m) {|m, i| m ? o.call(i, m) : i}
25
25
  when Symbol
26
26
  block_given? ?
27
- reverse.reduce(m && yield(m)) {|m, i| m ? yield(i).send(o, m) : yield(i)} :
28
- reverse.reduce(m) {|m, i| m ? i.send(o, m) : i}
27
+ reverse.reduce(m && yield(m)) {|m, i| m ? yield(i).__send__(o, m) : yield(i)} :
28
+ reverse.reduce(m) {|m, i| m ? i.__send__(o, m) : i}
29
29
  else TypeError! o, Proc, Symbol
30
30
  end
31
31
  end
@@ -1,3 +1,3 @@
1
1
  module RMTools
2
- VERSION = '2.1.1'
2
+ VERSION = '2.2.1'
3
3
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmtools
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
5
- prerelease:
4
+ version: 2.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sergey Baev
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-21 00:00:00.000000000 Z
11
+ date: 2013-07-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -117,6 +110,7 @@ files:
117
110
  - lib/rmtools/dev/timer.rb
118
111
  - lib/rmtools/dev/trace_format.rb
119
112
  - lib/rmtools/dev/void.rb
113
+ - lib/rmtools/dev/watching.rb
120
114
  - lib/rmtools/enumerable.rb
121
115
  - lib/rmtools/enumerable/array.rb
122
116
  - lib/rmtools/enumerable/array_iterators.rb
@@ -171,27 +165,26 @@ files:
171
165
  homepage: https://github.com/tinbka/rmtools
172
166
  licenses:
173
167
  - MIT
168
+ metadata: {}
174
169
  post_install_message:
175
170
  rdoc_options: []
176
171
  require_paths:
177
172
  - lib
178
173
  required_ruby_version: !ruby/object:Gem::Requirement
179
- none: false
180
174
  requirements:
181
175
  - - ! '>='
182
176
  - !ruby/object:Gem::Version
183
177
  version: '0'
184
178
  required_rubygems_version: !ruby/object:Gem::Requirement
185
- none: false
186
179
  requirements:
187
180
  - - ! '>='
188
181
  - !ruby/object:Gem::Version
189
182
  version: '0'
190
183
  requirements: []
191
184
  rubyforge_project:
192
- rubygems_version: 1.8.24
185
+ rubygems_version: 2.0.3
193
186
  signing_key:
194
- specification_version: 3
187
+ specification_version: 4
195
188
  summary: Collection of helpers for debug, text/array/file processing and simply easing
196
189
  a coding process
197
190
  test_files: []