glue 0.22.0 → 0.23.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.
@@ -1,85 +0,0 @@
1
- # * George Moschovitis <gm@navel.gr>
2
- # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: time.rb 182 2005-07-22 10:07:50Z gmosx $
4
-
5
- require 'time.rb'
6
-
7
- module Glue;
8
-
9
- # General time utilities collection
10
- #
11
- # Implement as a module to avoid class polution. You can
12
- # still Ruby's advanced features to include the module in your
13
- # class. Passing the object to act upon allows to check for nil,
14
- # which isn't possible if you use self.
15
- #
16
- # == TODO
17
- #
18
- # - SOS: add test units.
19
- # - add aliases for those methods in Kernel ?
20
-
21
- module TimeUtils
22
-
23
- NOW = Time.now
24
- NEVER = Time.mktime(2038)
25
- ZERO = Time.mktime(1972)
26
-
27
- # Convert the time to a nice String representation.
28
-
29
- def self.date_time(time)
30
- return nil unless time
31
- return time.strftime("%d-%m-%Y %H:%M")
32
- end
33
-
34
- # This method calculates the days extrema given two time objects.
35
- # start time is the given time1 at 00:00:00
36
- # end time is the given time2 at 23:59:59:999
37
- #
38
- # Input:
39
- # - the two times (if only time1 is provided then you get an extrema
40
- # of exactly one day extrema.
41
- #
42
- # Output
43
- # - the time range. you can get the start/end times using
44
- # range methods.
45
-
46
- def self.days_extrema(time1, time2=nil)
47
- time2 = time1 if (not time2.valid? Time)
48
- time2 = NEVER if (time2 <= time1)
49
- start_time = Time.self.start_of_day(time1)
50
- end_time = self.end_of_day(time2)
51
- return (start_time..end_time)
52
- end
53
-
54
- # Set time to start of day
55
-
56
- def self.start_of_day(time)
57
- return Time.mktime(time.year, time.month, time.day, 0, 0, 0, 0)
58
- end
59
-
60
-
61
- # Set time to end of day
62
-
63
- def self.end_of_day(time)
64
- return Time.mktime(time.year, time.month, time.day, 23, 59, 59, 999)
65
- end
66
-
67
-
68
- # Returns true only if day of time is included in the
69
- # range (stime..etime). Only year days are checked.
70
-
71
- def self.time_in_day_range(time, stime=ZERO, etime=NEVER)
72
- if (etime <= stime)
73
- Logger.debug "Invalid end time (#{etime} < #{stime})" if $DBG
74
- etime = NEVER
75
- end
76
-
77
- stime = start_of_day(stime)
78
- etime = end_of_day(etime)
79
-
80
- return (stime..etime).include?(time)
81
- end
82
-
83
- end
84
-
85
- end
@@ -1,53 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #--
3
- # Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
4
- # All rights reserved.
5
-
6
- # Permission is granted for use, copying, modification, distribution,
7
- # and distribution of modified versions of this work as long as the
8
- # above copyright notice is included.
9
- #++
10
-
11
- module Builder
12
-
13
- # BlankSlate provides an abstract base class with no predefined
14
- # methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
15
- # BlankSlate is useful as a base class when writing classes that
16
- # depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
17
- class BlankSlate
18
- class << self
19
- def hide(name)
20
- undef_method name if
21
- instance_methods.include?(name.to_s) and
22
- name !~ /^(__|instance_eval)/
23
- end
24
- end
25
-
26
- instance_methods.each { |m| hide(m) }
27
- end
28
- end
29
-
30
- # Since Ruby is very dynamic, methods added to the ancestors of
31
- # BlankSlate <em>after BlankSlate is defined</em> will show up in the
32
- # list of available BlankSlate methods. We handle this by defining a hook in the Object and Kernel classes that will hide any defined
33
- module Kernel
34
- class << self
35
- alias_method :blank_slate_method_added, :method_added
36
- def method_added(name)
37
- blank_slate_method_added(name)
38
- return if self != Kernel
39
- Builder::BlankSlate.hide(name)
40
- end
41
- end
42
- end
43
-
44
- class Object
45
- class << self
46
- alias_method :blank_slate_method_added, :method_added
47
- def method_added(name)
48
- blank_slate_method_added(name)
49
- return if self != Object
50
- Builder::BlankSlate.hide(name)
51
- end
52
- end
53
- end
@@ -1,36 +0,0 @@
1
- require "test/unit"
2
- require "glue/hash"
3
-
4
- class TestCaseGlueCache < Test::Unit::TestCase # :nodoc: all
5
- include Glue
6
-
7
- def setup
8
- @safe_cache = SafeHash.new(Hash.new)
9
- end
10
-
11
- def teardown
12
- @safe_cache = nil
13
- end
14
-
15
- def test_safe_cache
16
- assert_equal(nil, @safe_cache["no_item"])
17
- item1 = "item1"
18
- @safe_cache["key1"] = item1
19
- assert_equal(item1, @safe_cache["key1"])
20
-
21
- @safe_cache["rem1"] = item1
22
- @safe_cache.delete("rem1")
23
- assert_equal(nil, @safe_cache["rem1"])
24
-
25
- @safe_cache.clear()
26
- assert_equal(0, @safe_cache.size)
27
-
28
- @safe_cache["i1"] = "i1"
29
- @safe_cache["i2"] = "i1"
30
- @safe_cache["i3"] = "i1"
31
- assert_equal(3, @safe_cache.size)
32
-
33
- assert_equal(3, @safe_cache.values.size)
34
- end
35
-
36
- end
@@ -1,18 +0,0 @@
1
- require 'test/unit'
2
- require 'glue/number'
3
-
4
- class TestCaseNumberUtils < Test::Unit::TestCase # :nodoc: all
5
- include Glue
6
-
7
- def setup
8
- end
9
-
10
- def teardown
11
- end
12
-
13
- def test_ceil_multiple
14
- assert_equal(20, NumberUtils.ceil_multiple(15, 10))
15
- assert_equal(10, NumberUtils.ceil_multiple(1, 10))
16
- end
17
-
18
- end
@@ -1,102 +0,0 @@
1
-
2
- require 'test/unit'
3
- require 'glue/string'
4
-
5
- class TCStringUtilsUtils < Test::Unit::TestCase # :nodoc: all
6
- include Glue
7
-
8
- def setup
9
- @s = "this is any cool test"
10
- end
11
-
12
- def test_valid?
13
- assert_equal(true, StringUtils.valid?("test"))
14
- assert_equal(false, StringUtils.valid?(""))
15
- assert_equal(false, StringUtils.valid?(nil))
16
- end
17
-
18
- def test_head
19
- assert_equal(nil, StringUtils.head(nil))
20
- assert_equal("this...", StringUtils.head(@s, 3))
21
- assert_equal("this+++", StringUtils.head(@s, 3, false, "+++"))
22
- assert_equal("thi...", StringUtils.head(@s, 3, true))
23
- assert_equal("this is...", StringUtils.head(@s, 8))
24
- assert_equal(nil, StringUtils.head(@s, 0))
25
- assert_equal(nil, StringUtils.head(@s, 0, true))
26
- assert_equal("this is...", StringUtils.head(@s, 8, true))
27
- end
28
-
29
- def test_rewrite
30
- rules = [
31
- [/\/\$/, "../n1"],
32
- [/\$/, "../n1"],
33
- [/^games/, "../../games"],
34
- [/^tmp/, "/n/theseas/var/tmp"],
35
- ]
36
- string = StringUtils.rewrite("games/arkanoid.html", rules)
37
- assert_equal("../../games/arkanoid.html", string)
38
- string = StringUtils.rewrite("tmp/garbage.html", rules)
39
- assert_equal("/n/theseas/var/tmp/garbage.html", string)
40
- string = StringUtils.rewrite("root/index.html", rules)
41
- assert_equal("root/index.html", string)
42
- assert_equal(nil, StringUtils.rewrite(nil, rules))
43
- assert_equal("", StringUtils.rewrite("", rules))
44
-
45
- assert_raises(ArgumentError) {
46
- assert_equal("koko", StringUtils.rewrite("koko", nil))
47
- }
48
-
49
- # bug: should keep order
50
- s = StringUtils.rewrite("/$/koko.html", rules)
51
- assert_equal("../n1/koko.html", s)
52
- end
53
-
54
- def test_wrap
55
- s = "1234567890abcdefghijklmnopqrstu"
56
- r = "1234 5678 90ab cdef ghij klmn opqr stu"
57
- assert_equal(r, StringUtils::wrap(s, 4, " "))
58
-
59
- s = "111111111111111111111111111111111111111111111111"
60
- r = "1111111111 1111111111 1111111111 1111111111 11111111"
61
- assert_equal(r, StringUtils::wrap(s, 10, " "))
62
-
63
- s = "jackdaws love my big sphinx of quartz"
64
- r = "jackdaws love my big sphinx of quartz"
65
- assert_equal(r, StringUtils::wrap(s, 10, " "))
66
-
67
- s = "jackdaws love my big sphinx of quartz"
68
- r = "jack daws love my big sphi nx of quar tz"
69
- assert_equal(r, StringUtils::wrap(s, 4, " "))
70
-
71
- s = "jack.daws love my big sphinx of quartz"
72
- r = "jack .daw s love my big sphi nx of quar tz"
73
- assert_equal(r, StringUtils::wrap(s, 4, " "))
74
-
75
- assert_equal("", StringUtils::wrap("", 4, " "))
76
- assert_equal(nil, StringUtils::wrap(nil, 4, " "))
77
- end
78
- =begin
79
- def test_rationalize_filename
80
- filename = StringUtils.rationalize_filename("hello my friend!.gif")
81
- assert_equal("hello-my-friend.gif", filename)
82
- filename = StringUtils.rationalize_filename("���� ����.gif")
83
- assert_equal("pame-pali.gif", filename)
84
- filename = StringUtils.rationalize_filename("�� ���.gif")
85
- assert_equal("ti-les.gif", filename)
86
-
87
- # bug:
88
- filename = StringUtils.rationalize_filename("image-(10).gif")
89
- assert_equal("image-10.gif", filename)
90
- end
91
- =end
92
- def test_random_string
93
- s1 = StringUtils.random()
94
- s2 = StringUtils.random()
95
- assert_not_equal(s1, s2)
96
- assert(s1.size == s2.size)
97
- end
98
-
99
- def teardown
100
- @s = nil
101
- end
102
- end