splib 1.0 → 1.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.
data/CHANGELOG CHANGED
@@ -1,2 +1,6 @@
1
+ 1.1
2
+ * Added Splib.type_of? method to the Constants library
3
+ * Splib.find_const returns nil if constant not found
4
+ * Added Splib.find_const to the Constants library
1
5
  1.0
2
6
  * Initial release
data/README.rdoc CHANGED
@@ -19,14 +19,114 @@ The Spox Library is collection of helper methods and classes.
19
19
 
20
20
  === Usage
21
21
 
22
- The Spox Library has various things located within it. The Splib#load method will allow you to load individual parts of the library, or the entire thing into your program.
22
+ The Spox Library has various things located within it. The Splib#load method will allow you to load individual parts of the library, or the entire thing into your program. Lets take a quick look at some of the things available from this library.
23
+
24
+ ==== URL Shorteners
25
+
26
+ Access to a variety of URL shortening services
23
27
 
24
28
  require 'splib'
25
29
 
26
30
  Splib.load :UrlShorteners
27
- puts Splib.tinyurl 'www.google.com'
31
+ puts Splib.tiny_url 'www.google.com'
32
+ puts Splib.trim_url 'www.google.com'
33
+ puts Splib.isgd_url 'www.google.com'
34
+ puts Splib.shortest_url 'www.google.com'
35
+
36
+ Results:
37
+
38
+ http://tinyurl.com/2ty
39
+ http://tr.im/Ig5f
40
+ http://is.gd/5wmJ1
41
+ http://tr.im/Ig5p
42
+
43
+ ==== Conversions
44
+
45
+ Easy conversion for seconds and bytes to something more human readable
46
+
47
+ require 'splib'
48
+
49
+ Splib.load :Conversions
50
+ puts Splib.format_seconds 9999999
51
+ puts Splib.format_size 9999999999
52
+
53
+ Results:
54
+
55
+ 3 months 3 weeks 1 day 17 hours 46 minutes 39 seconds
56
+ 9.31 Gigabytes
57
+
58
+ ==== Exec
59
+
60
+ Adding a bit of safety and ease of use to exec
61
+
62
+ require 'splib'
63
+
64
+ Splib.load :Exec
65
+ begin
66
+ Splib.exec('echo test', 10, 1)
67
+ rescue IOError
68
+ puts 'Exceeded allowed number of returned bytes'
69
+ end
70
+ begin
71
+ Splib.exec('while [ true ]; do true; done;', 1)
72
+ rescue Timeout::Error
73
+ puts 'Exceeded allowed running time'
74
+ end
75
+ puts Splib.exec('echo "hello world"')
76
+
77
+ Results:
78
+
79
+ Exceeded allowed number of returned bytes
80
+ Exceeded allowed running time
81
+ hello world
82
+
83
+ ==== Constants
84
+
85
+ Find constants easily, especially within loaded modules
86
+
87
+ require 'splib'
88
+
89
+ Splib.load :Constants
90
+ mod = Module.new
91
+ mod.class_eval("
92
+ module Fu
93
+ class Bar
94
+ end
95
+ end"
96
+ )
97
+ p Splib.find_const('String')
98
+ p Splib.find_const('Fu::Bar', [mod])
99
+
100
+ Results:
101
+
102
+ String
103
+ #<Module:0x95f02a4>::Fu::Bar
104
+
105
+ ==== PriorityQueue
106
+
107
+ Add some logic to item queueing
108
+
109
+ require 'splib'
110
+
111
+ Splib.load :PriorityQueue
112
+
113
+ queue = Splib::PriorityQueue.new{|s| s == :last }
114
+ queue.push(:last, 'last')
115
+ 2.times{ queue.push(:slot1, 'test') }
116
+ 2.times{ queue.push(:slot2, 'fubar') }
117
+ until(queue.empty?)
118
+ puts queue.pop
119
+ end
120
+
121
+ Results:
122
+
123
+ test
124
+ fubar
125
+ test
126
+ fubar
127
+ last
28
128
 
29
- => "http://tinyurl.com/2ty"
129
+ ==== TODO: Write examples for CodeReloader and RandomIterator
30
130
 
31
131
  == Last remarks
32
132
 
@@ -3,6 +3,7 @@ module Splib
3
3
  # things:: Array of Object/Module constants to look in
4
4
  # Finds a constant if it exists
5
5
  # Example:: Foo::Bar
6
+ # Returns nil if nothing found
6
7
  def self.find_const(c, things=[])
7
8
  raise ArgumentError.new('Exepcting an array') unless things.is_a?(Array)
8
9
  const = nil
@@ -16,6 +17,28 @@ module Splib
16
17
  end
17
18
  break unless const.nil?
18
19
  end
19
- const.nil? ? c : const
20
+ const
21
+ end
22
+
23
+ # a:: an object
24
+ # b:: constant or string
25
+ # Returns true of a is a type of b. b can be given as a String
26
+ # to allow for matching of types contained within a module or
27
+ # for types that may not be loaded
28
+ def self.type_of?(a, b)
29
+ case b
30
+ when String
31
+ if(a.class.to_s.slice(0) == '#')
32
+ name = a.class.to_s
33
+ return name.slice(name.index('::')+2, name.length) == b
34
+ else
35
+ const = self.find_const(b)
36
+ return const.nil? ? false : a.is_a?(const)
37
+ end
38
+ when Class
39
+ return a.is_a?(b)
40
+ else
41
+ raise ArgumentError.new('Comparision type must be a string or constant')
42
+ end
20
43
  end
21
44
  end
data/splib.gemspec CHANGED
@@ -2,7 +2,7 @@ spec = Gem::Specification.new do |s|
2
2
  s.name = 'splib'
3
3
  s.author = 'spox'
4
4
  s.email = 'spox@modspox.com'
5
- s.version = '1.0'
5
+ s.version = '1.1'
6
6
  s.summary = 'Spox Library'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.files = Dir['**/*']
@@ -24,4 +24,20 @@ class ConstantsTest < Test::Unit::TestCase
24
24
  assert_equal(Foo::Bar::Fubar, Splib.find_const('Foo::Bar::Fubar'))
25
25
  assert_match(/<.+?>::Fu::Bar/, Splib.find_const('Fu::Bar', [mod]).to_s)
26
26
  end
27
+ def test_type_of?
28
+ mod = Module.new
29
+ mod.class_eval("
30
+ module Fu
31
+ class Bar
32
+ end
33
+ end"
34
+ )
35
+ assert(Splib.type_of?('test', 'String'))
36
+ assert(Splib.type_of?('test', String))
37
+ assert(Splib.type_of?('test', 'Object'))
38
+ assert(Splib.type_of?('test', Object))
39
+ fubar = Splib.find_const('Fu::Bar', [mod]).new
40
+ assert(Splib.type_of?(fubar, 'Fu::Bar'))
41
+ assert(Splib.type_of?(fubar, Object))
42
+ end
27
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splib
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.0"
4
+ version: "1.1"
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-17 00:00:00 -08:00
12
+ date: 2009-12-30 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15