active_object 1.0.0

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.
@@ -0,0 +1,41 @@
1
+ class Object
2
+
3
+ unless method_defined?(:blank?)
4
+ def blank?
5
+ object = self
6
+ object = object.strip if respond_to?(:strip)
7
+ respond_to?(:empty?) ? !!object.empty? : !object
8
+ end
9
+ end
10
+
11
+ def numeric?
12
+ !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
13
+ end
14
+
15
+ def palindrome?
16
+ to_s == to_s.reverse
17
+ end
18
+
19
+ unless method_defined?(:present?)
20
+ def present?
21
+ !blank?
22
+ end
23
+ end
24
+
25
+ unless method_defined?(:try)
26
+ def try(*a, &b)
27
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
28
+ end
29
+ end
30
+
31
+ unless method_defined?(:try!)
32
+ def try!(*a, &b)
33
+ if a.empty? && block_given?
34
+ b.arity.zero? ? instance_eval(&b) : yield(self)
35
+ else
36
+ public_send(*a, &b)
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,311 @@
1
+ class String
2
+
3
+ unless method_defined?(:at)
4
+ def at(position)
5
+ self[position]
6
+ end
7
+ end
8
+
9
+ unless method_defined?(:camelize)
10
+ def camelize(first_letter=:upper)
11
+ if first_letter != :lower
12
+ to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
13
+ else
14
+ to_s.first.chr.downcase + camelize(self)[1..-1]
15
+ end
16
+ end
17
+
18
+ alias_method :camelcase, :camelize
19
+ end
20
+
21
+ unless method_defined?(:camelize!)
22
+ def camelize!(first_letter=:upper)
23
+ replace(camelize(first_letter))
24
+ end
25
+
26
+ alias_method :camelcase!, :camelize!
27
+ end
28
+
29
+ unless method_defined?(:classify)
30
+ def classify
31
+ to_s.sub(/.*\./, "").camelize
32
+ end
33
+ end
34
+
35
+ unless method_defined?(:classify!)
36
+ def classify!
37
+ replace(classify)
38
+ end
39
+ end
40
+
41
+ unless method_defined?(:dasherize)
42
+ def dasherize
43
+ gsub(/_/, "-")
44
+ end
45
+ end
46
+
47
+ unless method_defined?(:dasherize!)
48
+ def dasherize!
49
+ replace(dasherize)
50
+ end
51
+ end
52
+
53
+ unless method_defined?(:demodulize)
54
+ def demodulize
55
+ to_s.gsub(/^.*::/, "")
56
+ end
57
+ end
58
+
59
+ unless method_defined?(:demodulize!)
60
+ def demodulize!
61
+ replace(demodulize)
62
+ end
63
+ end
64
+
65
+ def domain
66
+ string = dup
67
+ string =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : string
68
+ end
69
+
70
+ def downcase?
71
+ downcase == self
72
+ end
73
+
74
+ def ellipsize(ellipsize_at, options={})
75
+ return(self)if size <= ellipsize_at
76
+ separator = options.fetch(:separator, "...")
77
+ offset = options.fetch(:offset, 4)
78
+
79
+ "#{self[0,offset]}#{separator}#{self[-offset,offset]}"
80
+ end
81
+
82
+ unless method_defined?(:exclude?)
83
+ def exclude?(string)
84
+ !include?(string)
85
+ end
86
+ end
87
+
88
+ unless method_defined?(:first)
89
+ def first(limit=1)
90
+ return("") if limit.zero?
91
+ limit >= size ? self.dup : to(limit - 1)
92
+ end
93
+ end
94
+
95
+ unless method_defined?(:from)
96
+ def from(position)
97
+ self[position..-1]
98
+ end
99
+ end
100
+
101
+ unless method_defined?(:humanize)
102
+ def humanize(options = {})
103
+ underscore.
104
+ gsub(/_id\z/, "").
105
+ tr("_", " ").
106
+ gsub(/([a-z\d]*)/i) { |s| s.downcase }.
107
+ gsub(/\A\w/) { |s| options.fetch(:capitalize, true) ? s.upcase : s }
108
+ end
109
+ end
110
+
111
+ unless method_defined?(:humanize!)
112
+ def humanize!(options={})
113
+ replace(humanize(options))
114
+ end
115
+ end
116
+
117
+ unless method_defined?(:indent!)
118
+ def indent(amount, indent_string=nil, indent_empty_lines=false)
119
+ indent_string = indent_string || self[/^[ \t]/] || " "
120
+ substitutes = indent_empty_lines ? /^/ : /^(?!$)/
121
+ gsub(substitutes, indent_string * amount)
122
+ end
123
+ end
124
+
125
+ unless method_defined?(:indent!)
126
+ def indent!(amount, indent_string=nil, indent_empty_lines=false)
127
+ replace(indent(amount, indent_string, indent_empty_lines))
128
+ end
129
+ end
130
+
131
+ unless method_defined?(:last)
132
+ def last(limit=1)
133
+ return("") if limit.zero?
134
+ limit >= size ? self.dup : from(-limit)
135
+ end
136
+ end
137
+
138
+ def mixedcase?
139
+ !upcase? && !downcase?
140
+ end
141
+
142
+ unless method_defined?(:ordinal)
143
+ def ordinal
144
+ to_i.ordinal
145
+ end
146
+ end
147
+
148
+ unless method_defined?(:ordinalize)
149
+ def ordinalize
150
+ to_i.ordinalize
151
+ end
152
+ end
153
+
154
+ unless method_defined?(:parameterize)
155
+ def parameterize(sep="-")
156
+ underscore.
157
+ gsub(/\s+/, "_").
158
+ gsub("_", sep).
159
+ downcase
160
+ end
161
+ end
162
+
163
+ unless method_defined?(:parameterize!)
164
+ def parameterize!(sep="-")
165
+ replace(parameterize(sep))
166
+ end
167
+ end
168
+
169
+ def pollute(delimiter="^--^--^")
170
+ split('').map { |c| "#{c}#{delimiter}" }.join
171
+ end
172
+
173
+ unless method_defined?(:remove)
174
+ def remove(*patterns)
175
+ string = dup
176
+ patterns.each do |pattern|
177
+ string.gsub!(pattern, "")
178
+ end
179
+ string
180
+ end
181
+ end
182
+
183
+ unless method_defined?(:remove!)
184
+ def remove!(*patterns)
185
+ replace(remove(*patterns))
186
+ end
187
+ end
188
+
189
+ def remove_tags
190
+ gsub(/<\/?[^>]*>/, "")
191
+ end
192
+
193
+ def remove_tags!
194
+ replace(remove_tags)
195
+ end
196
+
197
+ def shift(*patterns)
198
+ string = dup
199
+ patterns.each do |pattern|
200
+ string.sub!(pattern, "")
201
+ end
202
+ string
203
+ end
204
+
205
+ def shift!(*patterns)
206
+ replace(shift(*patterns))
207
+ end
208
+
209
+ def slugify
210
+ gsub(/[^\x00-\x7F]+/, '').
211
+ gsub(/[^\w_ \-]+/i, '').
212
+ gsub(/[ \-]+/i, '-').
213
+ gsub(/^\-|\-$/i, '').
214
+ downcase
215
+ end
216
+
217
+ def slugify!
218
+ replace(slugify)
219
+ end
220
+
221
+ unless method_defined?(:squish)
222
+ def squish
223
+ strip.gsub(/\s+/, ' ')
224
+ end
225
+ end
226
+
227
+ unless method_defined?(:squish!)
228
+ def squish!
229
+ replace(squish)
230
+ end
231
+ end
232
+
233
+ unless method_defined?(:titleize)
234
+ def titleize
235
+ underscore.
236
+ humanize.
237
+ gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
238
+ end
239
+
240
+ alias_method :titlecase, :titleize
241
+ end
242
+
243
+ unless method_defined?(:titleize!)
244
+ def titleize!
245
+ replace(titleize)
246
+ end
247
+
248
+ alias_method :titlecase!, :titleize!
249
+ end
250
+
251
+ unless method_defined?(:to)
252
+ def to(position)
253
+ self[0..position]
254
+ end
255
+ end
256
+
257
+ unless method_defined?(:truncate)
258
+ def truncate(truncate_at, options={})
259
+ return(dup) unless length > truncate_at
260
+
261
+ omission = options.fetch(:omission, "...")
262
+ length_with_room_for_omission = truncate_at - omission.length
263
+
264
+ stop = if options.fetch(:separator, false)
265
+ rindex(options.fetch(:separator, ''), length_with_room_for_omission) || length_with_room_for_omission
266
+ else
267
+ length_with_room_for_omission
268
+ end
269
+
270
+ "#{self[0, stop]}#{omission}"
271
+ end
272
+ end
273
+
274
+ unless method_defined?(:truncate_words)
275
+ def truncate_words(words_count, options={})
276
+ sep = options[:separator] || /\s+/
277
+ sep = Regexp.escape(sep.to_s) unless Regexp === sep
278
+
279
+ if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
280
+ $1 + (options[:omission] || '...')
281
+ else
282
+ dup
283
+ end
284
+ end
285
+ end
286
+
287
+ unless method_defined?(:underscore)
288
+ def underscore
289
+ gsub(/::/, '/').
290
+ gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').
291
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
292
+ tr("-", "_").
293
+ downcase
294
+ end
295
+ end
296
+
297
+ unless method_defined?(:underscore!)
298
+ def underscore!
299
+ replace(underscore)
300
+ end
301
+ end
302
+
303
+ def unpollute(delimiter="^--^--^")
304
+ gsub(delimiter, "")
305
+ end
306
+
307
+ def upcase?
308
+ upcase == self
309
+ end
310
+
311
+ end
@@ -0,0 +1,150 @@
1
+ class Time
2
+
3
+ def format(string)
4
+ delimiters = string.scan /\W+/
5
+ formatters = string.scan /[a-z0-9_]+/i
6
+
7
+ format_units = {
8
+ "d" => "d",
9
+ "day" => "d",
10
+ "day_zero" => "d",
11
+ "dd" => "-d",
12
+ "Day" => "-d",
13
+ "day_unpadded" => "-d",
14
+ "ddd" => "_d",
15
+ "DAY" => "_d",
16
+ "day_blank" => "_d",
17
+ "dddd" => "j",
18
+ "day_of_the_year" => "j",
19
+ "m" => "m",
20
+ "month" => "m",
21
+ "month_zero" => "m",
22
+ "mm" => "-m",
23
+ "Month" => "-m",
24
+ "month_unpadded" => "-m",
25
+ "mmm" => "_m",
26
+ "MONTH" => "_m",
27
+ "month_blank" => "_m",
28
+ "mmmm" => "B",
29
+ "month_name" => "B",
30
+ "mmmmm" => "b",
31
+ "month_name_abbr" => "b",
32
+ "w" => "u",
33
+ "weekday" => "u",
34
+ "ww" => "w",
35
+ "weekday_offset" => "w",
36
+ "www" => "A",
37
+ "weekday_name" => "A",
38
+ "wwww" => "a",
39
+ "weekday_name_abbr" => "a",
40
+ "wwwww" => "W",
41
+ "week" => "W",
42
+ "wwwwww" => "U",
43
+ "week_offset" => "U",
44
+ "yy" => "y",
45
+ "yr" => "y",
46
+ "yyyy" => "Y",
47
+ "year" => "Y",
48
+ "h" => "H",
49
+ "hour" => "H",
50
+ "hour_zero" => "H",
51
+ "hh" => "k",
52
+ "HOUR" => "k",
53
+ "hour_blank" => "k",
54
+ "hhh" => "I",
55
+ "hour_imperical" => "I",
56
+ "hour_imperical_zero" => "I",
57
+ "hhhh" => "l",
58
+ "HOUR_IMPERICAL" => "l",
59
+ "hour_imperical_blank" => "l",
60
+ "ampm" => "P",
61
+ "meridian" => "P",
62
+ "AMPM" => "p",
63
+ "MERIDIAN" => "p",
64
+ "n" => "M",
65
+ "minute" => "M",
66
+ "s" => "S",
67
+ "second" => "S",
68
+ "z" => "z",
69
+ "time_zone" => "z",
70
+ "zz" => ":z",
71
+ "time_zone_offset" => ":z",
72
+ "zzz" => "::z",
73
+ "time_zone_offset_full" => "::z",
74
+ "zzzz" => "Z",
75
+ "time_zone_name" => "Z"
76
+ }
77
+
78
+ strftime(formatters.map { |f| "%#{format_units.fetch(f)}#{delimiters.shift || ''}" }.join(""))
79
+ end
80
+
81
+ def to_format(key)
82
+ format_units = {
83
+ month: "%m",
84
+ month_zero: "%m",
85
+ month_unpadded: "%-m",
86
+ month_blank: "%_m",
87
+ month_name: "%B",
88
+ month_name_abbr: "%b",
89
+ weekday: "%d",
90
+ weekday_zero: "%d",
91
+ weekday_unpadded: "%-d",
92
+ weekday_blank: "%_d",
93
+ weekday_name: "%A",
94
+ weekday_name_abbr: "%a",
95
+ yr: "%y",
96
+ year: "%Y",
97
+ hour: "%H",
98
+ hour_zero: "%H",
99
+ hour_blank: "%k",
100
+ hour_imperical: "%I",
101
+ hour_imperical_zero: "%I",
102
+ hour_imperical_blank: "%l",
103
+ ampm: "%P",
104
+ meridian: "%P",
105
+ minute: "%M",
106
+ second: "%S",
107
+ time_zone: "%z",
108
+ time_zone_offset: "%:z",
109
+ time_zone_offset_full: "%::z",
110
+ time_zone_name: "%Z",
111
+ date: "%B %-d, %Y",
112
+ date_abbr: "%b %-d, %Y",
113
+ date_iso: "%Y-%m-%d",
114
+ datetime: "%B %-d, %Y %H:%M",
115
+ datetime_abbr: "%b %-d, %Y %H:%M",
116
+ datetime_iso: "%Y-%m-%d %H:%M",
117
+ datetime_imperical: "%B %-d, %Y %I:%M %P",
118
+ datetime_imperical_abbr: "%b %-d, %Y %I:%M %P",
119
+ datetime_imperical_iso: "%Y-%m-%d %I:%M %P",
120
+ datetime_tzn: "%B %-d, %Y %H:%M %Z",
121
+ datetime_abbr_tzn: "%b %-d, %Y %H:%M %Z",
122
+ datetime_iso_tzn: "%Y-%m-%d %H:%M %z",
123
+ datetime_imperical_tzn: "%B %-d, %Y %I:%M %P %Z",
124
+ datetime_imperical_abbr_tzn: "%b %-d, %Y %I:%M %P %Z",
125
+ datetime_imperical_iso_tzn: "%Y-%m-%d %I:%M %P %z",
126
+ day: "%B %-d",
127
+ day_abbr: "%b %-d",
128
+ day_iso: "%m-%d",
129
+ daytime: "%B %-d %H:%M",
130
+ daytime_abbr: "%b %-d %H:%M",
131
+ daytime_iso: "%m-%d %H:%M",
132
+ daytime_imperical: "%B %-d %I:%M %P",
133
+ daytime_imperical_abbr: "%b %-d %I:%M %P",
134
+ daytime_imperical_iso: "%m-%d %I:%M %P",
135
+ time: "%H:%M",
136
+ time_zero: "%H:%M",
137
+ time_blank: "%k:%M",
138
+ time_tz: "%H:%M %z",
139
+ time_tzn: "%H:%M %Z",
140
+ time_imperical: "%I:%M %P",
141
+ time_imperical_zero: "%I:%M %P",
142
+ time_imperical_blank: "%l:%M %P",
143
+ time_imperical_tz: "%I:%M %P %z",
144
+ time_imperical_tzn: "%I:%M %P %Z"
145
+ }
146
+
147
+ strftime(format_units.fetch(key))
148
+ end
149
+
150
+ end