HelperClasses 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e9d72b8fe43505cae6d07152f6bf45f9d94cec5
4
+ data.tar.gz: c8ec6b3f48a225602628f578122ac32f5bb7388a
5
+ SHA512:
6
+ metadata.gz: cdd44f845254cadfc4771dce2c205fdca28139550e0d1bf6d2393cb67fd0f03f12a3cca089ce8ff3e5f5f117cd82a16f2614cd4288504d621f1ead249ec733a0
7
+ data.tar.gz: 3404bd78e68baa95a8c991714c80a0f9326c50dd8624e40ea907e76fa5c79315a0a12368cb44321826cc3b0dac0d1060c092d35e16cc9f78e0e6928dcf83b84c
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == HelperClasses
2
+
3
+ GPLv3 or any later version
data/README ADDED
@@ -0,0 +1,78 @@
1
+ == HelperClasses
2
+
3
+ Some simple classes to help in everyday tasks
4
+
5
+ === DPuts
6
+
7
+ Ever wanted to have detailed debug-output, but in the end even PRODUCING the
8
+ string without printing it was too long? Here comes DPuts! It let's you define
9
+ different debug-levels and the strings are only evaluated if they're about
10
+ to be printed! Great for that 10MB-dump that you only want for debugging...
11
+
12
+ Usage:
13
+
14
+ ```
15
+ include HelperClasses::DPuts
16
+
17
+ DEBUG_LVL = 5
18
+ dputs(5){ "String with some #{huge_db.inspect}" }
19
+ ```
20
+
21
+ This will evaluate and print the ```huge_db``` variable, while
22
+
23
+ ```
24
+ include HelperClasses::DPuts
25
+
26
+ DEBUG_LVL = 4
27
+ dputs(5){ "String with some #{huge_db.inspect}" }
28
+ ```
29
+
30
+ will NOT evaluate it! The debug-levels are arbitrarily chosen like this:
31
+
32
+ 0 - Errors
33
+ 1 - Info and warnings, important
34
+ 2 - Often used debug-messages - limit of production-use
35
+ 3 - More detailed debugging
36
+ 4
37
+ 5 - Dumping lots of raw data
38
+
39
+ === Arraysym
40
+
41
+ to_sym and to_sym! - calls .to_sym on all elements. Usage:
42
+
43
+ ```
44
+ using HelperClasses::ArraySym
45
+
46
+ p [ "a", "b" ].to_sym
47
+ ```
48
+
49
+ Produces "[ :a, :b ]"
50
+
51
+ === HashAccessor
52
+
53
+ This should be standard ruby. Access all elements of an array by
54
+ prepending a "_".
55
+
56
+ ==== Getting a value
57
+
58
+ ```
59
+ using HelperClasses::HashAccessor
60
+
61
+ p { :a => 2, "a" => 3 }._a
62
+ ```
63
+
64
+ Will print "2". So symbols have precedence over string-keys.
65
+
66
+ ==== Setting a value
67
+
68
+ ```
69
+ using HelperClasses::HashAccessor
70
+
71
+ a = { :a => 2, "a" => 3 }
72
+ a._a = 4
73
+ a._b = 5
74
+
75
+ p a
76
+ ```
77
+
78
+ Will print ```{ :a => 4, "a" => 3, :b => "5" }```
@@ -0,0 +1,3 @@
1
+ require 'helperclasses/hashaccessor'
2
+ require 'helperclasses/arraysym'
3
+ require 'helperclasses/dputs'
@@ -0,0 +1,25 @@
1
+
2
+ module HelperClasses
3
+ module ArraySym
4
+ refine Array do
5
+ # Comptaibility for Ruby < 1.9
6
+ if ! Array.respond_to? :to_h
7
+ def to_h
8
+ Hash[ *self ]
9
+ end
10
+ end
11
+
12
+ def to_sym
13
+ collect{|v| v.to_sym }
14
+ end
15
+
16
+ def to_sym!
17
+ self.replace( to_sym() )
18
+ end
19
+
20
+ def to_s
21
+ "[#{join(",")}]"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,86 @@
1
+ module HelperClasses
2
+ require 'thread'
3
+ require 'singleton'
4
+
5
+ module DPuts
6
+ extend self
7
+ attr_accessor :mutex, :silent, :show_time, :terminal_width, :log_file
8
+
9
+ @mutex = Mutex.new
10
+ @silent = false
11
+ @show_time = 60
12
+ @terminal_width = 160
13
+ @log_file = false
14
+
15
+ def dputs_out( n, s, call )
16
+ return if DPuts.silent
17
+ if precision = DPuts.show_time
18
+ $dputs_time ||= Time.now - 120
19
+ now = Time.now
20
+ show = false
21
+ case precision
22
+ when /sec/
23
+ show = now.to_i != $dputs_time.to_i
24
+ when /min/
25
+ show = ( now.to_i / 60 ).floor != ( $dputs_time.to_i / 60 ).floor
26
+ end
27
+ show and puts "\n *** It is now: " +
28
+ Time.now.strftime( "%Y-%m-%d %H:%M:%S" )
29
+ $dputs_time = now
30
+ end
31
+ DPuts.mutex.synchronize do
32
+ width = DPuts.terminal_width
33
+ width -= 30.0
34
+ file, func = call.split(" ")
35
+ file = file[/^.*\/([^.]*)/, 1]
36
+ who = ( ":" + n.to_s + ":" + file.to_s +
37
+ func.to_s ).ljust(30, [ "X","x","*","-","."," "][n])
38
+ lines = []
39
+ pos = 0
40
+ while ( pos < s.length )
41
+ len = width
42
+ if s.length - pos > width
43
+ len = s.rindex( /[, .;=&>]/, pos + width )
44
+ len and len = len - pos + 1
45
+ if len < width / 2
46
+ len = width
47
+ end
48
+ end
49
+ lines.push s.slice( pos, len )
50
+ pos += len
51
+ end
52
+ puts who + " " + lines.shift.to_s
53
+ lines.each{|l|
54
+ puts " " * ( 32 ) + l
55
+ }
56
+ end
57
+ end
58
+
59
+ def dputs(n, &s)
60
+ if DEBUG_LVL >= n
61
+ s = yield s
62
+ dputs_out( n, s, caller(0)[1] )
63
+ end
64
+ end
65
+
66
+ def ddputs( n, &s )
67
+ s = yield s
68
+ dputs_out( -n, s, caller(0)[1] )
69
+ end
70
+
71
+ def dp( s )
72
+ dputs_out( 0, s.class == String ? s : s.inspect, caller(0)[1] )
73
+ s
74
+ end
75
+
76
+ def log_msg( mod, msg )
77
+ dputs( 1 ){ "Info from #{mod}: #{msg}" }
78
+ return if not DPuts.log_file
79
+ File.open( DPuts.log_file, "a" ){ |f|
80
+ str = Time.now.strftime( "%a %y.%m.%d-%H:%M:%S #{mod}: #{msg}" )
81
+ f.puts str
82
+ }
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,41 @@
1
+ module HelperClasses
2
+ module HashAccessor
3
+ refine Hash do
4
+ # Converts all keys of a hash to syms recursively
5
+ def to_sym
6
+ ret = {}
7
+ each{|k,v|
8
+ ret[ k.to_sym ] = v.class == Hash ? v.to_sym : v
9
+ }
10
+ ret
11
+ end
12
+
13
+ def to_sym!
14
+ self.replace( to_sym() )
15
+ end
16
+
17
+ def method_missing( s, *args )
18
+ case s.to_s
19
+ when "to_ary"
20
+ super( s, args )
21
+ when /^_.*[^=]$/
22
+ key = s.to_s.sub(/^_{1,2}/, '').to_sym
23
+ self.has_key? key and return self[key]
24
+ self.has_key? key.to_s and return self[key.to_s]
25
+ if s.to_s =~ /^__/
26
+ return self[key] = {}
27
+ else
28
+ return nil
29
+ end
30
+ when /^_.*=$/
31
+ key = /^_{1,2}(.*)=$/.match(s.to_s)[1].to_sym
32
+ self.has_key? key and return self[key] = args[0]
33
+ self.has_key? key.to_s and return self[key.to_s] = args[0]
34
+ return self[key] = args[0]
35
+ else
36
+ super( s, args )
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ require 'helperclasses/arraysym'
2
+
3
+ using HelperClasses::ArraySym
4
+
5
+ p ["a", "b"].to_sym
@@ -0,0 +1,12 @@
1
+ require "helperclasses/dputs"
2
+
3
+ DEBUG_LVL = 5
4
+
5
+ include HelperClasses
6
+
7
+ DPuts.dputs(5){"Hello"}
8
+
9
+ include DPuts
10
+
11
+ dputs( 5 ){"Hello there"}
12
+
@@ -0,0 +1,9 @@
1
+ require 'helperclasses/hashaccessor'
2
+
3
+ using HelperClasses::HashAccessor
4
+
5
+ a = { :a => 3, "b" => 4 }
6
+
7
+ p a.to_sym
8
+ p a._a
9
+ p a._b
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: HelperClasses
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Linus Gasser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Array.to_sym, Hash-accessors and debug-outputs with lazy evaluation
14
+ email: ineiti@linusetviviane.ch
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - "./LICENSE"
20
+ - "./README"
21
+ - "./lib/helperclasses.rb"
22
+ - "./lib/helperclasses/arraysym.rb"
23
+ - "./lib/helperclasses/dputs.rb"
24
+ - "./lib/helperclasses/hashaccessor.rb"
25
+ - "./test/test_arraysym.rb"
26
+ - "./test/test_dputs.rb"
27
+ - "./test/test_hashaccessor.rb"
28
+ - LICENSE
29
+ - README
30
+ homepage: https://github.com/ineiti/HelperClasses
31
+ licenses:
32
+ - GPLv3
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.2.0
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Helpers for Array, Hash and debug-output
54
+ test_files: []