flash_extensions 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flash_extensions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flash_extensions"
8
+ spec.version = FlashExtensions::VERSION
9
+ spec.authors = ["Juan Gomez"]
10
+ spec.email = ["j.gomez@drexed.com"]
11
+ spec.summary = %q{Commonly used object helpers}
12
+ spec.description = %q{Class extensions of of commonly used object helpers.}
13
+ spec.homepage = "https://github.com/drexed/flash_extensions"
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.5"
22
+ spec.add_development_dependency "coveralls"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 3.0.0.beta"
25
+ end
@@ -0,0 +1,15 @@
1
+ class Array
2
+
3
+ def remove_blanks
4
+ self.reject{ |e| e.blank? }
5
+ end
6
+
7
+ def remove_first_element
8
+ self[1..self.size]
9
+ end
10
+
11
+ def remove_last_element
12
+ self[0..self.size-2]
13
+ end
14
+
15
+ end
@@ -0,0 +1,50 @@
1
+ class Hash
2
+
3
+ unless method_defined?("stringify_keys")
4
+ def stringify_keys
5
+ inject({}) do |options, (key, value)|
6
+ options[key.to_s] = value
7
+ options
8
+ end
9
+ end
10
+ end
11
+
12
+ unless method_defined?("stringify_keys!")
13
+ def stringify_keys!
14
+ self.replace(self.stringify_keys)
15
+ end
16
+ end
17
+
18
+ unless method_defined?("symbolize_keys")
19
+ def symbolize_keys
20
+ inject({}) do |options, (key, value)|
21
+ options[(key.to_sym rescue key) || key] = value
22
+ options
23
+ end
24
+ end
25
+ end
26
+
27
+ unless method_defined?("symbolize_keys!")
28
+ def symbolize_keys!
29
+ self.replace(self.symbolize_keys)
30
+ end
31
+ end
32
+
33
+ def rename_keys(*args)
34
+ keys = Hash[*args.flatten]
35
+ keys.each { |k, v| self[v] = self.delete(k) if self[k] }
36
+ self
37
+ end
38
+
39
+ def symbolize_and_underscore_keys
40
+ inject({}) do |options, (key, value)|
41
+ options[(key.to_s.underscore.to_sym rescue key) || key] = value
42
+ options
43
+ end
44
+ end
45
+
46
+ def symbolize_and_underscore_keys!
47
+ self.replace(self.symbolize_and_underscore_keys)
48
+ end
49
+
50
+ end
@@ -0,0 +1,29 @@
1
+ class Object
2
+
3
+ unless method_defined?("blank?")
4
+ def blank?
5
+ respond_to?(:empty?) ? !!empty? : !self
6
+ end
7
+ end
8
+
9
+ unless method_defined?("present?")
10
+ def present?
11
+ !blank?
12
+ end
13
+ end
14
+
15
+ unless method_defined?("try")
16
+ def try(method)
17
+ send method if respond_to? method
18
+ end
19
+ end
20
+
21
+ def numeric?
22
+ !self.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
23
+ end
24
+
25
+ def palindrome?
26
+ self.to_s.reverse == self.to_s
27
+ end
28
+
29
+ end
@@ -0,0 +1,131 @@
1
+ class String
2
+
3
+ unless method_defined?("camelize")
4
+ def camelize(first_letter_in_uppercase = true)
5
+ if first_letter_in_uppercase != :lower
6
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
7
+ else
8
+ self.to_s[0].chr.downcase + camelize(self)[1..-1]
9
+ end
10
+ end
11
+ end
12
+
13
+ unless method_defined?("ends_with?")
14
+ def ends_with?(suffix)
15
+ suffix = suffix.to_s
16
+ self[-suffix.length, suffix.length] == suffix
17
+ end
18
+ end
19
+
20
+ unless method_defined?("humanize")
21
+ def humanize(options = {})
22
+ self.underscore.
23
+ gsub(/_id$/, "").
24
+ tr('_', ' ').
25
+ gsub(/([a-z\d]*)/i) { |match| match.downcase }.
26
+ gsub(/^\w/) { |match| match.upcase } if options.fetch(:capitalize, true)
27
+ end
28
+ end
29
+
30
+ unless method_defined?("titleize")
31
+ def titleize
32
+ self.underscore.
33
+ humanize.
34
+ gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
35
+ end
36
+ end
37
+
38
+ unless method_defined?("starts_with?")
39
+ def starts_with?(prefix)
40
+ prefix = prefix.to_s
41
+ self[0, prefix.length] == prefix
42
+ end
43
+ end
44
+
45
+ unless method_defined?("underscore")
46
+ def underscore
47
+ self.gsub(/::/, '/').
48
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
49
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
50
+ tr("-", "_").
51
+ downcase
52
+ end
53
+ end
54
+
55
+ def domain(options={})
56
+ url = self.dup
57
+ url =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url
58
+ end
59
+
60
+ def downcase?
61
+ self.downcase == self
62
+ end
63
+
64
+ def ellipsize(options={})
65
+ length = options[:length] || 30
66
+ separator = options[:separator] || '...'
67
+ return self if self.size <= length
68
+ offset = options[:offset] || 4
69
+ self[0,offset] + separator + self[-offset,offset]
70
+ end
71
+
72
+ def gnix(string)
73
+ self.gsub(string, "")
74
+ end
75
+
76
+ def mixedcase?
77
+ !upcase? && !downcase?
78
+ end
79
+
80
+ def nix(string)
81
+ self.sub(string, "")
82
+ end
83
+
84
+ def pollute(delimiter="^--^--^")
85
+ self.split('').map{ |letter| "#{letter}#{delimiter}" }.join
86
+ end
87
+
88
+ def slugify
89
+ self.gsub(/[^\x00-\x7F]+/, ''). # Remove anything non-ASCII entirely (e.g. diacritics).
90
+ gsub(/[^\w_ \-]+/i, ''). # Remove unwanted chars.
91
+ gsub(/[ \-]+/i, '-'). # No more than one of the separator in a row.
92
+ gsub(/^\-|\-$/i, ''). # Remove leading/trailing separator.
93
+ downcase
94
+ end
95
+
96
+ def strip_tags
97
+ self.gsub(/<\/?[^>]*>/, "")
98
+ end
99
+
100
+ def strip_whitespace
101
+ self.gnix("\t").split(" ").remove_blanks.join(" ")
102
+ end
103
+
104
+ def truncate_preserving_words(options={})
105
+ separator = options[:separator] || '...'
106
+ max_words = options[:max_words] || nil
107
+ if max_words
108
+ words = self.split()
109
+ return self if words.size < max_words
110
+ words = words[0..(max_words-1)]
111
+ words << separator
112
+ words.join(" ")
113
+ else
114
+ max_chars = options[:max_chars] || 30
115
+ return self if self.size < max_chars
116
+ out = self[0..(max_chars-1)].split(" ")
117
+ out.pop
118
+ out << separator
119
+ out.join(" ")
120
+ end
121
+ end
122
+
123
+ def unpollute(delimiter="^--^--^")
124
+ self.gsub(delimiter, "")
125
+ end
126
+
127
+ def upcase?
128
+ self.upcase == self
129
+ end
130
+
131
+ end
@@ -0,0 +1,168 @@
1
+ class Time
2
+
3
+ def format(string="yyyy-m-d h:n z")
4
+ delimiters = string.scan /\W+/
5
+ formatters = string.scan /[a-z0-9_]+/i
6
+
7
+ format_units = {
8
+ ## Date keys
9
+ "d" => "d",
10
+ "day" => "d",
11
+ "day_zero" => "d",
12
+ "dd" => "-d",
13
+ "Day" => "-d",
14
+ "day_unpadded" => "-d",
15
+ "ddd" => "_d",
16
+ "DAY" => "_d",
17
+ "day_blank" => "_d",
18
+ "dddd" => "j",
19
+ "day_of_the_year" => "j",
20
+ "m" => "m",
21
+ "month" => "m",
22
+ "month_zero" => "m",
23
+ "mm" => "-m",
24
+ "Month" => "-m",
25
+ "month_unpadded" => "-m",
26
+ "mmm" => "_m",
27
+ "MONTH" => "_m",
28
+ "month_blank" => "_m",
29
+ "mmmm" => "B",
30
+ "month_name" => "B",
31
+ "mmmmm" => "b",
32
+ "month_name_abbr" => "b",
33
+ "w" => "u",
34
+ "weekday" => "u",
35
+ "ww" => "w",
36
+ "weekday_offset" => "w",
37
+ "www" => "A",
38
+ "weekday_name" => "A",
39
+ "wwww" => "a",
40
+ "weekday_name_abbr" => "a",
41
+ "wwwww" => "W",
42
+ "week" => "W",
43
+ "wwwwww" => "U",
44
+ "week_offset" => "U",
45
+ "yy" => "y",
46
+ "yr" => "y",
47
+ "yyyy" => "Y",
48
+ "year" => "Y",
49
+
50
+ ## Time keys
51
+ "h" => "H",
52
+ "hour" => "H",
53
+ "hour_zero" => "H",
54
+ "hh" => "k",
55
+ "HOUR" => "k",
56
+ "hour_blank" => "k",
57
+ "hhh" => "I",
58
+ "hour_imperical" => "I",
59
+ "hour_imperical_zero" => "I",
60
+ "hhhh" => "l",
61
+ "HOUR_IMPERICAL" => "l",
62
+ "hour_imperical_blank" => "l",
63
+ "ampm" => "P",
64
+ "meridian" => "P",
65
+ "AMPM" => "p",
66
+ "MERIDIAN" => "p",
67
+ "n" => "M",
68
+ "minute" => "M",
69
+ "s" => "S",
70
+ "second" => "S",
71
+ "z" => "z",
72
+ "time_zone" => "z",
73
+ "zz" => ":z",
74
+ "time_zone_offset" => ":z",
75
+ "zzz" => "::z",
76
+ "time_zone_offset_full" => "::z",
77
+ "zzzz" => "Z",
78
+ "time_zone_name" => "Z"
79
+ }
80
+
81
+ self.strftime(
82
+ if string.empty? || string.nil?
83
+ "%Y-%m-%d %H:%M %z"
84
+ else
85
+ formatters.map do |f|
86
+ "%#{format_units[f]}#{delimiters.shift || ''}"
87
+ end.join
88
+ end
89
+ )
90
+ end
91
+
92
+ def to_format(key=:datetime_iso_tzn)
93
+ format_units = {
94
+ ## Date keys
95
+ month: "%m",
96
+ month_zero: "%m",
97
+ month_unpadded: "%-m",
98
+ month_blank: "%_m",
99
+ month_name: "%B",
100
+ month_name_abbr: "%b",
101
+ weekday: "%d",
102
+ weekday_zero: "%d",
103
+ weekday_unpadded: "%-d",
104
+ weekday_blank: "%_d",
105
+ weekday_name: "%A",
106
+ weekday_name_abbr: "%a",
107
+ weekday_name: "%A",
108
+ weekday_name_abbr: "%a",
109
+ yr: "%y",
110
+ year: "%Y",
111
+
112
+ ## Time keys
113
+ hour: "%H",
114
+ hour_zero: "%H",
115
+ hour_blank: "%k",
116
+ hour_imperical: "%I",
117
+ hour_imperical_zero: "%I",
118
+ hour_imperical_blank: "%l",
119
+ ampm: "%P",
120
+ meridian: "%P",
121
+ minute: "%M",
122
+ second: "%S",
123
+ time_zone: "%z",
124
+ time_zone_offset: "%:z",
125
+ time_zone_offset_full: "%::z",
126
+ time_zone_name: "%Z",
127
+
128
+ ## Datetime keys
129
+ date: "%B %-d, %Y",
130
+ date_abbr: "%b %-d, %Y",
131
+ date_iso: "%Y-%m-%d",
132
+ datetime: "%B %-d, %Y %H:%M",
133
+ datetime_abbr: "%b %-d, %Y %H:%M",
134
+ datetime_iso: "%Y-%m-%d %H:%M",
135
+ datetime_imperical: "%B %-d, %Y %I:%M %P",
136
+ datetime_imperical_abbr: "%b %-d, %Y %I:%M %P",
137
+ datetime_imperical_iso: "%Y-%m-%d %I:%M %P",
138
+ datetime_tzn: "%B %-d, %Y %H:%M %Z",
139
+ datetime_abbr_tzn: "%b %-d, %Y %H:%M %Z",
140
+ datetime_iso_tzn: "%Y-%m-%d %H:%M %z",
141
+ datetime_imperical_tzn: "%B %-d, %Y %I:%M %P %Z",
142
+ datetime_imperical_abbr_tzn: "%b %-d, %Y %I:%M %P %Z",
143
+ datetime_imperical_iso_tzn: "%Y-%m-%d %I:%M %P %z",
144
+ day: "%B %-d",
145
+ day_abbr: "%b %-d",
146
+ day_iso: "%m-%d",
147
+ daytime: "%B %-d %H:%M",
148
+ daytime_abbr: "%b %-d %H:%M",
149
+ daytime_iso: "%m-%d %H:%M",
150
+ daytime_imperical: "%B %-d %I:%M %P",
151
+ daytime_imperical_abbr: "%b %-d %I:%M %P",
152
+ daytime_imperical_iso: "%m-%d %I:%M %P",
153
+ time: "%H:%M",
154
+ time_zero: "%H:%M",
155
+ time_blank: "%k:%M",
156
+ time_tz: "%H:%M %z",
157
+ time_tzn: "%H:%M %Z",
158
+ time_imperical: "%I:%M %P",
159
+ time_imperical_zero: "%I:%M %P",
160
+ time_imperical_blank: "%l:%M %P",
161
+ time_imperical_tz: "%I:%M %P %z",
162
+ time_imperical_tzn: "%I:%M %P %Z"
163
+ }
164
+
165
+ self.strftime(format_units[key])
166
+ end
167
+
168
+ end
@@ -0,0 +1,3 @@
1
+ module FlashExtensions
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'flash_extensions/version'
2
+ require 'flash_extensions/extensions/array_extension'
3
+ require 'flash_extensions/extensions/hash_extension'
4
+ require 'flash_extensions/extensions/object_extension'
5
+ require 'flash_extensions/extensions/string_extension'
6
+ require 'flash_extensions/extensions/time_extension'