rmtools 1.2.10 → 1.2.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt CHANGED
@@ -4,10 +4,18 @@ Copyright (c) 2010-2012
4
4
  This work is licensed under the same license as Ruby language.
5
5
 
6
6
  == RMTools
7
+ https://github.com/tinbka/rmtools
7
8
  Methods for basic classes addon collection.
8
9
 
9
10
  == CHANGES
10
11
 
12
+ == Version 1.2.11
13
+
14
+ * Added Array#select_by and #reject_by pattern-iterators
15
+ * Fixed ActiveRecord::Base.select_rand
16
+ * Restricted RMTools.format_trace to use with Rails because of hard slowdown
17
+ * Updated Proc constants for ruby 1.9
18
+
11
19
  == Version 1.2.10
12
20
 
13
21
  * Update String#parse:caller to parse ruby 1.9 "block level". Now block level processes in RMLogger and RMTools.format_trace
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'rake'
2
- require 'lib/rmtools/install'
2
+ require './lib/rmtools/install'
3
3
  compile_manifest
4
4
 
5
- RMTOOLS_VERSION = '1.2.10'
5
+ RMTOOLS_VERSION = '1.2.11'
6
6
  begin
7
7
  require 'hoe'
8
8
  config = Hoe.spec 'rmtools' do
@@ -31,6 +31,16 @@ module RMTools
31
31
  method_defined? :b and undef_method :b
32
32
  method_defined? :p and undef_method :p
33
33
 
34
+ def demo(str, pattern=nil)
35
+ %w[black red green yellow blue purple cyan gray].xprod(%w[bold underline graybg boldbg]).each {|color, effect|
36
+ if pattern
37
+ puts ghl(str, pattern, "#{color}_#{effect}")
38
+ else
39
+ puts paint(str, transparent, color, effect)
40
+ end
41
+ }
42
+ end
43
+
34
44
  def paint(str, transparent, num=nil, effect=nil)
35
45
  # default cmd.exe cannot into ANSI
36
46
  str = str.to_s
@@ -40,7 +50,7 @@ module RMTools
40
50
  if !num
41
51
  effect = Effects[num]
42
52
  elsif num.is Array
43
- num, effect = num
53
+ num, effect = num
44
54
  end
45
55
  end
46
56
  effect = Effects[effect] if effect.is String
@@ -57,6 +67,7 @@ module RMTools
57
67
  end
58
68
  end
59
69
 
70
+ # Without +transparent+ Painter stops coloring once it find colored substring
60
71
  # puts "words have one #{Painter.red_bold 'highlighted'} among them"
61
72
  # <default>words have one <red>highlighted</red> among them</default>
62
73
  # puts Painter.gray "words have one #{Painter.red_bold 'highlighted'} among them"
@@ -22,7 +22,7 @@ class Module
22
22
  end
23
23
 
24
24
  def self_name
25
- @self_name ||= name.match(/[^:]+$/)
25
+ @self_name ||= name[/[^:]+$/]
26
26
  end
27
27
 
28
28
  def my_methods filter=//
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
  class Proc
3
- NULL = lambda {} unless defined? Proc::NULL
4
- TRUE = lambda {true} unless defined? Proc::TRUE
5
- FALSE = lambda {false} unless defined? Proc::FALSE
3
+ NULL = lambda {|*x|} unless defined? Proc::NULL
4
+ TRUE = lambda {|*x| true} unless defined? Proc::TRUE
5
+ FALSE = lambda {|*x| false} unless defined? Proc::FALSE
6
6
  attr_accessor :string
7
7
 
8
8
  def when
@@ -19,9 +19,98 @@ module ActiveRecord
19
19
 
20
20
  class Base
21
21
 
