cowtech-extensions 1.5.2 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/{README.rdoc → README.md} +3 -3
- data/cowtech-extensions.gemspec +2 -3
- data/lib/cowtech-extensions.rb +8 -8
- data/lib/cowtech-extensions/boolean.rb +10 -0
- data/lib/cowtech-extensions/datetime.rb +124 -121
- data/lib/cowtech-extensions/object.rb +7 -7
- data/lib/cowtech-extensions/utils.rb +0 -0
- data/lib/cowtech-extensions/version.rb +2 -2
- metadata +54 -30
data/Gemfile
CHANGED
data/{README.rdoc → README.md}
RENAMED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
# cowtech-lib
|
2
2
|
|
3
3
|
Several Ruby object enhancements
|
4
4
|
http://github.com/ShogunPanda/cowtech-extensions
|
5
5
|
|
6
|
-
|
6
|
+
## Contributing to cowtech-extensions
|
7
7
|
|
8
8
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
9
9
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
@@ -13,7 +13,7 @@ http://github.com/ShogunPanda/cowtech-extensions
|
|
13
13
|
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
14
14
|
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
15
15
|
|
16
|
-
|
16
|
+
## Copyright
|
17
17
|
|
18
18
|
Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
19
19
|
Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
data/cowtech-extensions.gemspec
CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.authors = ["Shogun"]
|
13
13
|
s.email = ["shogun_panda@me.com"]
|
14
14
|
s.homepage = "http://github.com/ShogunPanda/cowtech-extensions"
|
15
|
-
s.summary = %q{
|
16
|
-
s.description = %q{
|
15
|
+
s.summary = %q{Several Ruby object enhancements.}
|
16
|
+
s.description = %q{Several Ruby object enhancements.}
|
17
17
|
|
18
18
|
s.rubyforge_project = "cowtech-extensions"
|
19
19
|
s.files = `git ls-files`.split("\n")
|
@@ -21,6 +21,5 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
22
|
s.require_paths = ["lib"]
|
23
23
|
|
24
|
-
s.required_ruby_version = ">= 1.9.2"
|
25
24
|
s.add_dependency("actionpack", "~> 3.0")
|
26
25
|
end
|
data/lib/cowtech-extensions.rb
CHANGED
@@ -15,7 +15,7 @@ require "cowtech-extensions/pathname"
|
|
15
15
|
|
16
16
|
module Cowtech
|
17
17
|
module Extensions
|
18
|
-
def self.load
|
18
|
+
def self.load(what = [])
|
19
19
|
what = ["object", "boolean", "string", "hash", "datetime", "math", "pathname"] if what.count == 0
|
20
20
|
what.collect! { |w| w.to_s }
|
21
21
|
|
@@ -31,13 +31,15 @@ module Cowtech
|
|
31
31
|
::TrueClass.class_eval do
|
32
32
|
include Cowtech::Extensions::Object
|
33
33
|
include Cowtech::Extensions::Boolean
|
34
|
-
|
34
|
+
end
|
35
35
|
|
36
36
|
::FalseClass.class_eval do
|
37
|
-
|
38
|
-
|
37
|
+
include Cowtech::Extensions::Object
|
38
|
+
include Cowtech::Extensions::Boolean
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
|
+
TrueClass.cowtech_extensions_setup
|
42
|
+
end
|
41
43
|
|
42
44
|
if what.include?("string") then
|
43
45
|
::String.class_eval do
|
@@ -64,9 +66,7 @@ module Cowtech
|
|
64
66
|
include Cowtech::Extensions::DateTime
|
65
67
|
end
|
66
68
|
|
67
|
-
|
68
|
-
::Time.setup_localization
|
69
|
-
::DateTime.setup_localization
|
69
|
+
#DateTime.cowtech_extensions_setup
|
70
70
|
end
|
71
71
|
|
72
72
|
if what.include?("math") then
|
@@ -9,6 +9,16 @@ module Cowtech
|
|
9
9
|
module Boolean
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
|
+
included do
|
13
|
+
cattr_accessor :boolean_names
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def cowtech_extensions_setup
|
18
|
+
TrueClass::boolean_names = ["No", "Yes"]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
12
22
|
def to_i
|
13
23
|
(self == true) ? 1 : 0
|
14
24
|
end
|
@@ -7,125 +7,128 @@
|
|
7
7
|
require "active_support/all"
|
8
8
|
|
9
9
|
module Cowtech
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
10
|
+
module Extensions
|
11
|
+
module DateTime
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
included do
|
15
|
+
cattr_accessor :date_names
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def months(short = true)
|
20
|
+
12.times.collect { |i| {:value => (i + 1).to_s.rjust(2, "0"), :description => self.send("localized_#{short ? "short_" : ""}months").at(i)} }
|
21
|
+
end
|
22
|
+
|
23
|
+
def years(offset = 10, also_future = true, reference = nil)
|
24
|
+
y = (reference || Date.today).year
|
25
|
+
(y - offset..(also_future ? y + offset : y)).collect { |year| {:value => year} }
|
26
|
+
end
|
27
|
+
|
28
|
+
def easter(year = nil)
|
29
|
+
day = 1
|
30
|
+
month = 3
|
31
|
+
year = Date.today.year if !year.is_integer?
|
32
|
+
|
33
|
+
# Compute using Gauss Method
|
34
|
+
a = year % 19
|
35
|
+
d = ((19 * a) + 24) % 30
|
36
|
+
e = ((2 * (year % 4)) + (4 * (year % 7)) + (6 * d) + 5) % 7
|
37
|
+
|
38
|
+
if d + e < 10 then
|
39
|
+
day = d + e + 22
|
40
|
+
else
|
41
|
+
day = d + e - 9
|
42
|
+
month = 4
|
43
|
+
end
|
44
|
+
|
45
|
+
if day == 26 && month == 4 then
|
46
|
+
day = 19
|
47
|
+
elsif day == 25 && month == 4 && d == 28 && e == 6 && a > 10 then
|
48
|
+
day = 18
|
49
|
+
end
|
50
|
+
# End
|
51
|
+
|
52
|
+
Date.civil(year, month, day)
|
53
|
+
end
|
54
|
+
|
55
|
+
def cowtech_extensions_setup
|
56
|
+
DateTime::date_names ||= {
|
57
|
+
:months => ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
|
58
|
+
:short_months => ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
|
59
|
+
:days => ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
60
|
+
:short_days => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
|
61
|
+
}
|
62
|
+
|
63
|
+
self.custom_formats.each_pair do |k, v| Time::DATE_FORMATS[k] = v end
|
64
|
+
end
|
65
|
+
|
66
|
+
def custom_formats
|
67
|
+
@@custom_formats ||= {
|
68
|
+
:ct_date => "%Y-%m-%d",
|
69
|
+
:ct_time => "%H:%M:%S",
|
70
|
+
:ct_date_time => "%F %T",
|
71
|
+
:ct_iso_8601 => "%FT%T%z"
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
def custom_format(key = "date")
|
76
|
+
key = "ct_#{key}" if key !~ /^ct_/
|
77
|
+
self.custom_formats.fetch(key.to_sym, "%d/%m/%Y")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
#module InstanceMethods
|
82
|
+
def utc_time
|
83
|
+
ua = (self.respond_to?(:utc) ? self : self.to_datetime).utc
|
84
|
+
::Time.utc(ua.year, ua.month, ua.day, ua.hour, ua.min, ua.sec)
|
85
|
+
end
|
86
|
+
|
87
|
+
def in_months(base = 2000)
|
88
|
+
((self.year - 1) - base) * 12 + self.month
|
89
|
+
end
|
90
|
+
|
91
|
+
def padded_month
|
92
|
+
self.month.to_s.rjust(2, "0")
|
93
|
+
end
|
94
|
+
|
95
|
+
def lstrftime(format = nil)
|
96
|
+
localized_format = format.gsub(/(%{1,2}[ab])/i) do |match|
|
97
|
+
mrv = match
|
98
|
+
|
99
|
+
if match !~ /^%%/ then
|
100
|
+
case match
|
101
|
+
when "%a"
|
102
|
+
mrv = ::DateTime.date_names[:short_days][self.wday]
|
103
|
+
when "%A"
|
104
|
+
mrv = ::DateTime.date_names[:days][self.wday]
|
105
|
+
when "%b"
|
106
|
+
mrv = ::DateTime.date_names[:short_months][self.month]
|
107
|
+
when "%B"
|
108
|
+
mrv = ::DateTime.date_names[:months][self.month]
|
109
|
+
end
|
110
|
+
|
111
|
+
mrv.sub!("%", "%%")
|
112
|
+
end
|
113
|
+
|
114
|
+
mrv
|
115
|
+
end
|
116
|
+
|
117
|
+
self.strftime(localized_format)
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_localized_s(format = nil)
|
121
|
+
self.lstrftime(format = nil)
|
122
|
+
end
|
123
|
+
|
124
|
+
def local_strftime(format = nil)
|
125
|
+
(self.respond_to?(:in_time_zone) ? self.in_time_zone : self).strftime(format)
|
126
|
+
end
|
127
|
+
|
128
|
+
def local_lstrftime(format = nil)
|
129
|
+
(self.respond_to?(:in_time_zone) ? self.in_time_zone : self).lstrftime(format)
|
130
|
+
end
|
131
|
+
#end
|
132
|
+
end
|
133
|
+
end
|
131
134
|
end
|
@@ -13,7 +13,7 @@ module Cowtech
|
|
13
13
|
include ActionView::Helpers::NumberHelper
|
14
14
|
extend ActiveSupport::Concern
|
15
15
|
|
16
|
-
|
16
|
+
def is_number?
|
17
17
|
self.is_float?
|
18
18
|
end
|
19
19
|
|
@@ -42,23 +42,23 @@ module Cowtech
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def to_integer
|
45
|
-
|
46
|
-
|
45
|
+
self.is_integer? ? Kernel.Integer(self) : 0
|
46
|
+
end
|
47
47
|
|
48
48
|
def to_boolean
|
49
49
|
(self.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(self.ensure_string.strip)) ? true : false
|
50
50
|
end
|
51
51
|
|
52
52
|
def round_to_precision(prec = 2)
|
53
|
-
number_with_precision(self, precision
|
53
|
+
number_with_precision(self, :precision => prec)
|
54
54
|
end
|
55
55
|
|
56
|
-
def format_number(prec = 2, decimal_separator = ",", add_string = "
|
57
|
-
number_to_currency(self, {precision
|
56
|
+
def format_number(prec = 2, decimal_separator = ",", add_string = "", k_separator = ".")
|
57
|
+
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})
|
58
58
|
end
|
59
59
|
|
60
60
|
def format_boolean
|
61
|
-
|
61
|
+
TrueClass.boolean_names[self.to_boolean.to_i]
|
62
62
|
end
|
63
63
|
|
64
64
|
def debug_dump(format = :yaml, must_raise = true)
|
File without changes
|
metadata
CHANGED
@@ -1,38 +1,51 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: cowtech-extensions
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 6
|
9
|
+
- 0
|
10
|
+
version: 1.6.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Shogun
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-06-05 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: actionpack
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
25
|
+
requirements:
|
19
26
|
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 7
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: "3.0"
|
22
33
|
type: :runtime
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
email:
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Several Ruby object enhancements.
|
36
|
+
email:
|
27
37
|
- shogun_panda@me.com
|
28
38
|
executables: []
|
39
|
+
|
29
40
|
extensions: []
|
41
|
+
|
30
42
|
extra_rdoc_files: []
|
31
|
-
|
43
|
+
|
44
|
+
files:
|
32
45
|
- .gitignore
|
33
46
|
- Gemfile
|
34
47
|
- Gemfile.lock
|
35
|
-
- README.
|
48
|
+
- README.md
|
36
49
|
- Rakefile
|
37
50
|
- cowtech-extensions.gemspec
|
38
51
|
- lib/cowtech-extensions.rb
|
@@ -47,26 +60,37 @@ files:
|
|
47
60
|
- lib/cowtech-extensions/version.rb
|
48
61
|
homepage: http://github.com/ShogunPanda/cowtech-extensions
|
49
62
|
licenses: []
|
63
|
+
|
50
64
|
post_install_message:
|
51
65
|
rdoc_options: []
|
52
|
-
|
66
|
+
|
67
|
+
require_paths:
|
53
68
|
- lib
|
54
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
70
|
none: false
|
56
|
-
requirements:
|
57
|
-
- -
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
|
60
|
-
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
79
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
66
87
|
requirements: []
|
88
|
+
|
67
89
|
rubyforge_project: cowtech-extensions
|
68
|
-
rubygems_version: 1.8.
|
90
|
+
rubygems_version: 1.8.24
|
69
91
|
signing_key:
|
70
92
|
specification_version: 3
|
71
|
-
summary:
|
93
|
+
summary: Several Ruby object enhancements.
|
72
94
|
test_files: []
|
95
|
+
|
96
|
+
has_rdoc:
|