cowtech-extensions 1.6.3 → 1.6.4

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.
@@ -20,6 +20,10 @@ require "cowtech-extensions/pathname"
20
20
 
21
21
  module Cowtech
22
22
  module Extensions
23
+ def self.settings
24
+ Cowtech::Extensions::Settings.instance
25
+ end
26
+
23
27
  def self.load!(*what)
24
28
  what = ["object", "boolean", "string", "hash", "datetime", "math", "pathname"] if what.count == 0
25
29
  what.collect! { |w| w.to_s }
@@ -42,8 +46,6 @@ module Cowtech
42
46
  include Cowtech::Extensions::Object
43
47
  include Cowtech::Extensions::Boolean
44
48
  end
45
-
46
- TrueClass.cowtech_extensions_setup
47
49
  end
48
50
 
49
51
  if what.include?("string") then
@@ -13,13 +13,6 @@ module Cowtech
13
13
  cattr_accessor :boolean_names
14
14
  end
15
15
 
16
- module ClassMethods
17
- # TODO: To test
18
- def cowtech_extensions_setup
19
- TrueClass::boolean_names = ["No", "Yes"]
20
- end
21
- end
22
-
23
16
  def to_i
24
17
  (self == true) ? 1 : 0
25
18
  end
@@ -4,27 +4,33 @@
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
- # TODO: To test
8
7
  module Cowtech
9
8
  module Extensions
10
9
  module Object
11
10
  include ActionView::Helpers::NumberHelper
12
11
  extend ActiveSupport::Concern
13
12
 
13
+ def normalize_number
14
+ rv = self.ensure_string.strip
15
+ rv = rv.split(/[\.,]/)
16
+ rv[-1] = "." + rv[-1] if rv.length > 1
17
+ rv.join("")
18
+ end
19
+
14
20
  def is_number?
15
21
  self.is_float?
16
22
  end
17
23
 
18
24
  def is_integer?
19
- self.is_a?(Integer) || /^([+-]?)(\d+)$/.match(self.ensure_string.strip)
25
+ self.is_a?(Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number)
20
26
  end
21
27
 
22
28
  def is_float?
23
- self.is_a?(Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.ensure_string.strip)
29
+ self.is_a?(Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.normalize_number)
24
30
  end
25
31
 
26
32
  def is_boolean?
27
- self.is_a?(TrueClass) || self.is_a?(FalseClass) || self.is_a?(NilClass) || /^(1|0|true|false|yes|no|t|f|y|n)$/i.match(self.ensure_string.strip)
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)
28
34
  end
29
35
 
30
36
  def ensure_array
@@ -35,28 +41,51 @@ module Cowtech
35
41
  self.present? ? self.to_s : ""
36
42
  end
37
43
 
38
- def to_float
39
- self.is_float? ? Kernel.Float(self.respond_to?(:gsub) ? self.gsub(",", ".") : self) : 0.0
44
+ def to_float(default_value = 0.0)
45
+ if self.is_a?(Float)
46
+ self
47
+ elsif self.is_a?(Integer)
48
+ self.to_f
49
+ else
50
+ self.is_float? ? Kernel.Float(self.normalize_number) : default_value
51
+ end
40
52
  end
41
53
 
42
- def to_integer
43
- self.is_integer? ? Kernel.Integer(self) : 0
54
+ def to_integer(default_value = 0)
55
+ if self.is_a?(Integer)
56
+ self
57
+ elsif self.is_a?(Float)
58
+ self.to_i
59
+ else
60
+ self.is_integer? ? Kernel.Integer(self.normalize_number) : default_value
61
+ end
44
62
  end
45
63
 
46
64
  def to_boolean
47
- (self.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(self.ensure_string.strip)) ? true : false
65
+ rv = self
66
+ rv = rv.to_i if rv.is_a?(Float)
67
+ (rv.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(rv.ensure_string.strip)) ? true : false
48
68
  end
49
69
 
50
70
  def round_to_precision(prec = 2)
51
- number_with_precision(self, :precision => prec)
71
+ (self.is_number? && prec >= 0) ? number_with_precision(self, :precision => prec) : nil
52
72
  end
53
73
 