22
- def self.establish_connection_with config
23
- ActiveRecord.establish_connection_with config
24
- end
22
+ class << self
23
+
24
+ def establish_connection_with config
25
+ ActiveRecord.establish_connection_with config
26
+ end
27
+
28
+ def merge_conditions(*conditions)
29
+ segments = conditions.map {|condition|
30
+ sanitize_sql condition
31
+ }.reject {|condition|
32
+ condition.blank?
33
+ }
34
+ "(#{segments.join(') AND (')})" unless segments.empty?
35
+ end
36
+
37
+ def execute_sanitized(sql)
38
+ connection.execute sanitize_sql sql
39
+ end
40
+
41
+ # requires primary key
42
+ def forced_create(hash)
43
+ names = columns.names
44
+ id = (hash[primary_key.to_sym] ||= maximum(primary_key)+1)
45
+ execute_sanitized([
46
+ "INSERT INTO #{quoted_table_name} VALUES (:#{names*', :'})",
47
+ Hash[names.map {|name| [name.to_sym, nil]}].merge(hash)
48
+ ])
49
+ find_by_sql(["SELECT * FROM #{quoted_table_name} WHERE #{primary_key} = ?", id])[0]
50
+ end
51
+
52
+ # values must be 2-tuple array [[column1, value1], [column2, value2], ...] and columns must be in order they've been created
53
+ def insert_unless_exist(table, values)
54
+ table = connection.quote_table_name table
55
+ if execute_sanitized(["SELECT COUNT(*) FROM #{table} WHERE #{vaues.firsts.map {|v| "#{connection.quote_column_name v}=?"}*' AND '} LIMIT 1", *values.lasts]).to_a.flatten > 0
56
+ false
57
+ else
58
+ execute_sanitized ["INSERT INTO #{table} VALUES (#{['?']*values.size*','})", *values.lasts]
59
+ true
60
+ end
61
+ end
62
+
63
+ def select_rand(limit, options={})
64
+ cnt = options.delete :cnt
65
+ _where = options.delete :where
66
+ cnt_where = options.delete(:cnt_where) || _where
67
+ if !cnt and !cnt_where
68
+ ids = options[:ids] || pluck(:id)
69
+ if fields = options[:fields]
70
+ return select(fields).where(id: ids.randsample(limit)).all
71
+ else
72
+ return where(id: ids.randsample(limit)).all
73
+ end
74
+ #cnt = options[:ids].size
75
+ #where ||= "#{table_name}.id IN (:ids)"
76
+ end
77
+ discnt = options.delete :discnt
78
+ tables = options.delete(:tables) || table_name
79
+ cnt_tables = options.delete(:cnt_tables) || tables
80
+ fields = (options.delete(:fields) || %w[*])*','
81
+
82
+ find_by_sql(["SELECT * FROM (
83
+ SELECT @cnt:=#{cnt ? cnt.to_i : 'COUNT(*)'}+1#{-discnt.to_i if discnt}, @lim:=#{limit.to_i}#{" FROM #{cnt_tables} WHERE #{cnt_where}" if !cnt}
84
+ ) vars
85
+ STRAIGHT_JOIN (
86
+ SELECT #{fields}, @lim:=@lim-1 FROM #{tables} WHERE (@cnt:=@cnt-1) AND RAND() < @lim/@cnt#{" AND (#{_where})" if _where}
87
+ ) i", options])
88
+ end
89
+
90
+ class_attribute :enums
91
+ def enum hash
92
+ key = hash.keys.first
93
+ (self.enums ||= {}).merge! hash
94
+ define_attribute_methods if !attribute_methods_generated?
95
+ class_eval %{
96
+ def #{key}
97
+ missing_attribute('#{key}', caller) unless @attributes.has_key?('#{key}')
98
+ self.class.enums[:#{key}][@attributes['#{key}']]
99
+ end
100
+ def #{key}=(val)
101
+ write_attribute('#{key}', Fixnum === val ? val : self.class.enums[:#{key}].index val.to_s
102
+ end
103
+ }
104
+ end
105
+
106
+ end
107
+
108
+ # fix for thinking_sphinx equation in #instances_from_class:
109
+ # ids.collect {|obj_id| instances.detect do |obj| obj.primary_key_for_sphinx == obj_id end}
110
+ # where obj_id is Array
111
+ #def primary_key_for_sphinx
112
+ # [read_attribute(self.class.primary_key_for_sphinx)]
113
+ #end
25
114
 
26
115
  def to_hash
27
116
  return attributes if respond_to? :attributes
@@ -50,85 +139,10 @@ module ActiveRecord
50
139
  self.class.destroy_all(attributes)
51
140
  end
52
141
 
53
- def self.merge_conditions(*conditions)
54
- segments = conditions.map {|condition|
55
- sanitize_sql condition
56
- }.reject {|condition|
57
- condition.blank?
58
- }
59
- "(#{segments.join(') AND (')})" unless segments.empty?
60
- end
61
-
62
- def self.execute_sanitized(sql)
63
- connection.execute sanitize_sql sql
64
- end
65
-
66
- # requires primary key
67
- def self.forced_create(hash)
68
- names = columns.names
69
- id = (hash[primary_key.to_sym] ||= maximum(primary_key)+1)
70
- execute_sanitized([
71
- "INSERT INTO #{quoted_table_name} VALUES (:#{names*', :'})",
72
- Hash[names.map {|name| [name.to_sym, nil]}].merge(hash)
73
- ])
74
- find_by_sql(["SELECT * FROM #{quoted_table_name} WHERE #{primary_key} = ?", id])[0]
75
- end
76
-
77
- # values must be 2-tuple array [[column1, value1], [column2, value2], ...] and columns must be in order they've been created
78
- def self.insert_unless_exist(table, values)
79
- table = connection.quote_table_name table
80
- if execute_sanitized(["SELECT COUNT(*) FROM #{table} WHERE #{vaues.firsts.map {|v| "#{connection.quote_column_name v}=?"}*' AND '} LIMIT 1", *values.lasts]).to_a.flatten > 0
81
- false
82
- else
83
- execute_sanitized ["INSERT INTO #{table} VALUES (#{['?']*values.size*','})", *values.lasts]
84
- true
85
- end
86
- end
87
-
88
142
  def resource_path
89
143
  "#{self.class.name.tableize}/#{id}"
90
144
  end
91
145
 
92
- def self.select_rand(limit, options={})
93
- cnt = options.delete :cnt
94
- discnt = options.delete :discnt
95
- where = options.delete :where
96
- cnt_where = options.delete :cnt_where
97
- tables = options.delete :tables
98
- cnt_tables = options.delete :cnt_tables
99
- fields = options.delete :fields
100
-
101
- find_by_sql(["SELECT * FROM (
102
- SELECT @cnt:=#{cnt ? cnt.to_i : 'COUNT(*)'}+1#{-discnt.to_i if discnt}, @lim:=#{limit.to_i}#{"FROM #{options[:cnt_tables] || table_name} WHERE #{cnt_where}" if !cnt}
103
- ) vars
104
- STRAIGHT_JOIN (
105
- SELECT #{fields || table_name+'.*'}, @lim:=@lim-1 FROM #{tables || table_name} WHERE (@cnt:=@cnt-1) AND RAND() < @lim/@cnt#{" AND (#{where})" if where}
106
- ) i", options])
107
- end
108
-
109
- class_attribute :enums
110
- def self.enum hash
111
- key = hash.keys.first
112
- (self.enums ||= {}).merge! hash
113
- define_attribute_methods if !attribute_methods_generated?
114
- class_eval %{
115
- def #{key}
116
- missing_attribute('#{key}', caller) unless @attributes.has_key?('#{key}')
117
- self.class.enums[:#{key}][@attributes['#{key}']]
118
- end
119
- def #{key}=(val)
120
- write_attribute('#{key}', Fixnum === val ? val : self.class.enums[:#{key}].index val.to_s
121
- end
122
- }
123
- end
124
-
125
- # fix for thinking_sphinx equation in #instances_from_class:
126
- # ids.collect {|obj_id| instances.detect do |obj| obj.primary_key_for_sphinx == obj_id end}
127
- # where obj_id is Array
128
- #def primary_key_for_sphinx
129
- # [read_attribute(self.class.primary_key_for_sphinx)]
130
- #end
131
-
132
146
  end
133
147
 
134
148
  class Relation
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  class BlackHole
3
+ __init__
3
4
 
4
5
  # abc = BlackHole.new
5
6
  # (abc.first.get {|_| !_}.something << 'blah blah')[123].raise!
@@ -15,6 +16,8 @@ class BlackHole
15
16
  end
16
17
  end
17
18
 
19
+ def b; false end
20
+
18
21
  if RUBY_VERSION < '1.9'
19
22
  undef id
20
23
  end
@@ -14,7 +14,7 @@ class Array
14
14
  res = "[ "
15
15
  indent = (size-1).to_s.size
16
16
  res << map_with_index {|k,i|
17
- "#{i.to_s.rjust(indent)}: #{(k.is String and !inspect_string) ? k : k.inspect}"
17
+ "#{RMTools::Painter.w(i.to_s.rjust(indent))}: #{(k.is String and !inspect_string) ? k : k.inspect}"
18
18
  }*"\n "
19
19
  res << "]"
20
20
  puts res
@@ -52,7 +52,7 @@ class Hash
52
52
  str = "{ "
53
53
  sorted = sort rescue to_a.sort_by_to_s
54
54
  str << sorted.map {|k,v|
55
- "#{(k.is String and !inspect_string) ? k : k.inspect} => #{(v.is String and !inspect_string) ? v : v.inspect},"
55
+ "#{RMTools::Painter.w((k.is String and !inspect_string) ? k : k.inspect)} => #{(v.is String and !inspect_string) ? v : v.inspect},"
56
56
  }*"\n "
57
57
  str << "}"
58
58
  puts str
@@ -25,10 +25,11 @@ module RMTools
25
25
  def format_trace(a)
26
26
  bt, steps, i = [], [], 0
27
27
  m = a[0].parse:caller
28
- m.line -= 1 if m and m.file =~ /\.haml$/
28
+ # seems like that bug is fixed for now
29
+ #m.line -= 1 if m and m.file =~ /\.haml$/
29
30
  while i < a.size
30
31
  m2 = a[i+1] && a[i+1].parse(:caller)
31
- m2.line -= 1 if m2 and m2.file =~ /\.haml$/
32
+ #m2.line -= 1 if m2 and m2.file =~ /\.haml$/
32
33
  if !m or m.path =~ IgnoreFiles
33
34
  nil
34
35
  else
@@ -2,8 +2,11 @@
2
2
  RMTools::require 'dev/trace_format'
3
3
  require 'active_support/core_ext/class/attribute'
4
4
 
5
- # as for rmtools-1.1.0, 1.9 may hung up processing IO while generating traceback
6
- #if RUBY_VERSION < '1.9'
5
+ # As for rmtools-1.1.0, 1.9.1 may hung up processing IO while generating traceback
6
+ # As for 1.2.10 with 1.9.3 with readline support it isn't hung up anymore
7
+ # Still it's not suitable for Rails, too many raises inside the engine slow all the wor in 5-10 times.
8
+ # I need a way to format backtrace only in case it is actually about to be printed onto log or console/
9
+ unless ENV['RAILS_ENV']
7
10
  class Exception
8
11
  alias :set_bt :set_backtrace
9
12
  class_attribute :__trace_format
@@ -35,4 +38,4 @@ require 'active_support/core_ext/class/attribute'
35
38
  class SystemStackError
36
39
  trace_format false
37
40
  end
38
- #end
41
+ end
@@ -159,6 +159,10 @@ class Array
159
159
  select {|e| e.__send__(key) == value}
160
160
  end
161
161
 
162
+ def reject_by(key, value)
163
+ reject {|e| e.__send__(key) == value}
164
+ end
165
+
162
166
  # concatenation
163
167
  # analogue to String#>>
164
168
  def >>(ary)
@@ -14,7 +14,7 @@ unless defined? RMTools::Iterators
14
14
  # => [[1, 2, 3], [3, 4, 6]]
15
15
  class Array
16
16
  alias :throw_no :method_missing
17
- RMTools::Iterators = %r{^(#{(instance_methods.grep(/_by$/)+%w{every no select reject partition find_all find sum foldr find_by select_by})*'|'})_([\w\d\_]+[!?]?)}
17
+ RMTools::Iterators = %r{^(#{(instance_methods.grep(/_by$/)+%w{every no select reject partition find_all find sum foldr find_by select_by reject_by})*'|'})_([\w\d\_]+[!?]?)}
18
18
 
19
19
  def method_missing(method, *args, &block)
20
20
  if match = (meth = method.to_s).match(RMTools::Iterators)
@@ -26,7 +26,7 @@ unless defined? RMTools::Iterators
26
26
  end
27
27
  return case iterator
28
28
  when :sum; sum(args.shift) {|i| i.__send__ meth, *args, &block}
29
- when :find_by; find_by(meth, *args)
29
+ when :find_by, :select_by, :reject_by; __send__(iterator, meth, *args)
30
30
  else __send__(iterator) {|i| i.__send__ meth, *args, &block}
31
31
  end
32
32
  rescue NoMethodError => e
@@ -7,6 +7,6 @@ module LibXML::XML
7
7
  Error.reset_handler
8
8
 
9
9
  class XPath::Object
10
- include Enumerable
10
+ include Enumerable
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,81 +1,92 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rmtools
3
- version: !ruby/object:Gem::Version
4
- hash: 11
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.11
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 2
9
- - 10
10
- version: 1.2.10
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Sergey Baev
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-08-01 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rake
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 49
29
- segments:
30
- - 0
31
- - 8
32
- - 7
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 0.8.7
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: activesupport
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
40
33
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 19
45
- segments:
46
- - 2
47
- - 3
48
- - 8
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
49
37
  version: 2.3.8
50
38
  type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: hoe
54
39
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.8
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.10'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
56
57
  none: false
57
- requirements:
58
+ requirements:
58
59
  - - ~>
59
- - !ruby/object:Gem::Version
60
- hash: 27
61
- segments:
62
- - 2
63
- - 12
64
- version: "2.12"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.10'
62
+ - !ruby/object:Gem::Dependency
63
+ name: hoe
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
65
70
  type: :development
66
- version_requirements: *id003
67
- description: Applied library primarily for debug and text/arrays/files processing purposes.
68
- email:
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ description: Applied library primarily for debug and text/arrays/files processing
79
+ purposes.
80
+ email:
69
81
  - tinbka@gmail.com
70
82
  executables: []
71
-
72
- extensions:
83
+ extensions:
73
84
  - ext/extconf.rb
74
- extra_rdoc_files:
85
+ extra_rdoc_files:
75
86
  - ./Manifest.txt
76
87
  - ./License.txt
77
88
  - ./README.txt
78
- files:
89
+ files:
79
90
  - ext/extconf.rb
80
91
  - ext/rmtools.h
81
92
  - ext/rmtools.cpp
@@ -173,39 +184,30 @@ files:
173
184
  - ./Manifest.txt
174
185
  - ./License.txt
175
186
  - ./README.txt
176
- homepage: http://github.com/tinbka
187
+ homepage: https://github.com/tinbka/rmtools
177
188
  licenses: []
178
-
179
189
  post_install_message:
180
- rdoc_options:
190
+ rdoc_options:
181
191
  - --main
182
192
  - README.txt
183
- require_paths:
193
+ require_paths:
184
194
  - lib
185
- required_ruby_version: !ruby/object:Gem::Requirement
195
+ required_ruby_version: !ruby/object:Gem::Requirement
186
196
  none: false
187
- requirements:
188
- - - ">="
189
- - !ruby/object:Gem::Version
190
- hash: 3
191
- segments:
192
- - 0
193
- version: "0"
194
- required_rubygems_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ! '>='
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
202
  none: false
196
- requirements:
197
- - - ">="
198
- - !ruby/object:Gem::Version
199
- hash: 3
200
- segments:
201
- - 0
202
- version: "0"
203
+ requirements:
204
+ - - ! '>='
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
203
207
  requirements: []
204
-
205
208
  rubyforge_project: rmtools
206
- rubygems_version: 1.8.17
209
+ rubygems_version: 1.8.24
207
210
  signing_key:
208
211
  specification_version: 3
209
212
  summary: Yet another Ruby applied lib
210
213
  test_files: []
211
-