cowtech-extensions 2.1.3 → 2.5.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.
Files changed (60) hide show
  1. data/.gitignore +1 -0
  2. data/.yardopts +1 -0
  3. data/README.md +10 -0
  4. data/Rakefile +6 -3
  5. data/cowtech-extensions.gemspec +9 -5
  6. data/doc/Cowtech.html +128 -0
  7. data/doc/Cowtech/Extensions.html +546 -0
  8. data/doc/Cowtech/Extensions/Boolean.html +297 -0
  9. data/doc/Cowtech/Extensions/DateTime.html +787 -0
  10. data/doc/Cowtech/Extensions/DateTime/ClassMethods.html +1592 -0
  11. data/doc/Cowtech/Extensions/Exceptions.html +125 -0
  12. data/doc/Cowtech/Extensions/Exceptions/Dump.html +133 -0
  13. data/doc/Cowtech/Extensions/Hash.html +393 -0
  14. data/doc/Cowtech/Extensions/Math.html +130 -0
  15. data/doc/Cowtech/Extensions/Math/ClassMethods.html +362 -0
  16. data/doc/Cowtech/Extensions/Object.html +1565 -0
  17. data/doc/Cowtech/Extensions/Pathname.html +225 -0
  18. data/doc/Cowtech/Extensions/Settings.html +1249 -0
  19. data/doc/Cowtech/Extensions/String.html +471 -0
  20. data/doc/Cowtech/Extensions/TimeZone.html +1210 -0
  21. data/doc/Cowtech/Extensions/TimeZone/ClassMethods.html +925 -0
  22. data/doc/Cowtech/Extensions/Version.html +189 -0
  23. data/doc/_index.html +305 -0
  24. data/doc/class_list.html +53 -0
  25. data/doc/css/common.css +1 -0
  26. data/doc/css/full_list.css +57 -0
  27. data/doc/css/style.css +328 -0
  28. data/doc/file.README.html +103 -0
  29. data/doc/file_list.html +55 -0
  30. data/doc/frames.html +28 -0
  31. data/doc/index.html +103 -0
  32. data/doc/js/app.js +214 -0
  33. data/doc/js/full_list.js +173 -0
  34. data/doc/js/jquery.js +4 -0
  35. data/doc/method_list.html +620 -0
  36. data/doc/top-level-namespace.html +112 -0
  37. data/lib/cowtech-extensions.rb +47 -16
  38. data/lib/cowtech-extensions/boolean.rb +8 -1
  39. data/lib/cowtech-extensions/datetime.rb +377 -71
  40. data/lib/cowtech-extensions/exceptions.rb +16 -0
  41. data/lib/cowtech-extensions/hash.rb +20 -9
  42. data/lib/cowtech-extensions/math.rb +15 -8
  43. data/lib/cowtech-extensions/object.rb +84 -27
  44. data/lib/cowtech-extensions/pathname.rb +10 -1
  45. data/lib/cowtech-extensions/settings.rb +120 -0
  46. data/lib/cowtech-extensions/string.rb +30 -3
  47. data/lib/cowtech-extensions/version.rb +11 -2
  48. data/spec/coverage_helper.rb +19 -0
  49. data/spec/cowtech-extensions/boolean_spec.rb +4 -0
  50. data/spec/cowtech-extensions/datetime_spec.rb +238 -79
  51. data/spec/cowtech-extensions/hash_spec.rb +5 -2
  52. data/spec/cowtech-extensions/math_spec.rb +14 -4
  53. data/spec/cowtech-extensions/object_spec.rb +19 -1
  54. data/spec/cowtech-extensions/pathname_spec.rb +5 -1
  55. data/spec/cowtech-extensions/settings_spec.rb +101 -0
  56. data/spec/cowtech-extensions/string_spec.rb +13 -0
  57. data/spec/cowtech-extensions_spec.rb +33 -13
  58. data/spec/spec_helper.rb +2 -5
  59. metadata +182 -97
  60. data/lib/cowtech-extensions/utils.rb +0 -74
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-extensions gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module Extensions
9
+ # Exceptions for cowtech-extensions.
10
+ module Exceptions
11
+ # This exception is raised from {Object#debug_dump} when `must_raise` is `true`.
12
+ class Dump < ::RuntimeError
13
+ end
14
+ end
15
+ end
16
+ end
@@ -6,25 +6,36 @@
6
6
 
7
7
  module Cowtech
8
8
  module Extensions
9
+ # Extensions for Hash objects.
9
10
  module Hash
10
- extend ActiveSupport::Concern
11
+ extend ::ActiveSupport::Concern
11
12
 
13
+ # This is called when the user access a member using dotted notation.
14
+ #
15
+ # @param method [String|Symbol] Key to search.
16
+ # @param args [Array] *Unused.*
17
+ # @param block [Proc] *Unused.*
18
+ # @return [Object] The value for the key.
12
19
  def method_missing(method, *args, &block)
13
- rv = nil
20
+ rv = nil
14
21
 
15
- if self.has_key?(method.to_sym) then
22
+ if self.has_key?(method.to_sym) then
16
23
  rv = self[method.to_sym]
17
- elsif self.has_key?(method.to_s) then
24
+ elsif self.has_key?(method.to_s) then
18
25
  rv = self[method.to_s]
19
- else
20
- rv = super(method, *args, &block)
21
- end
26
+ else
27
+ rv = ::Hash.method_missing(method, *args, &block)
28
+ end
22
29
 
23
- rv
30
+ rv
24
31
  end
25
32
 
33
+ # This is called when the user access a member using dotted notation.
34
+ #
35
+ # @param method [String|Symbol] Key to search.
36
+ # @return [Boolean] `true` if the key exists, `false` otherwise.
26
37
  def respond_to?(method)
27
- (self.has_key?(method.to_sym) || self.has_key?(method.to_s)) ? true : super(method)
38
+ (self.has_key?(method.to_sym) || self.has_key?(method.to_s)) ? true : ::Hash.respond_to?(method)
28
39
  end
29
40
  end
30
41
  end
@@ -6,36 +6,43 @@
6
6
 
7
7
  module Cowtech
8
8
  module Extensions
9
+ # Utility methods for Math module.
9
10
  module Math
10
- extend ActiveSupport::Concern
11
+ extend ::ActiveSupport::Concern
11
12
 
13
+ # General methods.
12
14
  module ClassMethods
15
+ # Returns the minimum value in the arguments
16
+ #
17
+ # @param args [Array] A collection of object to compare (with the `<` operator).
18
+ # @return [Object] The minimum value or `nil` (if the collection is empty).
13
19
  def min(*args)
14
- args = args.ensure_array.flatten
20
+ rv = nil
15
21
 
22
+ args = args.ensure_array.flatten
16
23
  if args.length > 0 then
17
24
  rv = args[0]
18
25
  args.each do |a| rv = a if a < rv end
19
- else
20
- rv = nil
21
26
  end
22
27
 
23
28
  rv
24
29
  end
25
30
 
31
+ # Returns the maximum value in the arguments
32
+ #
33
+ # @param args [Array] A collection of object to compare (with the `>` operator).
34
+ # @return [Object] The maximum value or `nil` (if the collection is empty).
26
35
  def max(*args)
27
- args = args.ensure_array.flatten
36
+ rv = nil
28
37
 
38
+ args = args.ensure_array.flatten
29
39
  if args.length > 0 then
30
40
  rv = args[0]
31
41
  args.each do |a| rv = a if a > rv end
32
- else
33
- rv = nil
34
42
  end
35
43
 
36
44
  rv
37
45
  end
38
-
39
46
  end
40
47
  end
41
48
  end
@@ -6,10 +6,14 @@
6
6
 
7
7
  module Cowtech
8
8
  module Extensions
9
+ # Extensions for all objects.
9
10
  module Object
10
- include ActionView::Helpers::NumberHelper
11
- extend ActiveSupport::Concern
11
+ include ::ActionView::Helpers::NumberHelper
12
+ extend ::ActiveSupport::Concern
12
13
 
14
+ # Normalizes a number for conversion. Basically this methods removes all separator and ensures that `.` is used for decimal separator.
15
+ #
16
+ # @return [String] The normalized number.
13
17
  def normalize_number
14
18
  rv = self.ensure_string.strip
15
19
  rv = rv.split(/[\.,]/)
@@ -17,83 +21,136 @@ module Cowtech
17
21
  rv.join("")
18
22
  end
19
23
 
24
+ # Checks if the object is a valid number.
25
+ #
26
+ # @return [Boolean] `true` is a valid number, `false` otherwise.
20
27
  def is_number?
21
28
  self.is_float?
22
29
  end
23
30
 
31
+ # Checks if the object is a valid integer.
32
+ #
33
+ # @return [Boolean] `true` is a valid integer, `false` otherwise.
24
34
  def is_integer?
25
- self.is_a?(Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number)
35
+ self.is_a?(::Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number)
26
36
  end
27
37
 
38
+ # Checks if the object is a valid float.
39
+ #
40
+ # @return [Boolean] `true` is a valid float, `false` otherwise.
28
41
  def is_float?
29
- self.is_a?(Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.normalize_number)
42
+ self.is_a?(::Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.normalize_number)
30
43
  end
31
44
 
45
+ # Checks if the object is a valid boolean value.
46
+ #
47
+ # @return [Boolean] `true` is a valid boolean value, `false` otherwise.
32
48
  def is_boolean?
33
- self.is_a?(TrueClass) || self.is_a?(FalseClass) || self.is_a?(NilClass) || (self.ensure_string.strip =~ /^(1|0|true|false|yes|no|t|f|y|n)$/i)
49
+ self.is_a?(::TrueClass) || self.is_a?(::FalseClass) || self.is_a?(::NilClass) || (self.ensure_string.strip =~ /^(1|0|true|false|yes|no|t|f|y|n)$/i)
34
50
  end
35
51
 
52
+ # Makes sure that the object is an array. For non array objects, return a single element array containing the object.
53
+ #
54
+ # @return [Array] If the object is an array, then the object itself, a single element array containing the object otherwise.
36
55
  def ensure_array
37
- self.is_a?(Array) ? self : [self]
56
+ self.is_a?(::Array) ? self : [self]
38
57
  end
39
58
 
40
- def ensure_string
59
+ # Makes sure that the object is a string. For `nil`, it returns "".
60
+ #
61
+ # @return [String] The string representation of the object.
62
+ def ensure_string
41
63
  self.present? ? self.to_s : ""
42
64
  end
43
65
 
44
- def to_float(default_value = 0.0)
45
- if self.is_a?(Float)
66
+ # Converts the object to a float.
67
+ #
68
+ # @param default_value [Float] The value to return if the conversion is not possible.
69
+ # @return [Float] The float representation of the object.
70
+ def to_float(default_value = 0.0)
71
+ if self.is_a?(::Float)
46
72
  self
47
- elsif self.is_a?(Integer)
73
+ elsif self.is_a?(::Integer)
48
74
  self.to_f
49
75
  else
50
- self.is_float? ? Kernel.Float(self.normalize_number) : default_value
76
+ self.is_float? ? ::Kernel.Float(self.normalize_number) : default_value
51
77
  end
52
78
  end
53
79
 
80
+ # Converts the object to a integer.
81
+ #
82
+ # @param default_value [Fixnum] The value to return if the conversion is not possible.
83
+ # @return [Fixnum] The integer representation of the object.
54
84
  def to_integer(default_value = 0)
55
- if self.is_a?(Integer)
85
+ if self.is_a?(::Integer)
56
86
  self
57
- elsif self.is_a?(Float)
87
+ elsif self.is_a?(::Float)
58
88
  self.to_i
59
89
  else
60
- self.is_integer? ? Kernel.Integer(self.normalize_number) : default_value
90
+ self.is_integer? ? ::Kernel.Integer(self.normalize_number) : default_value
61
91
  end
62
92
  end
63
93
 
94
+ # Converts the object to a boolean.
95
+ #
96
+ # @return [Boolean] The boolean representation of the object.
64
97
  def to_boolean
65
98
  rv = self
66
- rv = rv.to_i if rv.is_a?(Float)
99
+ rv = rv.to_i if rv.is_a?(::Float)
67
100
  (rv.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(rv.ensure_string.strip)) ? true : false
68
101
  end
69
102
 
103
+ # Returns the rounded float representaton of the object.
104
+ #
105
+ # @param prec [Fixnum] The precision to keep.
106
+ # @return [Float] The rounded float representaton of the object.
70
107
  def round_to_precision(prec = 2)
71
108
  (self.is_number? && prec >= 0) ? number_with_precision(self, :precision => prec) : nil
72
109
  end
73
110
 
111
+ # Formats a number.
112
+ # @see Settings#setup_format_number
113
+ #
114
+ # @param prec [Fixnum] The precision to show.
115
+ # @param decimal_separator [String] The string to use as decimal separator.
116
+ # @param add_string [String] The string to append to the number.
117
+ # @param k_separator [String] The string to use as thousands separator.
118
+ # @return [String] The string representation of the object.
74
119
  def format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