54
- def format_number(prec = 2, decimal_separator = ",", add_string = "", k_separator = ".")
55
- 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})
74
+ 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?
79
+
80
+ (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
56
81
  end
57
82
 
83
+ def set_defaults_for_format_number
84
+
85
+ end
86
+
58
87
  def format_boolean
59
- TrueClass.boolean_names[self.to_boolean.to_i]
88
+ Cowtech::Extensions.settings.boolean_names[self.to_boolean]
60
89
  end
61
90
 
62
91
  def debug_dump(format = :yaml, must_raise = true)
@@ -6,6 +6,32 @@
6
6
 
7
7
  module Cowtech
8
8
  module Extensions
9
+ class Settings
10
+ attr_reader :format_number, :date_names, :boolean_names
11
+
12
+ def self.instance
13
+ @@instance ||= Cowtech::Extensions::Settings.new
14
+ end
15
+
16
+ def initialize
17
+ self.setup_format_number
18
+ self.setup_boolean_names
19
+ end
20
+
21
+ def setup_format_number(prec = 2, decimal_separator = ".", add_string = "", k_separator = ",")
22
+ @format_number = {
23
+ :prec => prec,
24
+ :decimal_separator => decimal_separator,
25
+ :add_string => add_string,
26
+ :k_separator => k_separator
27
+ }
28
+ end
29
+
30
+ def setup_boolean_names(true_name = "Yes", false_name = "No")
31
+ @boolean_names = {true => true_name, false => false_name}
32
+ end
33
+ end
34
+
9
35
  module Exceptions
10
36
  class Dump < ::RuntimeError
11
37
  end
@@ -9,7 +9,7 @@ module Cowtech
9
9
  module Version
10
10
  MAJOR = 1
11
11
  MINOR = 6
12
- PATCH = 3
12
+ PATCH = 4
13
13
 
14
14
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
15
15
  end
@@ -2,4 +2,10 @@
2
2
  #
3
3
  # This file is part of the cowtech-extensions gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Cowtech::Extensions::DateTime do
10
+
11
+ end
@@ -2,4 +2,212 @@
2
2
  #
3
3
  # This file is part of the cowtech-extensions gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Cowtech::Extensions::Object do
10
+ describe "#normalize_number" do
11
+ it "should correctly sanitize numbers" do
12
+ 123.normalize_number.should == "123"
13
+ "123.45".normalize_number.should == "123.45"
14
+ "1,23.45".normalize_number.should == "123.45"
15
+ "-1.23.45".normalize_number.should == "-123.45"
16
+ "+123.45".normalize_number.should == "+123.45"
17
+ "+1.231,45".normalize_number.should == "+1231.45"
18
+ end
19
+ end
20
+
21
+ describe "#is_number?" do
22
+ it "should return true for a valid number" do
23
+ "123.45".is_number?.should be_true
24
+ "1,23.45".is_number?.should be_true
25
+ "-1.23.45".is_number?.should be_true
26
+ "+123.45".is_number?.should be_true
27
+ "+1.231,45".is_number?.should be_true
28
+ end
29
+
30
+ it "should return true for a invalid number" do
31
+ "s213".is_number?.should be_false
32
+ nil.is_number?.should be_false
33
+ end
34
+ end
35
+
36
+ describe "#is_integer?" do
37
+ it "should return true for a valid number" do
38
+ "123".is_integer?.should be_true
39
+ "-123".is_integer?.should be_true
40
+ "+123".is_integer?.should be_true
41
+ end
42
+
43
+ it "should return true for a invalid number" do
44
+ "s123".is_integer?.should be_false
45
+ "123.12".is_integer?.should be_false
46
+ end
47
+ end
48
+
49
+ describe "#is_float?" do
50
+ it "should return true for a valid number" do
51
+ "123.45".is_float?.should be_true
52
+ "1,23.45".is_float?.should be_true
53
+ "-1.23.45".is_float?.should be_true
54
+ "+123.45".is_float?.should be_true
55
+ "+1.231,45".is_float?.should be_true
56
+ end
57
+
58
+ it "should return true for a invalid number" do
59
+ "s213".is_float?.should be_false
60
+ nil.is_float?.should be_false
61
+ end
62
+ end
63
+
64
+ describe "#is_boolean?" do
65
+ it "should return true for a valid boolean" do
66
+ true.is_boolean?.should be_true
67
+ false.is_boolean?.should be_true
68
+ nil.is_boolean?.should be_true
69
+ "y".is_boolean?.should be_true
70
+ "n".is_boolean?.should be_true
71
+ "yes".is_boolean?.should be_true
72
+ "no".is_boolean?.should be_true
73
+ "1".is_boolean?.should be_true
74
+ "0".is_boolean?.should be_true
75
+ "true".is_boolean?.should be_true
76
+ "false".is_boolean?.should be_true
77
+ "t".is_boolean?.should be_true
78
+ "f".is_boolean?.should be_true
79
+ 1.is_boolean?.should be_true
80
+ 0.is_boolean?.should be_true
81
+ end
82
+
83
+ it "should return true for a invalid boolean" do
84
+ "11".is_boolean?.should be_false
85
+ end
86
+ end
87
+
88
+ describe "#ensure_string" do
89
+ it "should correctly handle strings" do
90
+ " abc ".ensure_string.should == " abc "
91
+ 1.ensure_string == "1"
92
+ 1.0.ensure_string.should == "1.0"
93
+ :abc.ensure_string.should == "abc"
94
+ nil.ensure_string.should == ""
95
+ end
96
+ end
97
+
98
+ describe "#to_float" do
99
+ it "should correctly convert number" do
100
+ 123.45.to_float.should == 123.45
101
+ 123.to_float.should == 123.00
102
+ "123.45".to_float.should == 123.45
103
+ "1,23.45".to_float.should == 123.45
104
+ "-1.23.45".to_float.should == -123.45
105
+ "+123.45".to_float.should == 123.45
106
+ "+1.231,45".to_float.should == 1231.45
107
+ end
108
+
109
+ it "should return 0.0 for a invalid number without a default" do
110
+ "s213".to_float.should == 0.0
111
+ end
112
+
113
+ it "should return the default for a invalid number" do
114
+ "s213".to_float(1.0).should == 1.0
115
+ end
116
+ end
117
+
118
+ describe "#to_integer" do
119
+ it "should correctly convert number" do
120
+ 123.45.to_integer.should == 123
121
+ 123.to_integer.should == 123
122
+ "+123".to_integer.should == 123
123
+ "-123".to_integer.should == -123
124
+ end
125
+
126
+ it "should return 0 for a invalid number without a default" do
127
+ "s213".to_integer.should == 0
128
+ end
129
+
130
+ it "should return the default for a invalid number" do
131
+ "s213".to_integer(1).should == 1
132
+ end
133
+ end
134
+
135
+ describe "#to_boolean" do
136
+ it "should return true for a valid true boolean" do
137
+ true.to_boolean.should be_true
138
+ "y".to_boolean.should be_true
139
+ "yes".to_boolean.should be_true
140
+ "1".to_boolean.should be_true
141
+ 1.to_boolean.should be_true
142
+ 1.0.to_boolean.should be_true
143
+ end
144
+
145
+ it "should return false for all other values" do
146
+ false.to_boolean.should be_false
147
+ nil.to_boolean.should be_false
148
+ "n".to_boolean.should be_false
149
+ "no".to_boolean.should be_false
150
+ "2".to_boolean.should be_false
151
+ 2.0.to_boolean.should be_false
152
+ "false".to_boolean.should be_false
153
+ "abc".to_boolean.should be_false
154
+ 0.to_boolean.should be_false
155
+ end
156
+ end
157
+
158
+ describe "#round_to_precision" do
159
+ it "should round number" do
160
+ 123.456789.round_to_precision(2).should == "123.46"
161
+ 123.456789.round_to_precision(0).should == "123"
162
+ "123.456789".round_to_precision(2).should == "123.46"
163
+ end
164
+
165
+ it "should return nil for non numeric values" do
166
+ 123.456789.round_to_precision(-1).should == nil
167
+ "abc".round_to_precision(-1).should == nil
168
+ end
169
+ end
170
+
171
+ describe "#format_number" do
172
+ it "should format number" do
173
+ 123123.456789.format_number.should == "123,123.46"
174
+ 123123.456789.format_number(2).should == "123,123.46"
175
+ 123123.456789.format_number(3, "@").should == "123,123@457"
176
+ 123123.456789.format_number(3, "@", "$").should == "123,123@457 $"
177
+ "123123.456789".format_number(3, "@", "$", "!").should == "123!123@457 $"
178
+
179
+ Cowtech::Extensions.settings.setup_format_number(5, ",", "£", ".")
180
+ 123123.456789.format_number.should == "123.123,45679 £"
181
+ end
182
+
183
+ it "should return nil for non numeric values" do
184
+ 123.456789.format_number(-1).should == nil
185
+ "abc".format_number(-1).should == nil
186
+ end
187
+ end
188
+
189
+ describe "#format_boolean" do
190
+ it "should return the correct string for all values" do
191
+ "yes".format_boolean.should == "Yes"
192
+ "abc".format_boolean.should == "No"
193
+ end
194
+
195
+ it "should support localization" do
196
+ Cowtech::Extensions.settings.setup_boolean_names("YYY", "NNN")
197
+ "yes".format_boolean.should == "YYY"
198
+ "abc".format_boolean.should == "NNN"
199
+ end
200
+ end
201
+
202
+ describe "#debug_dump" do
203
+ it "should return the correct representation for an object" do
204
+ reference = {:a => "b"}
205
+ reference.debug_dump(:json, false).should == reference.to_json
206
+ reference.debug_dump(:yaml, false).should == reference.to_yaml
207
+ end
208
+
209
+ it "should raise an exception if requested" do
210
+ expect { {:a => "b"}.debug_dump }.to raise_error(Cowtech::Extensions::Exceptions::Dump)
211
+ end
212
+ end
213
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cowtech-extensions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 6
9
- - 3
10
- version: 1.6.3
9
+ - 4
10
+ version: 1.6.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shogun
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-06 00:00:00 Z
18
+ date: 2012-06-07 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: actionpack