lazier 1.0.0

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