cowtech-extensions 1.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'jeweler'
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Shogun
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = cowtech-lib
2
+
3
+ Several Ruby object enhancements
4
+ http://github.com/ShogunPanda/cowtech-extensions
5
+
6
+ == Contributing to cowtech-extensions
7
+
8
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
9
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
10
+ * Fork the project
11
+ * Start a feature/bugfix branch
12
+ * Commit and push until you are happy with your contribution
13
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
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
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2011 Shogun. See LICENSE.txt for further details.
19
+
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'jeweler'
3
+ require "./lib/cowtech-extensions/version.rb"
4
+
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "cowtech-extensions"
7
+ gem.homepage = "http://github.com/ShogunPanda/cowtech-extensions"
8
+ gem.license = "MIT"
9
+ gem.summary = %Q{Several Ruby object enhancements.}
10
+ gem.description = %Q{Several Ruby object enhancements.}
11
+ gem.email = "shogun_panda@me.com"
12
+ gem.authors = ["Shogun"]
13
+ gem.version = Cowtech::Extensions::Version::STRING
14
+ gem.add_dependency 'actionpack'
15
+ end
16
+
17
+ Jeweler::RubygemsDotOrgTasks.new
@@ -0,0 +1,86 @@
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
+ require "cowtech-extensions/object"
8
+ require "cowtech-extensions/boolean"
9
+ require "cowtech-extensions/string"
10
+ require "cowtech-extensions/hash"
11
+ require "cowtech-extensions/pathname"
12
+ require "cowtech-extensions/datetime"
13
+ require "cowtech-extensions/math"
14
+ require "cowtech-extensions/active_record" if defined?(ActiveRecord)
15
+
16
+ module Cowtech
17
+ module Extensions
18
+ def self.load!(what = [])
19
+ what = ["object", "boolean", "string", "hash", "pathname", "datetime", "math", "active_record"] if what.count == 0
20
+ what.collect! { |w| w.to_s }
21
+
22
+ yield if block_given?
23
+
24
+ if what.include?("object") then
25
+ Object.class_eval do
26
+ include Cowtech::Extensions::Object
27
+ end
28
+ end
29
+
30
+ if what.include?("boolean") then
31
+ TrueClass.class_eval do
32
+ include Cowtech::Extensions::Boolean
33
+ end
34
+
35
+ FalseClass.class_eval do
36
+ include Cowtech::Extensions::Boolean
37
+ end
38
+ end
39
+
40
+ if what.include?("string") then
41
+ String.class_eval do
42
+ include Cowtech::Extensions::String
43
+ end
44
+ end
45
+
46
+ if what.include?("string") then
47
+ Hash.class_eval do
48
+ include Cowtech::Extensions::Hash
49
+ end
50
+ end
51
+
52
+ if what.include?("pathname") then
53
+ Pathname.class_eval do
54
+ include Cowtech::Extensions::Pathname
55
+ end
56
+ end
57
+
58
+ if what.include?("datetime") then
59
+ Time.class_eval do
60
+ include Cowtech::Extensions::DateTime
61
+ end
62
+
63
+ Date.class_eval do
64
+ include Cowtech::Extensions::DateTime
65
+ end
66
+
67
+ DateTime.class_eval do
68
+ include Cowtech::Extensions::DateTime
69
+ end
70
+ end
71
+
72
+ if what.include?("math") then
73
+ Math.class_eval do
74
+ include Cowtech::Extensions::Math
75
+ end
76
+ end
77
+
78
+ if defined?(ActiveRecord) && what.include?("active_record") then
79
+ ActiveRecord::Base.class_eval do
80
+ include Cowtech::Extensions::AR
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,62 @@
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
+ require "active_support/all"
8
+
9
+ module Cowtech
10
+ module Extensions
11
+ module AR
12
+ extend ActiveSupport::Concern
13
+
14
+ module ClassMethods
15
+ if defined?(Rails) then
16
+ def self.table_prefix
17
+ p = ActiveRecord::Base.configurations[Rails.env]["table_prefix"]
18
+ !p.blank? ? p + "_" : ""
19
+ end
20
+
21
+ def self.table_suffix
22
+ p = ActiveRecord::Base.configurations[Rails.env]["table_suffix"]
23
+ !p.blank? ? p + "_" : ""
24
+ end
25
+ end
26
+
27
+ def self.set_table_name(value = nil, &block)
28
+ define_attr_method :table_name, "#{ActiveRecord::Base.table_prefix}#{value}#{ActiveRecord::Base.table_suffix}", &block
29
+ end
30
+
31
+ def self.find_or_create(oid, attributes = nil)
32
+ begin
33
+ self.find(oid)
34
+ rescue ActiveRecord::RecordNotFound
35
+ self.new(attributes)
36
+ end
37
+ end
38
+
39
+ def self.safe_find(oid)
40
+ begin
41
+ rv = self.find(oid)
42
+ rescue ActiveRecord::RecordNotFound
43
+ nil
44
+ end
45
+ end
46
+
47
+ def self.random
48
+ c = self.count
49
+ c != 0 ? self.find(:first, :offset => rand(c)) : nil
50
+ end
51
+
52
+ def self.per_page
53
+ 25
54
+ end
55
+ end
56
+
57
+ module InstanceMethods
58
+
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,23 @@
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
+ module Boolean
10
+ extend ActiveSupport::Concern
11
+
12
+ module InstanceMethods
13
+ def to_i
14
+ (self == true) ? 1 : 0
15
+ end
16
+
17
+ def value
18
+ self
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,131 @@
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
+ require "active_support/all"
8
+
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
+ self.setup_locale
24
+ end
25
+
26
+ module ClassMethods
27
+ def setup_locale
28
+ self.default_localized_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
29
+ self.default_localized_short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
30
+ self.default_localized_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
31
+ self.default_localized_short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
32
+ self.localized_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
33
+ self.localized_short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
34
+ self.localized_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
35
+ self.localized_short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
36
+ end
37
+
38
+ def months(short = true)
39
+ 12.times.collect |k| {:value => (i + 1).to_s.rjust(2, "0"), :description => self.send("localized_#{short ? "short_" : ""}months").at(i) }
40
+ end
41
+
42
+ def years(offset = 10, also_future = true)
43
+ y = Date.today.year
44
+ (y - offset..(also_future ? y + offset : y)).collect do |year| {:value => year} end
45
+ end
46
+
47
+ def custom_formats
48
+ @@custom_formats ||= {
49
+ "date" => "%d/%m/%Y",
50
+ "time" => "%H:%M:%S",
51
+ "date-8601" => "%Y-%m-%d",
52
+ "date-time-8601" => "%Y-%m-%d %H:%M:%S",
53
+ "iso-8601" => "%FT%T%z",
54
+ "update" => "%d/%m/%Y %H:%M"
55
+ }
56
+ end
57
+
58
+ def custom_format(key = "date")
59
+ self.custom_formats.fetch(key.to_s, "%d/%m/%Y")
60
+ end
61
+
62
+ def easter(year = nil)
63
+ day = 1
64
+ month = 3
65
+ year = Date.today.year if !year.is_integer?
66
+
67
+ # GAUSS METHOD
68
+ a = year % 19
69
+ d = ((19 * a) + 24) % 30
70
+ e = ((2 * (year % 4)) + (4 * (year % 7)) + (6 * d) + 5) % 7
71
+
72
+ if d + e < 10 then
73
+ day = d + e + 22
74
+ else
75
+ day = d + e - 9
76
+ month = 4
77
+ end
78
+
79
+ if day == 26 && month == 4 then
80
+ day = 19
81
+ elsif day == 25 && month == 4 && d == 28 && e == 6 && a > 10 then
82
+ day = 18
83
+ end
84
+ #END
85
+
86
+ Date.civil(year, month, day)
87
+ end
88
+ end
89
+
90
+ module InstanceMethods
91
+ def lstrftime(format = nil)
92
+ format = self.class.custom_format($1) if format =~ /^custom::(.+)/
93
+ unlocal = self.strftime(format || self.class.custom_format("update"))
94
+
95
+ [
96
+ [self.class.default_localized_months, self.class.localized_months], [self.class.default_localized_days, self.class.localized_days],
97
+ [self.class.default_localized_short_months, self.class.localized_short_months], [self.class.default_localized_short_days, self.class.localized_short_days]
98
+ ].each do |iter|
99
+ dict = {}
100
+
101
+ iter[0].each_index { |i|
102
+ key = iter[0][i]
103
+ value = iter[1][i]
104
+ dict[key] = value
105
+ }
106
+
107
+ unlocal.gsub!(/(#{dict.keys.join("|")})/i) { |s| dict[$1] }
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
+ (self.respond_to?(:utc) ? self : self.to_datetime).utc.to_time
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,23 @@
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
+ module Hash
10
+ extend ActiveSupport::Concern
11
+
12
+ module InstanceMethods
13
+ def method_missing(method, *args, &block)
14
+ (self.has_key?(method.to_sym) || self.has_key?(method.to_s)) ? (self[method.to_sym] || self[method.to_s]) : super(method, *args, &block)
15
+ end
16
+
17
+ def respond_to?(method)
18
+ (self.has_key?(method.to_sym) || self.has_key?(method.to_s)) ? true : super(method)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
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
+ module Math
10
+ extend ActiveSupport::Concern
11
+
12
+ module ClassMethods
13
+ def self.max(a, b)
14
+ if a > b then a else b end
15
+ end
16
+
17
+ def self.min(a, b)
18
+ if a < b then a else b end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,81 @@
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
+ require "active_support/all"
8
+ require "action_view/helpers/number_helper"
9
+
10
+ module Cowtech
11
+ module Extensions
12
+ module Object
13
+ extend ActiveSupport::Concern
14
+
15
+ module InstanceMethods
16
+ include ActionView::Helpers::NumberHelper
17
+
18
+ def as_s
19
+ self.present? ? self.to_s : ""
20
+ end
21
+
22
+ def is_number?
23
+ self.is_float?
24
+ end
25
+
26
+ def is_integer?
27
+ self.is_a?(Integer) || /^([+-]?)(\d+)$/.match(self.as_s.strip)
28
+ end
29
+
30
+ def is_float?
31
+ self.is_a?(Float) || /^([+-]?)(\d+)([.,]\d*)?$/.match(self.as_s.strip)
32
+ end
33
+
34
+ def is_boolean?
35
+ 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.as_s.strip)
36
+ end
37
+
38
+ def ensure_array
39
+ self.is_a?(Array) ? self : [self]
40
+ end
41
+
42
+ def to_float
43
+ self.is_float? ? Kernel.Float(self.respond_to?(:gsub) ? self.gsub(",", ".") : self) : 0.0
44
+ end
45
+
46
+ def to_integer
47
+ self.is_integer? ? Kernel.Integer(self, self.is_a?(String) ? 10 : 0) : 0
48
+ end
49
+
50
+ def to_boolean
51
+ (self.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(self.as_s.strip)) ? true : false
52
+ end
53
+
54
+ def round_to_precision(prec = 2)
55
+ number_with_precision(self, :precision => prec)
56
+ end
57
+
58
+ def format_number(prec = 2, decimal_separator = ",", add_string = "€", k_separator = ".")
59
+ 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})
60
+ end
61
+
62
+ def format_boolean
63
+ self.to_boolean ? "Yes" : "No"
64
+ end
65
+
66
+ def debug(format = :yaml, must_raise = true)
67
+ rv = ""
68
+
69
+ begin
70
+ rv = self.send("to_#{format}")
71
+ rescue
72
+ rv = self.inspect
73
+ end
74
+
75
+ rv = "DEBUG:\n#{rv}"
76
+ must_raise ? raise(rv) : rv
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,19 @@
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
+ module Pathname
10
+ extend ActiveSupport::Concern
11
+
12
+ module InstanceMethods
13
+ def components
14
+ self.each_filename.collect { |p| p }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
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
+ require "active_support/all"
8
+
9
+ module Cowtech
10
+ module Extensions
11
+ module String
12
+ extend ActiveSupport::Concern
13
+
14
+ module InstanceMethods
15
+ def remove_accents
16
+ self.mb_chars.normalize(:kd).gsub(/[^\-x00-\x7F]/n, '').to_s
17
+ end
18
+
19
+ def untitleize
20
+ self.downcase.gsub(" ", "-")
21
+ end
22
+
23
+ def replace_ampersands
24
+ self.gsub(/&amp;(\S+);/, "&\\1;")
25
+ end
26
+
27
+ def value
28
+ self
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,20 @@
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
+ require "active_support"
8
+
9
+ module Cowtech
10
+ module Extensions
11
+ module Version
12
+ MAJOR = 1
13
+ MINOR = 0
14
+ PATCH = 0
15
+ BUILD = 0
16
+
17
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cowtech-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shogun
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-29 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jeweler
16
+ requirement: &70148186816900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70148186816900
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &70148186815620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70148186815620
36
+ description: Several Ruby object enhancements.
37
+ email: shogun_panda@me.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - LICENSE.txt
42
+ - README.rdoc
43
+ files:
44
+ - Gemfile
45
+ - LICENSE.txt
46
+ - README.rdoc
47
+ - Rakefile
48
+ - lib/cowtech-extensions.rb
49
+ - lib/cowtech-extensions/activerecord.rb
50
+ - lib/cowtech-extensions/boolean.rb
51
+ - lib/cowtech-extensions/datetime.rb
52
+ - lib/cowtech-extensions/hash.rb
53
+ - lib/cowtech-extensions/math.rb
54
+ - lib/cowtech-extensions/object.rb
55
+ - lib/cowtech-extensions/pathname.rb
56
+ - lib/cowtech-extensions/string.rb
57
+ - lib/cowtech-extensions/version.rb
58
+ homepage: http://github.com/ShogunPanda/cowtech-extensions
59
+ licenses:
60
+ - MIT
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Several Ruby object enhancements.
83
+ test_files: []