knjrbfw 0.0.30 → 0.0.31

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,17 +1,27 @@
1
1
  #This file can be symlinked and thereby easily translated by POEdit. Further more the static methods can be used for stuff.
2
2
  module Knj::Locales
3
+ #Returns an array of translated day-names ('_'-method must exist!).
4
+ #===Examples
5
+ # Knj::Locales.days_arr #=> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
3
6
  def self.days_arr
4
7
  return [_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")]
5
8
  end
6
9
 
10
+ #Returns an array of short translated day-names.
11
+ #===Examples
12
+ # Knj::Locales.days_short_arr #=> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
7
13
  def self.days_short_arr
8
14
  return [_("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun")]
9
15
  end
10
16
 
17
+ #Returns an array of translated month-names.
18
+ #===Examples
19
+ # Knj::Locales.months_arr #=> ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
11
20
  def self.months_arr
12
21
  return [_("January"), _("February"), _("March"), _("April"), _("May"), _("June"), _("July"), _("August"), _("September"), _("October"), _("November"), _("December")]
13
22
  end
14
23
 
24
+ #Returns an array of translate 'ago'-strings. View the source to see which.
15
25
  def self.ago_strings
16
26
  return {
17
27
  :year_ago_str => _("%s year ago"),
@@ -1,5 +1,8 @@
1
+ #This module can be used to handel various language-stuff.
1
2
  module Knj::Locales
2
3
  #Returns the primary locale, secondary locale and the two put together.
4
+ #===Examples
5
+ # Knj::Locales.lang #=> {"first" => "en", "second" => "GB", "full" => "en_GB"}
3
6
  def self.lang
4
7
  match = self.locale.to_s.match(/^([a-z]{2})_([A-Z]{2})/)
5
8
  raise "Could not understand language: #{self.locale}." if !match
@@ -12,6 +15,8 @@ module Knj::Locales
12
15
  end
13
16
 
14
17
  #Returns various localized information about the environment.
18
+ #===Examples
19
+ # Knj::Locales.localeconv #=> {"decimal_point" => ".", "thousands_sep" => ",", "csv_delimiter" => ","}
15
20
  def self.localeconv
16
21
  f = Knj::Locales.lang["first"]
17
22
 
@@ -38,18 +43,25 @@ module Knj::Locales
38
43
  end
39
44
 
40
45
  #Returns a float from the formatted string according to the current locale.
46
+ #===Examples
47
+ # Knj::Locales.number_in("123,456.68") #=> 123456.68
41
48
  def self.number_in(num_str)
42
49
  lc = Knj::Locales.localeconv
43
50
  return num_str.to_s.gsub(lc["thousands_sep"], "").gsub(lc["decimal_point"], ".").to_f
44
51
  end
45
52
 
46
53
  #Returns the given number as a formatted string according to the current locale.
54
+ #===Examples
55
+ # Knj::Locales.number_out(123456.68) #=> "123,456.68"
47
56
  def self.number_out(num_str, dec = 2)
48
57
  lc = Knj::Locales.localeconv
49
58
  return Knj::Php.number_format(num_str, dec, lc["decimal_point"], lc["thousands_sep"])
50
59
  end
51
60
 
52
- #Returns the current locale for the current environment.
61
+ #Returns the current locale for the current environment (_session[:locale] or Thread.current[:locale]).
62
+ #===Examples
63
+ # Knj::Locales.locale #=> 'en_GB'
64
+ # Knj::Locales.locale #=> 'da_DK'
53
65
  def self.locale
54
66
  begin
55
67
  return _session[:locale] if _session[:locale].to_s.strip.length > 0
@@ -1,8 +1,11 @@
1
+ #This module is used to handel various random handeling.
1
2
  module Knj::Rand
3
+ #Returns a random integer between the two integers given.
2
4
  def self.range(first, last)
3
5
  return first.to_i + rand(last.to_i - first.to_i)
4
6
  end
5
7
 
8
+ #Returns a random element in the given array.
6
9
  def self.array(arr)
7
10
  key = rand(arr.length)
8
11
  return arr[key]
@@ -1,10 +1,13 @@
1
1
  #encoding: utf-8
2
2
 
3
+ #This module contains various methods to escape, change or treat strings.
3
4
  module Knj::Strings
5
+ #Returns a string that is safe to use on the command line.
4
6
  def self.UnixSafe(tha_string)
5
7
  return tha_string.to_s.gsub(" ", "\\ ").gsub("&", "\&").gsub("(", "\\(").gsub(")", "\\)").gsub('"', '\"').gsub("\n", "\"\n\"").gsub(":", "\\:").gsub('\'', "\\\\'").gsub("`", "\\\\`")
6
8
  end
7
9
 
10
+ #Alias for UnixSafe.
8
11
  def self.unixsafe(string)
9
12
  return Knj::Strings.UnixSafe(string)
10
13
  end
@@ -125,14 +128,21 @@ module Knj::Strings
125
128
  end
126
129
 
127
130
  #Shortens a string to maxlength and adds "..." if it was shortened.
131
+ #===Examples
132
+ # Knj::Strings.shorten("Kasper Johansen", 6) #=> "Kasper..."
128
133
  def self.shorten(str, maxlength)
129
134
  str = str.to_s
130
135
  str = str.slice(0..(maxlength - 1)).strip + "..." if str.length > maxlength
131
136
  return str
132
137
  end
133
138
 
139
+ #Search for what looks like links in a string and does something with it based on block given or arguments given.
140
+ #===Examples
141
+ # str = "asd asd asd asdjklqwejqwer http://www.google.com asdfas df asf"
142
+ # Knj::Strings.html_links(str) #=> "asd asd asd asdjklqwejqwer <a href=\"http://www.google.com\">http://www.google.com</a> asdfas df asf"
143
+ # Knj::Strings.html_links(str){ |data| str.gsub(data[:match][0], "!!!#{data[:match][1]}!!!")} #=> "asd asd asd asdjklqwejqwer !!!www!!! asdfas df asf"
134
144
  def self.html_links(str, args = {})
135
- str.to_s.html.scan(/(http:\/\/([A-z]+)\S*\.([A-z]{2,4})(\S+))/) do |match|
145
+ Knj::Web.html(str).scan(/(http:\/\/([A-z]+)\S*\.([A-z]{2,4})(\S+))/) do |match|
136
146
  if block_given?
137
147
  str = yield(:str => str, :args => args, :match => match)
138
148
  else
@@ -150,8 +160,11 @@ module Knj::Strings
150
160
  return str
151
161
  end
152
162
 
163
+ #Strips various given characters from a given string.
164
+ #===Examples
165
+ # Knj::Strings.strip("...kasper...", {:right => false, :left => true, :strips => [".", ","]}) #=> "kasper..."
153
166
  def self.strip(origstr, args)
154
- newstr = "#{origstr}<br>"
167
+ newstr = "#{origstr}"
155
168
 
156
169
  if !args.key?(:right) or args[:right]
157
170
  loop do
@@ -114,10 +114,10 @@ class Knj::Threadhandler
114
114
  else
115
115
  #No free objects, but we can spawn a new one and use that...
116
116
  newobj = @spawn_new_block.call
117
- @objects << {
117
+ @objects << Knj::Threadsafe::Synced_hash.new.merge(
118
118
  :free => false,
119
119
  :object => newobj
120
- }
120
+ )
121
121
  STDOUT.print "Spawned db and locked new.\n" if @args[:debug]
122
122
  end
123
123
  end
@@ -105,7 +105,7 @@ module Knj::Threadsafe
105
105
  # arr << 5
106
106
  # ret = arr[0]
107
107
  class Synced_array < ::Array
108
- include Mutexed
108
+ include Monitored
109
109
  end
110
110
 
111
111
  #Predefined synchronized hash.
@@ -115,6 +115,6 @@ module Knj::Threadsafe
115
115
  # h['test'] = 'trala'
116
116
  # ret = h['test']
117
117
  class Synced_hash < ::Hash
118
- include Mutexed
118
+ include Monitored
119
119
  end
120
120
  end
@@ -1,5 +1,6 @@
1
1
  require "#{$knjpath}wref"
2
2
 
3
+ #This class handels various stuff regarding Unix-processes.
3
4
  class Knj::Unix_proc
4
5
  attr_reader :data
5
6
 
@@ -1,7 +1,25 @@
1
1
  #A simple weak-reference framework with mapping. Only handles the referencing of objects.
2
+ #===Examples
3
+ # user_obj = ob.get(:User, 1)
4
+ # weak_ref = Knj::Wref(user_obj)
5
+ # user_obj = nil
6
+ # sleep 0.5
7
+ # GC.start
8
+ #
9
+ # begin
10
+ # user_obj = weak_ref.get
11
+ # print "The user still exists in memory and has ID #{user.id}."
12
+ # rescue Knj::Wref::Recycled
13
+ # print "The user has been removed from memory."
14
+ # end
2
15
  class Knj::Wref
3
- attr_reader :class_name, :id
16
+ #Returns the classname of the object.
17
+ attr_reader :class_name
4
18
 
19
+ #Returns the object-ID which is used to look up the ObjectSpace (if not running JRuby).
20
+ attr_reader :id
21
+
22
+ #Initializes various variables.
5
23
  def initialize(obj)
6
24
  @id = obj.__id__
7
25
 
@@ -17,7 +35,13 @@ class Knj::Wref
17
35
  end
18
36
  end
19
37
 
20
- #Returns the object that this weak reference holds or throws Knj::Wref::Recycled.
38
+ #Returns the object that this weak reference holds or raises Knj::Wref::Recycled.
39
+ # begin
40
+ # obj = wref.get
41
+ # print "Object still exists in memory."
42
+ # rescue Knj::Wref::Recycled
43
+ # print "Object has been garbage-collected."
44
+ # end
21
45
  def get
22
46
  begin
23
47
  raise Knj::Wref::Recycled if !@class_name or !@id
@@ -54,6 +78,7 @@ class Knj::Wref
54
78
  end
55
79
 
56
80
  #Returns true if the reference is still alive.
81
+ # print "The object still exists in memory." if wref.alive?
57
82
  def alive?
58
83
  begin
59
84
  self.get
@@ -63,11 +88,28 @@ class Knj::Wref
63
88
  end
64
89
  end
65
90
 
66
- #Make Wref compatible with the normal WeakRef.
91
+ #Makes Wref compatible with the normal WeakRef.
67
92
  alias weakref_alive? alive?
68
93
  alias __getobj__ get
69
94
  end
70
95
 
96
+ #A weak hash-map.
97
+ #===Examples
98
+ # map = Knj::Wref_map.new
99
+ # map[1] = obj
100
+ # obj = nil
101
+ #
102
+ # sleep 0.5
103
+ #
104
+ # begin
105
+ # obj = map[1]
106
+ # print "Object still exists in memory."
107
+ # rescue Knj::Wref::Recycled
108
+ # print "Object has been garbage-collected."
109
+ # end
110
+ #
111
+ # obj = map.get!(1)
112
+ # print "Object still exists in memory." if obj
71
113
  class Knj::Wref_map
72
114
  def initialize(args = nil)
73
115
  @map = {}
@@ -93,6 +135,13 @@ class Knj::Wref_map
93
135
  end
94
136
 
95
137
  #Returns a object by ID or raises a RefError.
138
+ #===Examples
139
+ # begin
140
+ # obj = map[1]
141
+ # print "Object still exists in memory."
142
+ # rescue Knj::Wref::Recycled
143
+ # print "Object has been garbage-collected."
144
+ # end
96
145
  def get(id)
97
146
  begin
98
147
  @mutex.synchronize do
@@ -106,6 +155,9 @@ class Knj::Wref_map
106
155
  end
107
156
 
108
157
  #The same as 'get' but returns nil instead of WeakRef-error. This can be used to avoid writing lots of code.
158
+ #===Examples
159
+ # obj = map.get!(1)
160
+ # print "Object still exists in memory." if obj
109
161
  def get!(id)
110
162
  begin
111
163
  return self.get(id)
@@ -147,6 +199,8 @@ class Knj::Wref_map
147
199
  end
148
200
 
149
201
  #Returns true if the given key exists in the hash.
202
+ #===Examples
203
+ # print "Key exists but we dont know if the value has been garbage-collected." if map.key?(1)
150
204
  def key?(key)
151
205
  @mutex.synchronize do
152
206
  return @map.key?(key)
@@ -193,4 +247,5 @@ class Knj::Wref_map
193
247
  alias []= set
194
248
  end
195
249
 
250
+ #This error is raised when an object in a wref has been garbage-collected.
196
251
  class Knj::Wref::Recycled < RuntimeError; end
metadata CHANGED
@@ -1,91 +1,94 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: knjrbfw
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.30
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.0.31
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Kasper Johansen
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-03 00:00:00.000000000 +02:00
12
+
13
+ date: 2012-05-06 00:00:00 +02:00
13
14
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
16
17
  name: rspec
17
- requirement: &10501580 !ruby/object:Gem::Requirement
18
+ requirement: &id001 !ruby/object:Gem::Requirement
18
19
  none: false
19
- requirements:
20
+ requirements:
20
21
  - - ~>
21
- - !ruby/object:Gem::Version
22
+ - !ruby/object:Gem::Version
22
23
  version: 2.3.0
23
24
  type: :development
24
25
  prerelease: false
25
- version_requirements: *10501580
26
- - !ruby/object:Gem::Dependency
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
27
28
  name: bundler
28
- requirement: &10500380 !ruby/object:Gem::Requirement
29
+ requirement: &id002 !ruby/object:Gem::Requirement
29
30
  none: false
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
33
34
  version: 1.0.0
34
35
  type: :development
35
36
  prerelease: false
36
- version_requirements: *10500380
37
- - !ruby/object:Gem::Dependency
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
38
39
  name: jeweler
39
- requirement: &10498900 !ruby/object:Gem::Requirement
40
+ requirement: &id003 !ruby/object:Gem::Requirement
40
41
  none: false
41
- requirements:
42
+ requirements:
42
43
  - - ~>
43
- - !ruby/object:Gem::Version
44
+ - !ruby/object:Gem::Version
44
45
  version: 1.6.3
45
46
  type: :development
46
47
  prerelease: false
47
- version_requirements: *10498900
48
- - !ruby/object:Gem::Dependency
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
49
50
  name: rcov
50
- requirement: &10498100 !ruby/object:Gem::Requirement
51
+ requirement: &id004 !ruby/object:Gem::Requirement
51
52
  none: false
52
- requirements:
53
- - - ! '>='
54
- - !ruby/object:Gem::Version
55
- version: '0'
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
56
57
  type: :development
57
58
  prerelease: false
58
- version_requirements: *10498100
59
- - !ruby/object:Gem::Dependency
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
60
61
  name: sqlite3
61
- requirement: &10497180 !ruby/object:Gem::Requirement
62
+ requirement: &id005 !ruby/object:Gem::Requirement
62
63
  none: false
63
- requirements:
64
- - - ! '>='
65
- - !ruby/object:Gem::Version
66
- version: '0'
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
67
68
  type: :development
68
69
  prerelease: false
69
- version_requirements: *10497180
70
- - !ruby/object:Gem::Dependency
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
71
72
  name: rmagick
72
- requirement: &10496360 !ruby/object:Gem::Requirement
73
+ requirement: &id006 !ruby/object:Gem::Requirement
73
74
  none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
78
79
  type: :development
79
80
  prerelease: false
80
- version_requirements: *10496360
81
+ version_requirements: *id006
81
82
  description: Including stuff for HTTP, SSH and much more.
82
83
  email: k@spernj.org
83
84
  executables: []
85
+
84
86
  extensions: []
85
- extra_rdoc_files:
87
+
88
+ extra_rdoc_files:
86
89
  - LICENSE.txt
87
90
  - README.rdoc
88
- files:
91
+ files:
89
92
  - .document
90
93
  - .rspec
91
94
  - Gemfile
@@ -332,31 +335,34 @@ files:
332
335
  - testfiles/image.jpg
333
336
  has_rdoc: true
334
337
  homepage: http://github.com/kaspernj/knjrbfw
335
- licenses:
338
+ licenses:
336
339
  - MIT
337
340
  post_install_message:
338
341
  rdoc_options: []
339
- require_paths:
342
+
343
+ require_paths:
340
344
  - lib
341
- required_ruby_version: !ruby/object:Gem::Requirement
345
+ required_ruby_version: !ruby/object:Gem::Requirement
342
346
  none: false
343
- requirements:
344
- - - ! '>='
345
- - !ruby/object:Gem::Version
346
- version: '0'
347
- segments:
347
+ requirements:
348
+ - - ">="
349
+ - !ruby/object:Gem::Version
350
+ hash: -1220639088849160956
351
+ segments:
348
352
  - 0
349
- hash: -3738020012836301532
350
- required_rubygems_version: !ruby/object:Gem::Requirement
353
+ version: "0"
354
+ required_rubygems_version: !ruby/object:Gem::Requirement
351
355
  none: false
352
- requirements:
353
- - - ! '>='
354
- - !ruby/object:Gem::Version
355
- version: '0'
356
+ requirements:
357
+ - - ">="
358
+ - !ruby/object:Gem::Version
359
+ version: "0"
356
360
  requirements: []
361
+
357
362
  rubyforge_project:
358
363
  rubygems_version: 1.6.2
359
364
  signing_key:
360
365
  specification_version: 3
361
366
  summary: A framework with lots of stuff for Ruby.
362
367
  test_files: []
368
+