marskal-core-extensions 0.0.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a34bbfa1eb84d2ef1435c9ac3c3386188fe82a7d
4
+ data.tar.gz: 6703c331f1c2936398950fa8f3159c48b85c2480
5
+ SHA512:
6
+ metadata.gz: 65069ab7c457c71a8d88c017e18bc6d908141cd6f5b6143fed640734aee675ebc41d7aebb78332ea9b778e40c95f8f260f0775e431b262889054a50841ae61d4
7
+ data.tar.gz: ed66b0013e2585035c0697b98e925839a5c83535a1e94855fd97027bc26bc8e01caeb5e629b1ef50f87dd271e729781cca6ef5853399f2dd66b791271e56b8a3
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
24
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in marskal-core-extensions.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Urban
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Marskal::Core::Extensions
2
+
3
+ This gems contains several extensions to the Core Functions of Ruby and Ruby On rails.
4
+ This was developed using Ruby 2.0 and Ruby2.1 but many of the extension will likely work with older versions
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'marskal-core-extensions'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install marskal-core-extensions
19
+
20
+ ## Usage
21
+
22
+ array.rb => Extensions for data type **Array**
23
+ numeric.rb => Extensions for data type **Numeric**
24
+ date.rb => Extensions for data type **Date**
25
+ i18n.rb => Extensions for the Internationalization module **I18n**
26
+ string.rb => Extensions for data type **String**
27
+
28
+ Contact
29
+ -------
30
+ Comments and feedback are welcome. Send an email to Mike Urban via mike@marskalgroup.com
31
+
32
+
33
+ ## Contributing
34
+
35
+ Copyright Marskal Group LLC, 2012-2015
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,16 @@
1
+ require "marskal/core/extensions/version"
2
+
3
+ #utilities
4
+ require "marskal/core/extensions/array"
5
+ require "marskal/core/extensions/date"
6
+ require "marskal/core/extensions/i18n"
7
+ require "marskal/core/extensions/numeric"
8
+ require "marskal/core/extensions/string"
9
+
10
+ module Marskal
11
+ module Core
12
+ module Extensions
13
+ # Your code goes here...
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ class Array
2
+
3
+ #converts an array to a string and strips outer brackets ex: [1,2,3].to_string_no_brackets ==> #returns "1,2,3"
4
+ def to_string_no_brackets
5
+ self.to_s.gsub(/[\[\]]/,'')
6
+ end
7
+
8
+ def to_string_no_brackets_or_quotes #addded sept 204 when using for datatables jquery plugin, but can be used for anything
9
+ self.to_s.gsub(/[\"\[\]]/,'') #convert an array into a string and remove the quotes Originally developed to help 'pluck'
10
+ end
11
+
12
+
13
+ def prepare_for_sql_in_clause
14
+ "(#{self.to_string_no_brackets})"
15
+ end
16
+
17
+ def json_data_for_highcharts
18
+ # self.to_json.gsub(/[\'\"]/,'') #maybe add this as a parameter to include or exclude single quotes
19
+ self.to_json.gsub(/[\"]/,'') # but for now, we will allow single quotes a strings are allowed in json
20
+ end
21
+
22
+ def sql_null_to_blank
23
+ self.map {|v| "IFNULL(#{v}, '')" }
24
+ end
25
+
26
+
27
+ end
@@ -0,0 +1,63 @@
1
+ class Date
2
+ def start_of_next_month
3
+ (self + 1.month).start_of_month
4
+ end
5
+
6
+ def for_highcharts
7
+ # MAU 05/2014 - I left the experiments commented out for reference
8
+ # I cant say I understand exactly how this works, but using examples I came up with a method that HighCharts seems to work well with
9
+ # "#{self.to_time.to_i}000".to_i
10
+ # "#{self.strftime('%Y-%m-%d 14:00').to_time.utc.to_i}000".to_i
11
+ # "Date.UTC(#{self.strftime('%Y,%m,%d')})"
12
+ "#{(self-1.day).to_time.strftime("%Y-%m-%d 19:00").to_time.utc.to_i}000".to_i
13
+ end
14
+
15
+ def self.start_of_week
16
+ Date.today.start_of_week
17
+ end
18
+
19
+ def start_of_week
20
+ if self.wday == 0 #sundays count as end of week for vehicle app
21
+ return self - 6
22
+ else
23
+ (self - self.wday.days) + 1 #start on monfay
24
+ end
25
+ end
26
+
27
+
28
+ def start_of_month
29
+ self.strftime('%Y-%m-01').to_date
30
+ end
31
+
32
+
33
+ def self.create_from_string(p_date_str) #MAUNEWP Added 1/2011..creats a date from a string
34
+ parts = p_date_str.split("/")
35
+ return nil if parts.length != 3
36
+ return Date.new(parts[2].to_i,parts[0].to_i,parts[1].to_i)
37
+ end
38
+
39
+ def formatted
40
+ self.strftime("%m/%d/%Y")
41
+ end
42
+
43
+ def self.is_today_weekend?
44
+ (Date.today.wday == 0) or (Date.today.wday == 6)
45
+ end
46
+
47
+ def is_weekend?
48
+ (self.wday == 0) or (self.wday == 6)
49
+ end
50
+
51
+ def self.next_week(date)
52
+ date.start_of_week + 7.days
53
+ end
54
+
55
+ def next_week
56
+ self.start_of_week + 7.days
57
+ end
58
+
59
+ def end_of_week
60
+ self.start_of_week + 4.days
61
+ end
62
+
63
+ end
@@ -0,0 +1,82 @@
1
+ module I18n
2
+
3
+ #translate a single string with no plurals, take :one or only one if only one exist
4
+ #my understanding of how rails recommends
5
+ #have separate entries for plurals in the internationalization fies (e.g. en.yml)
6
+ #for example
7
+ # contact:
8
+ # one: "Contact"
9
+ # many: "Contacts"
10
+ # this func automatically grabs the item labeled :one
11
+ # if :one is not available, then the first available translation will be returned
12
+ # ex from above:
13
+ # I18n.tnp_single('contact') ==> returns "Contact"
14
+ def self.tnp_single(p_token)
15
+ l_translate = t(p_token)
16
+ if l_translate.is_a?(Hash)
17
+ l_translate = l_translate[:one]||l_translate.first[1]
18
+ end
19
+ l_translate
20
+ end
21
+
22
+
23
+ #take elements from the translation file and make into an array
24
+ #if :count is passed, it will attempt to find the plural value from the translation file
25
+ #plural example:
26
+ #en.yml snippet:
27
+ # investor_statuses:
28
+ # client:
29
+ # one: "Client"
30
+ # many: "Clients"
31
+ # prospect:
32
+ # one: "Prospect"
33
+ # many: "Prospects"
34
+ # cold:
35
+ # one: "Cold"
36
+ # many: "Cold Names"
37
+ # I18n.array_it('investor_statuses', count: :many) ==>
38
+ # returns ==> ["Clients", "Prospects", "Cold Names"]
39
+ #
40
+ # Example 2: Simple no plural terms
41
+ # gender:
42
+ # male: "Male"
43
+ # female: "Female"
44
+ # I18n.array_it('gender') ==> ["Male", "Female"]
45
+ #
46
+ def self.array_it(p_token, options = {})
47
+ options.assert_valid_keys(:count)
48
+ l_translate = options[:count] ? plural_hash_it(p_token, options) : t(p_token)
49
+
50
+ if l_translate.is_a?(Hash)
51
+ l_return = []
52
+ l_translate.each do |p_key, p_val|
53
+ l_return << p_val
54
+ end
55
+ else
56
+ l_return = Array(l_translate)
57
+ end
58
+ l_return
59
+ end
60
+
61
+ #take elements from the translation file and make into an array
62
+ def self.plural_hash_it(p_token, options = {})
63
+ options.assert_valid_keys(:count)
64
+ l_count = options[:count] || :one
65
+ l_translate = t(p_token)
66
+ if l_translate.is_a?(Hash)
67
+ l_return = {}
68
+ l_translate.each do |l_key, l_val|
69
+ if l_val.is_a?(Hash)
70
+ l_return.merge!(l_key => l_val[l_count.to_sym]||"Not Found")
71
+ else
72
+ l_return.merge!(l_key => l_val)
73
+ end
74
+ end
75
+ else
76
+ l_return = l_translate
77
+ end
78
+ l_return
79
+ end
80
+
81
+
82
+ end
@@ -0,0 +1,11 @@
1
+ # include ActionView::Helpers::NumberHelper
2
+ #
3
+ # class Numeric
4
+ # def to_currency(options={})
5
+ # number_to_currency(self, options)
6
+ # end
7
+ #
8
+ # def to_pct(options={})
9
+ # number_to_percentage(self, options)
10
+ # end
11
+ # end
@@ -0,0 +1,108 @@
1
+ class String
2
+
3
+ def replace_eol(p_str = ' ')
4
+ self.gsub(/(\r)?\n/, p_str)
5
+ end
6
+
7
+ def unquote
8
+ self.gsub("'","").gsub('"', '')
9
+ end
10
+
11
+ def to_object
12
+ self.singularize.classify.constantize
13
+ end
14
+
15
+ def is_valid_email?
16
+ ret = self =~ /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\z/
17
+ return ret.nil? ? false : true
18
+ end
19
+
20
+ def escape_single_quotes
21
+ self.gsub(/'/, "\\\\'")
22
+ return self
23
+ end
24
+ #check if nil or empty
25
+ def self.nil_or_e?(p_string)
26
+ (p_string.nil? or p_string == "")
27
+ end
28
+
29
+ #set to nil if empty
30
+ def self.nil_to_e(p_string)
31
+ p_string.nil? ? "" : p_string
32
+ end
33
+
34
+ def currency_to_f(options={})
35
+ self.tr_s('$','').tr_s(',','').to_f
36
+ end
37
+
38
+ def to_currency(options={})
39
+ l_fnum = self.currency_to_f #convert back to number and then reformat
40
+ l_fnum.to_currency(options)
41
+ end
42
+
43
+ def to_valid_date(options={})
44
+ value = self
45
+ return if value.blank?
46
+ return value if value.is_a?(Date)
47
+ return value.to_date if value.is_a?(Time) || value.is_a?(DateTime)
48
+
49
+ us_date_format = options[:european_format] ? false : true
50
+
51
+ year, month, day = case value.strip
52
+ # 22/1/06, 22\1\06 or 22.1.06
53
+ when /\A(\d{1,2})[\\\/\.-](\d{1,2})[\\\/\.-](\d{2}|\d{4})\Z/
54
+ us_date_format ? [$3, $1, $2] : [$3, $2, $1]
55
+ # 22 Feb 06 or 1 jun 2001
56
+ when /\A(\d{1,2}) (\w{3,9}) (\d{2}|\d{4})\Z/
57
+ [$3, $2, $1]
58
+ # July 1 2005
59
+ when /\A(\w{3,9}) (\d{1,2})\,? (\d{2}|\d{4})\Z/
60
+ [$3, $1, $2]
61
+ # 2006-01-01
62
+ when /\A(\d{4})-(\d{2})-(\d{2})\Z/
63
+ [$1, $2, $3]
64
+ # 2006-01-01T10:10:10+13:00
65
+ when /\A(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\Z/
66
+ [$1, $2, $3]
67
+ # Not a valid date string
68
+ else
69
+ return nil
70
+ end
71
+
72
+ #do best to make year a legit 4 character year
73
+ year = "#{year.to_i < 20 ? '20' : '19'}#{year.to_i}" if (year.length == 2 or (year.length > 2 && year.slice(0..1) == "00"))
74
+
75
+ Date.new(year.to_i, month.to_i, day.to_i) rescue nil
76
+ end
77
+
78
+ def nameize
79
+ if self.match(/ /)
80
+ # If the name has a space in it, we gotta run the parts through the nameizer.
81
+ name = self.split(' ').each { |part| part.nameize! }.join(' ')
82
+ return name
83
+ #elsif self.match(/^[A-Z]/) --removed by Mike Urban 06/09 because we want to process even if name is capitalized
84
+ # If they took the time to capitalize their name then let's just jump out.
85
+ #return self
86
+ else
87
+ # If there are no spaces and there is no prior capitalization then let's downcase the whole thing.
88
+ name = self.downcase
89
+ end
90
+ # Let's now assume that they were lazy...
91
+ return case
92
+ when name.match(/^mac/i)
93
+ name.gsub(/^mac/i, "").capitalize.insert(0, "Mac")
94
+ when name.match(/^mc/i)
95
+ name.gsub(/^mc/i, "").capitalize.insert(0, "Mc")
96
+ when name.match(/^o\'/i)
97
+ name.split("'").each{ |piece| piece.capitalize! }.join("'")
98
+ else
99
+ name.capitalize # Basically if the name is a first name or it's not Irish then capitalize it.
100
+ end
101
+ end
102
+
103
+ def nameize!
104
+ replace nameize # BANG!
105
+ end
106
+
107
+
108
+ end
@@ -0,0 +1,7 @@
1
+ module Marskal
2
+ module Core
3
+ module Extensions
4
+ VERSION = "0.0.3"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'marskal/core/extensions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "marskal-core-extensions"
8
+ spec.version = Marskal::Core::Extensions::VERSION
9
+ spec.authors = ["Mike Urban"]
10
+ spec.email = ["mike@marskalgroup.com"]
11
+ spec.summary = %q{Many handy uitilities to extend Ruby on Rails core functionality}
12
+ spec.description = %q{Extends modules/classes such as Array, String, Numeric, Date, I18n and more}
13
+ spec.homepage = "http://www.marskalgroup.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ # spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 0"
23
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marskal-core-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Mike Urban
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Extends modules/classes such as Array, String, Numeric, Date, I18n and
42
+ more
43
+ email:
44
+ - mike@marskalgroup.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/marskal/core/extensions.rb
55
+ - lib/marskal/core/extensions/array.rb
56
+ - lib/marskal/core/extensions/date.rb
57
+ - lib/marskal/core/extensions/i18n.rb
58
+ - lib/marskal/core/extensions/numeric.rb
59
+ - lib/marskal/core/extensions/string.rb
60
+ - lib/marskal/core/extensions/version.rb
61
+ - marskal-core-extensions.gemspec
62
+ homepage: http://www.marskalgroup.com
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Many handy uitilities to extend Ruby on Rails core functionality
86
+ test_files: []