75
- prec = Cowtech::Extensions.settings.format_number[:prec] if prec.nil?
76
- decimal_separator = Cowtech::Extensions.settings.format_number[:decimal_separator] if decimal_separator.nil?
77
- add_string = Cowtech::Extensions.settings.format_number[:add_string] if add_string.nil?
78
- k_separator = Cowtech::Extensions.settings.format_number[:k_separator] if k_separator.nil?
120
+ prec = ::Cowtech::Extensions.settings.format_number[:prec] if prec.nil?
121
+ decimal_separator = ::Cowtech::Extensions.settings.format_number[:decimal_separator] if decimal_separator.nil?
122
+ add_string = ::Cowtech::Extensions.settings.format_number[:add_string] if add_string.nil?
123
+ k_separator = ::Cowtech::Extensions.settings.format_number[:k_separator] if k_separator.nil?
79
124
 
80
125
  (self.is_number? && prec >= 0) ? number_to_currency(self, {:precision => prec, :separator => decimal_separator, :delimiter => k_separator, :format => add_string.blank? ? "%n" : "%n %u", :unit => add_string.blank? ? "" : add_string.strip}) : nil
81
- end
82
-
83
- def set_defaults_for_format_number
84
-
85
126
  end
86
127
 
87
- def format_boolean
88
- Cowtech::Extensions.settings.boolean_names[self.to_boolean]
128
+ # Formats a boolean.
129
+ # @see Settings#setup_boolean_names
130
+ #
131
+ # @param true_name [String] The string representation of `true`. Defaults to `Yes`.
132
+ # @param false_name [String] The string representation of `false`. Defaults to `No`.
133
+ # @return [String] The string representation of the object.
134
+ def format_boolean(true_name = nil, false_name = nil)
135
+ names = {
136
+ true => true_name || ::Cowtech::Extensions.settings.boolean_names[true],
137
+ false => false_name || ::Cowtech::Extensions.settings.boolean_names[false]
138
+ }
139
+
140
+ names[self.to_boolean]
89
141
  end
90
142
 
143
+ # Inspects an object.
144
+ #
145
+ # @param format The format to use.
146
+ # @param must_raise [Boolean] If raise a Dump exception.
147
+ # @return [String] The object inspected and formatted.
91
148
  def debug_dump(format = :yaml, must_raise = true)
92
149
  rv = ""
93
150
 
94
151
  begin
95
152
  if format == :pretty_json then
96
- rv = JSON.pretty_generate(self)
153
+ rv = ::JSON.pretty_generate(self)
97
154
  else
98
155
  rv = self.send("to_#{format}")
99
156
  end
@@ -101,7 +158,7 @@ module Cowtech
101
158
  rv = self.inspect
102
159
  end
103
160
 
104
- must_raise ? raise(Cowtech::Extensions::Exceptions::Dump.new(rv)) : rv
161
+ must_raise ? raise(::Cowtech::Extensions::Exceptions::Dump.new(rv)) : rv
105
162
  end
106
163
  end
107
164
  end
@@ -6,9 +6,18 @@
6
6
 
7
7
  module Cowtech
8
8
  module Extensions
9
+ # Extensions for the Pathname class.
9
10
  module Pathname
10
- extend ActiveSupport::Concern
11
+ extend ::ActiveSupport::Concern
11
12
 
13
+ # Returns all the components that are included in this path.
14
+ #
15
+ # ```ruby
16
+ # Pathname.new("/usr/bin/ruby").components
17
+ # # => ["usr", "bin", "ruby"]
18
+ # ```
19
+ #
20
+ # @return [Array] A list of all components that are included in this path.
12
21
  def components
13
22
  rv = []
14
23
  self.each_filename { |p| rv << p }
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-extensions gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module Extensions
9
+ # Settings for the extensions.
10
+ class Settings
11
+ # Settings for numbers formatting.
12
+ attr_reader :format_number
13
+
14
+ # String representations of booleans.
15
+ attr_reader :boolean_names
16
+
17
+ # String representations of days and months.
18
+ attr_reader :date_names
19
+
20
+ # Custom date and time formats.
21
+ attr_reader :date_formats
22
+
23
+ # Returns the singleton instance of the settings.
24
+ #
25
+ # @return [Settings] The singleton instance of the settings.
26
+ def self.instance
27
+ @instance ||= self.new
28
+ end
29
+
30
+ # Initializes a new settings object.
31
+ def initialize
32
+ self.setup_format_number
33
+ self.setup_boolean_names
34
+ self.setup_date_formats
35
+ self.setup_date_names
36
+ end
37
+
38
+ # Setups formatters for a number.
39
+ # @see Object#format_number
40
+ #
41
+ # @param prec [Fixnum] The precision to show.
42
+ # @param decimal_separator [String] The string to use as decimal separator.
43
+ # @param add_string [String] The string to append to the number.
44
+ # @param k_separator [String] The string to use as thousands separator.
45
+ # @return [Hash] The new formatters.
46
+ def setup_format_number(prec = 2, decimal_separator = ".", add_string = "", k_separator = ",")
47
+ @format_number = {
48
+ :prec => prec,
49
+ :decimal_separator => decimal_separator,
50
+ :add_string => add_string,
51
+ :k_separator => k_separator
52
+ }
53
+ end
54
+
55
+ # Setups strings representation of booleans.
56
+ # @see Object#format_boolean
57
+ #
58
+ # @param true_name [String] The string representation of `true`. Defaults to `Yes`.
59
+ # @param false_name [String] The string representation of `false`. Defaults to `No`.
60
+ # @return [Hash] The new representations.
61
+ def setup_boolean_names(true_name = nil, false_name = nil)
62
+ true_name ||= "Yes"
63
+ false_name ||= "No"
64
+ @boolean_names = {true => true_name, false => false_name}
65
+ end
66
+
67
+ # Setups custom formats for dates and times.
68
+ # @see DateTime#lstrftime
69
+ #
70
+ # @param formats [Hash] The format to add or replace.
71
+ # @param replace [Boolean] If to discard current formats.
72
+ # @return [Hash] The new formats.
73
+ def setup_date_formats(formats = nil, replace = false)
74
+ formats = {
75
+ :ct_date => "%Y-%m-%d",
76
+ :ct_time => "%H:%M:%S",
77
+ :ct_date_time => "%F %T",
78
+ :ct_iso_8601 => "%FT%T%z"
79
+ } if formats.blank?
80
+
81
+ if formats.is_a?(Hash) then
82
+ if !replace then
83
+ @date_formats ||= {}
84
+ @date_formats.merge!(formats)
85
+ else
86
+ @date_formats = formats
87
+ end
88
+
89
+ @date_formats.each_pair do |k, v| ::Time::DATE_FORMATS[k] = v end
90
+ end
91
+
92
+ @date_formats
93
+ end
94
+
95
+ # Setups strings representation of days and months.
96
+ # @see DateTime::ClassMethods#days
97
+ # @see DateTime::ClassMethods#months
98
+ # @see DateTime#lstrftime
99
+ #
100
+ # @param long_months [Array] The string representation of months.
101
+ # @param short_months [Array] The abbreviated string representation of months.
102
+ # @param long_days [Array] The string representation of days.
103
+ # @param short_days [Array] The abbreviated string representation of days.
104
+ # @return [Hash] The new representations.
105
+ def setup_date_names(long_months = nil, short_months = nil, long_days = nil, short_days = nil)
106
+ long_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] if long_months.blank?
107
+ short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] if short_months.blank?
108
+ long_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] if long_days.blank?
109
+ short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] if short_days.blank?
110
+
111
+ @date_names = {
112
+ :long_months => long_months,
113
+ :short_months => short_months,
114
+ :long_days => long_days,
115
+ :short_days => short_days
116
+ }
117
+ end
118
+ end
119
+ end
120
+ end
@@ -6,21 +6,48 @@
6
6
 
7
7
  module Cowtech
8
8
  module Extensions
9
+ # Extensions for the String class.
9
10
  module String
10
- extend ActiveSupport::Concern
11
+ extend ::ActiveSupport::Concern
11
12
 
13
+ # Removes accents from the string, normalizing to the normal letter.
14
+ #
15
+ # ```ruby
16
+ # "èòàù".remove_accents
17
+ # # => "eoau"
18
+ # ```
19
+ #
20
+ # @return The string with all accents removed.
12
21
  def remove_accents
13
- self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, "").to_s
22
+ silence_warnings {
23
+ self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, "").to_s
24
+ }
14
25
  end
15
26
 
27
+ # Returns the tagged version of a string.
28
+ #
29
+ # The string is downcased and spaces are substituted with `-`.
30
+ #
31
+ # ```ruby
32
+ # "ABC cde".untitleize
33
+ # # => "abc-cde"
34
+ # ```
35
+ #
36
+ # @return [String] The untitleized version of the string.
16
37
  def untitleize
17
38
  self.downcase.gsub(" ", "-")
18
39
  end
19
40
 
20
- def replace_ampersands
41
+ # Returns the string with all `&amp;` replaced with `&`.
42
+ #
43
+ # @return [String] The string with all `&amp;` replaced with `&`.
44
+ def replace_ampersands
21
45
  self.gsub(/&amp;(\S+);/, "&\\1;")
22
46
  end
23
47
 
48
+ # Returns the string itself for use in form helpers.
49
+ #
50
+ # @return [String] The string itself.
24
51
  def value
25
52
  self
26
53
  end