as-extensions 0.1.0

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.
@@ -0,0 +1,52 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Moodstocks SAS
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # or in the LICENSE file which you should have received as part of
11
+ # this distribution.
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ module ASE
21
+
22
+ # Initialize logger
23
+ LOGGER = Logger.new(STDERR)
24
+ LOGGER.level = Logger::WARN
25
+
26
+ class << self
27
+
28
+ # Print to the logs. Exit if logging a fatal error.
29
+ # Level can be one of: :debug, :info, :warn, :error, :fatal.
30
+ # Returns nil.
31
+ def log(s, level=(:warn))
32
+ LOGGER.send(level, s)
33
+ exit! if level == :fatal
34
+ nil
35
+ end
36
+
37
+ def log_level=(level=(:warn))
38
+ LOGGER.level = level.is_a?(Symbol) ? Logger.const_get(level.to_s.upcase) : level
39
+ end
40
+
41
+ # Run a block with logging disabled
42
+ def silently
43
+ old_level = LOGGER.level
44
+ LOGGER.level = Logger::FATAL
45
+ ret = yield
46
+ LOGGER.level = old_level
47
+ ret
48
+ end
49
+
50
+ end # class << self
51
+
52
+ end
@@ -0,0 +1,47 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Moodstocks SAS
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # or in the LICENSE file which you should have received as part of
11
+ # this distribution.
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ module ASE
21
+
22
+ # To store gems already need()ed
23
+ NEEDED = {}
24
+
25
+ class << self
26
+
27
+ # Require a gem / gems on the fly.
28
+ # Raises an exception if the load fails.
29
+ def need(ext)
30
+ if ext.is_a?(::Array)
31
+ ext.each do |e| need(e) end
32
+ else
33
+ return true if NEEDED[ext]
34
+ begin
35
+ require ext
36
+ rescue Exception
37
+ ASE::log("Couldn't load \"#{ext}\".", :error)
38
+ raise
39
+ end
40
+ load_extra_ext(ext)
41
+ NEEDED[ext] = true
42
+ end
43
+ end
44
+
45
+ end # class << self
46
+
47
+ end
@@ -0,0 +1,38 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Moodstocks SAS
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # or in the LICENSE file which you should have received as part of
11
+ # this distribution.
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ module ASE
21
+
22
+ class << self
23
+
24
+ # Download a file to a path and return it
25
+ def download(uri, path)
26
+ File.open(path, "wb") do |o_f|
27
+ open(uri) do |i_f|
28
+ while (blk = i_f.read(65536))
29
+ o_f.write(blk)
30
+ end
31
+ end
32
+ end
33
+ path
34
+ end
35
+
36
+ end # class << self
37
+
38
+ end
@@ -0,0 +1,159 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Moodstocks SAS
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # or in the LICENSE file which you should have received as part of
11
+ # this distribution.
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ # Simple test methods for as-extensions
21
+ # Their main advantage over unit/test
22
+ # is that they can run tests in isolation.
23
+
24
+ module ASE module Test
25
+
26
+ CONFIG = { :verbose => (ARGV.include?('verbose')),
27
+ :fatal => (ARGV.include?('fatal')),
28
+ :nb_ok => 0,
29
+ :nb_nok => 0,
30
+ :mute_stats => false }
31
+
32
+ class << self
33
+
34
+ def assert(success=nil, msg=nil)
35
+ if success
36
+ CONFIG[:nb_ok] += 1
37
+ debug "Test \##{CONFIG[:nb_ok]+CONFIG[:nb_nok]} OK"
38
+ return true
39
+ else
40
+ CONFIG[:nb_nok] += 1
41
+ debug "=== TEST NOT OK ==="
42
+ debug caller.join("\n")
43
+ puts "ERROR ==> #{msg}" if msg
44
+ raise "test failure" if CONFIG[:fatal]
45
+ return false
46
+ end
47
+ end
48
+
49
+ def assert_blank(x='a', msg=nil)
50
+ assert x.blank?, msg
51
+ end
52
+
53
+ def assert_boolean(b=nil, msg=nil)
54
+ assert (b == true || b == false), msg_stack(msg, "Expected a boolean, found a #{b.class}.")
55
+ end
56
+
57
+ def assert_empty(x=1, msg=nil)
58
+ assert x.empty?, msg
59
+ end
60
+
61
+ def assert_equal(a, b, msg=nil)
62
+ assert a == b, msg_stack(msg, "#{b.inspect} found, expected #{a.inspect}")
63
+ end
64
+
65
+ def assert_fail(msg=nil)
66
+ assert false, msg
67
+ end
68
+
69
+ def assert_false(b=nil, msg=nil)
70
+ assert b == false, msg
71
+ end
72
+
73
+ def assert_include(a, b, msg=nil)
74
+ assert b.include?(a), msg_stack(msg, "#{b.inspect} does not include #{a.inspect}")
75
+ end
76
+
77
+ def assert_instance_of(k=nil, x=nil, msg=nil)
78
+ assert x.instance_of?(k), msg
79
+ end
80
+
81
+ def assert_kind_of(k=nil, x=nil, msg=nil)
82
+ assert x.kind_of?(k), msg
83
+ end
84
+
85
+ def assert_looksame(a, b, msg=nil)
86
+ assert_equal(a.inspect, b.inspect, msg)
87
+ end
88
+
89
+ def assert_nil(x=1, msg=nil)
90
+ assert x.nil?, msg
91
+ end
92
+
93
+ def assert_not_blank(x='', msg=nil)
94
+ assert !x.blank?, msg
95
+ end
96
+
97
+ def assert_not_empty(x=[], msg=nil)
98
+ assert !x.empty?, msg
99
+ end
100
+
101
+ def assert_not_equal(a, b, msg=nil)
102
+ assert a != b, msg_stack(msg, "#{b.inspect} and #{a.inspect} are equal")
103
+ end
104
+
105
+ def assert_not_include(a, b, msg=nil)
106
+ assert !b.include?(a), msg_stack(msg, "#{b.inspect} includes #{a.inspect}")
107
+ end
108
+
109
+ def assert_not_nil(x=nil, msg=nil)
110
+ assert !x.nil?, msg
111
+ end
112
+
113
+ def assert_raise(kind=Exception, msg=nil, &blk)
114
+ begin
115
+ yield
116
+ rescue Exception => e
117
+ assert_kind_of kind, e, "Expected #{kind.to_s} but #{e.to_s} was raised instead"
118
+ return true
119
+ end
120
+ assert_fail msg_stack(msg, 'No exception raised.')
121
+ end
122
+
123
+ def assert_true(b=nil, msg=nil)
124
+ assert b == true, msg
125
+ end
126
+
127
+ def debug(s)
128
+ puts s if CONFIG[:verbose]
129
+ end
130
+
131
+ def isolate(msg=nil, &blk)
132
+ fork do
133
+ yield
134
+ exit!(47) if Test::CONFIG[:nb_nok] > 0 # propagate failure
135
+ end
136
+ child_pid = Process.wait
137
+ msg = msg.is_a?(::String) ? "Failure in isolation jail \"#{msg}\"" : nil
138
+ assert_equal 0, $?.exitstatus, msg_stack(msg, "Process in isolation returned #{$?.exitstatus}")
139
+ end
140
+
141
+ def msg_stack(msg1=nil, msg2=nil)
142
+ if msg1
143
+ if msg2 then "#{msg1}\nERROR ==> #{msg2}"
144
+ else msg1 end
145
+ elsif msg2 then msg2
146
+ else nil end
147
+ end
148
+
149
+ def stats(mute=nil)
150
+ CONFIG[:mute_stats] = mute if !mute.nil?
151
+ return if CONFIG[:mute_stats]
152
+ puts "=== TEST STATS ==="
153
+ puts "OK: #{CONFIG[:nb_ok]}"
154
+ puts "NOK: #{CONFIG[:nb_nok]}"
155
+ end
156
+
157
+ end # class << self
158
+
159
+ end end
@@ -0,0 +1,67 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Moodstocks SAS
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # or in the LICENSE file which you should have received as part of
11
+ # this distribution.
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ module ASE
21
+
22
+ # Ruby Time classes are a mess. This module attempts to restore
23
+ # order by making bold choices.
24
+ # It provides a standard string format and always uses UTC and Epoch.
25
+ module Time
26
+
27
+ DATE_FORMAT = "%Y-%m-%dT%H:%M:%S"
28
+
29
+ class << self
30
+
31
+ def now(type=nil)
32
+ case type
33
+ when :string; 0.seconds.ago.strftime(DATE_FORMAT)
34
+ else 0.seconds.ago
35
+ end
36
+ end
37
+
38
+ # Convert various time classes to string
39
+ def to_string(t)
40
+ t.respond_to?(:getutc) ? t.getutc.strftime(DATE_FORMAT) : t.strftime(DATE_FORMAT)
41
+ end
42
+
43
+ # Convert an ASE time string to a DateTime
44
+ def from_string(s)
45
+ ::Time.parse("#{s}Z")
46
+ end
47
+
48
+ # Force conversion of a DateTime to a Time
49
+ def to_time(t)
50
+ if t.is_a?(::Time) then t
51
+ else from_string(to_string(t)) end
52
+ end
53
+
54
+ # Converts various times to a number of seconds since Epoch
55
+ def epoch(t)
56
+ if t.nil? then epoch(now)
57
+ elsif ( t.is_a?(::Time) || t.is_a?(::Numeric) ) then t.to_i
58
+ elsif t.is_a?(::DateTime) then epoch(to_time(t))
59
+ elsif t.is_a?(::String) then epoch(from_string(t))
60
+ else nil end
61
+ end
62
+
63
+ end # class << self
64
+
65
+ end # module Time
66
+
67
+ end
@@ -0,0 +1,64 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Moodstocks SAS
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # or in the LICENSE file which you should have received as part of
11
+ # this distribution.
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ # Force KCODE to UTF-8, a sane default
21
+ $KCODE = "U" if (RUBY_VERSION < "1.9") && ($KCODE == "NONE")
22
+
23
+ module ASE
24
+
25
+ class << self
26
+
27
+ # Require a part / parts of a library
28
+ def require_part(part, dir=nil)
29
+ orig = caller(1).first.split(':').first
30
+ dir ||= File.join( File.dirname(orig), File.basename(orig, ".rb") )
31
+ if part.is_a?(::Array)
32
+ part.each do |p| require_part(p, dir) end
33
+ else
34
+ require File.join(dir, part)
35
+ end
36
+ end
37
+
38
+ # Require an extension / extensions of a library
39
+ def require_ext(ext, dir=nil)
40
+ orig = caller(1).first.split(':').first
41
+ dir ||= File.join( File.dirname(File.dirname(orig)), 'ext' )
42
+ if ext.is_a?(::Array)
43
+ ext.each do |e| require_ext(e, dir) end
44
+ else
45
+ require File.join(dir, ext)
46
+ end
47
+ end
48
+
49
+ end # class << self
50
+
51
+ # This loading order (extra-ext, need, logger, log) is necessary to bootstrap need()
52
+ require_part %w{ extra-ext need }
53
+ need 'logger'
54
+ require_part 'log'
55
+
56
+ # Now we can load everything normally
57
+ need %w{ active_support babosa fileutils map open-uri pathname uri set socket }
58
+ need %w{ active_support/core_ext }
59
+ require_part %w{ fs enum deep net test time ext-logic }
60
+ require_ext %w{ array datetime dir enumerable file hash io object set socket string symbol time uri }
61
+
62
+ end
63
+
64
+ ActiveSupport::const_set('Extension', ASE) unless ActiveSupport::const_defined?('Extension')
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: as-extensions
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Pierre Chapuis
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-23 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: babosa
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: i18n
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: map
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :runtime
71
+ version_requirements: *id004
72
+ description: This gem expends Rails' ActiveSupport
73
+ email: contact@moodstocks.com
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ extra_rdoc_files:
79
+ - LICENSE
80
+ - README.md
81
+ files:
82
+ - ext/array.rb
83
+ - ext/boolean.rb
84
+ - ext/datetime.rb
85
+ - ext/dir.rb
86
+ - ext/enumerable.rb
87
+ - ext/file.rb
88
+ - ext/hash.rb
89
+ - ext/io.rb
90
+ - ext/object.rb
91
+ - ext/set.rb
92
+ - ext/socket.rb
93
+ - ext/string.rb
94
+ - ext/symbol.rb
95
+ - ext/time.rb
96
+ - ext/uri.rb
97
+ - lib/as-extensions.rb
98
+ - lib/as-extensions/deep.rb
99
+ - lib/as-extensions/enum.rb
100
+ - lib/as-extensions/ext-logic.rb
101
+ - lib/as-extensions/ext-logic/io.rb
102
+ - lib/as-extensions/ext-logic/string.rb
103
+ - lib/as-extensions/extra-ext.rb
104
+ - lib/as-extensions/extra-ext/libxml.rb
105
+ - lib/as-extensions/fs.rb
106
+ - lib/as-extensions/log.rb
107
+ - lib/as-extensions/need.rb
108
+ - lib/as-extensions/net.rb
109
+ - lib/as-extensions/test.rb
110
+ - lib/as-extensions/time.rb
111
+ - LICENSE
112
+ - README.md
113
+ has_rdoc: true
114
+ homepage: http://moodstocks.com
115
+ licenses: []
116
+
117
+ post_install_message:
118
+ rdoc_options: []
119
+
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project:
141
+ rubygems_version: 1.3.7
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Useful extensions for ActiveSupport
145
+ test_files: []
146
+