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 CHANGED
@@ -6,4 +6,4 @@
6
6
 
7
7
  source "http://rubygems.org"
8
8
 
9
- gemspec
9
+ gemspec
@@ -1,9 +1,9 @@
1
- = cowtech-lib
1
+ # cowtech-lib
2
2
 
3
3
  Several Ruby object enhancements
4
4
  http://github.com/ShogunPanda/cowtech-extensions
5
5
 
6
- == Contributing to cowtech-extensions
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
- == Copyright
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.
@@ -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{A Rails blog engine plugin.}
16
- s.description = %q{A Rails blog engine plugin.}
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
@@ -15,7 +15,7 @@ require "cowtech-extensions/pathname"
15
15
 
16
16
  module Cowtech
17
17
  module Extensions
18
- def self.load!(what = [])
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
- end
34
+ end
35
35
 
36
36
  ::FalseClass.class_eval do
37
- include Cowtech::Extensions::Object
38
- include Cowtech::Extensions::Boolean
37
+ include Cowtech::Extensions::Object
38
+ include Cowtech::Extensions::Boolean
39
39
  end
40
- end
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
- ::Date.setup_localization
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
- module Extensions
11
- module DateTime
12
- extend ActiveSupport::Concern
13
-
14
- included do
15
- cattr_accessor :default_localized_months
16
- cattr_accessor :default_localized_short_months
17
- cattr_accessor :default_localized_days
18
- cattr_accessor :default_localized_short_days
19
- cattr_accessor :localized_months
20
- cattr_accessor :localized_short_months
21
- cattr_accessor :localized_days
22
- cattr_accessor :localized_short_days
23
- end
24
-
25
- module ClassMethods
26
- def setup_localization
27
- self.default_setup_locale
28
- self.setup_locale
29
- end
30
-
31
- def default_setup_locale
32
- self.default_localized_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
33
- self.default_localized_short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
34
- self.default_localized_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
35
- self.default_localized_short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
36
- end
37
-
38
- def setup_locale
39
- self.localized_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
40
- self.localized_short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
41
- self.localized_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
42
- self.localized_short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
43
- end
44
-
45
- def months(short = true)
46
- 12.times.collect { |i| {value: (i + 1).to_s.rjust(2, "0"), description: self.send("localized_#{short ? "short_" : ""}months").at(i) } }
47
- end
48
-
49
- def years(offset = 10, also_future = true)
50
- y = Date.today.year
51
- (y - offset..(also_future ? y + offset : y)).collect { |year| {value: year} }
52
- end
53
-
54
- def custom_formats
55
- @@custom_formats ||= {
56
- "date" => "%d/%m/%Y",
57
- "time" => "%H:%M:%S",
58
- "date-8601" => "%Y-%m-%d",
59
- "date-time-8601" => "%Y-%m-%d %H:%M:%S",
60
- "iso-8601" => "%FT%T%z",
61
- "update" => "%d/%m/%Y %H:%M"
62
- }
63
- end
64
-
65
- def custom_format(key = "date")
66
- self.custom_formats.fetch(key.to_s, "%d/%m/%Y")
67
- end
68
-
69
- def easter(year = nil)
70
- day = 1
71
- month = 3
72
- year = Date.today.year if !year.is_integer?
73
-
74
- # GAUSS METHOD
75
- a = year % 19
76
- d = ((19 * a) + 24) % 30
77
- e = ((2 * (year % 4)) + (4 * (year % 7)) + (6 * d) + 5) % 7
78
-
79
- if d + e < 10 then
80
- day = d + e + 22
81
- else
82
- day = d + e - 9
83
- month = 4
84
- end
85
-
86
- if day == 26 && month == 4 then
87
- day = 19
88
- elsif day == 25 && month == 4 && d == 28 && e == 6 && a > 10 then
89
- day = 18
90
- end
91
- #END
92
-
93
- Date.civil(year, month, day)
94
- end
95
- end
96
-
97
- def lstrftime(format = nil)
98
- format = self.class.custom_format($1) if format =~ /^custom::(.+)/
99
- format ||= self.class.custom_format("update")
100
- unlocal = self.strftime(format)
101
-
102
- { "%a" => "short_days", "%A" => "days", "%b" => "short_months", "%B" => "months" }.each_pair do |specifier, method|
103
- if format.include?(specifier) then
104
- from = self.class.send("default_localized_" + method)
105
- to = self.class.send("localized_" + method)
106
- unlocal.gsub!(/(#{from.join("|")})/i) { |s| to[from.index($1)] }
107
- end
108
- end
109
-
110
- unlocal
111
- end
112
-
113
- def local_lstrftime(format = nil)
114
- (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).lstrftime(format)
115
- end
116
-
117
- def padded_month
118
- self.month.to_s.rjust(2, "0")
119
- end
120
-
121
- def in_months
122
- ((self.year - 1) % 2000) * 12 + self.month
123
- end
124
-
125
- def utc_time
126
- ua = (self.respond_to?(:utc) ? self : self.to_datetime).utc
127
- ::Time.utc(ua.year, ua.month, ua.day, ua.hour, ua.min, ua.sec)
128
- end
129
- end
130
- end
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
- def is_number?
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
- self.is_integer? ? Kernel.Integer(self, self.is_a?(String) ? 10 : 0) : 0
46
- end
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: prec)
53
+ number_with_precision(self, :precision => prec)
54
54
  end
55
55
 
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})
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
- self.to_boolean ? "Yes" : "No"
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
@@ -10,8 +10,8 @@ module Cowtech
10
10
  module Extensions
11
11
  module Version
12
12
  MAJOR = 1
13
- MINOR = 5
14
- PATCH = 2
13
+ MINOR = 6
14
+ PATCH = 0
15
15
 
16
16
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
17
17
  end
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
- version: 1.5.2
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
- date: 2012-04-23 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-06-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: actionpack
16
- requirement: &70350714263580 !ruby/object:Gem::Requirement
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
- version: '3.0'
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
22
33
  type: :runtime
23
- prerelease: false
24
- version_requirements: *70350714263580
25
- description: A Rails blog engine plugin.
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
- files:
43
+
44
+ files:
32
45
  - .gitignore
33
46
  - Gemfile
34
47
  - Gemfile.lock
35
- - README.rdoc
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
- require_paths:
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
- version: 1.9.2
60
- required_rubygems_version: !ruby/object:Gem::Requirement
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
- version: '0'
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.15
90
+ rubygems_version: 1.8.24
69
91
  signing_key:
70
92
  specification_version: 3
71
- summary: A Rails blog engine plugin.
93
+ summary: Several Ruby object enhancements.
72
94
  test_files: []
95
+
96
+ has_rdoc: