kelredd-useful 0.1.25 → 0.2.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.
- data/README.rdoc +3 -2
- data/Rakefile +6 -7
- data/lib/useful/active_record_helpers/mysql_migration_helpers.rb +64 -65
- data/lib/useful/active_record_helpers.rb +1 -1
- data/lib/useful/cap_tasks/app_role_migrations.rb +41 -0
- data/lib/useful/cap_tasks/disable_migrate.rb +15 -0
- data/lib/useful/cap_tasks/git_query_revision_remote.rb +31 -0
- data/lib/useful/cap_tasks/passenger_deploy.rb +44 -0
- data/lib/useful/cap_tasks/rack_cache.rb +17 -0
- data/lib/useful/cap_tasks.rb +1 -3
- data/lib/useful/erb_helpers/common.rb +86 -0
- data/lib/useful/erb_helpers/forms.rb +118 -0
- data/lib/useful/erb_helpers/links.rb +156 -0
- data/lib/useful/erb_helpers/proper.rb +109 -0
- data/lib/useful/erb_helpers/tags.rb +64 -0
- data/lib/useful/erb_helpers.rb +3 -0
- data/lib/useful/rails_helpers/environment_tests.rb +59 -0
- data/lib/useful/rails_helpers/erb.rb +3 -0
- data/lib/useful/rails_helpers.rb +3 -0
- data/lib/useful/ruby_extensions/array.rb +33 -34
- data/lib/useful/ruby_extensions/date.rb +11 -11
- data/lib/useful/ruby_extensions/false_class.rb +16 -13
- data/lib/useful/ruby_extensions/fixnum.rb +50 -19
- data/lib/useful/ruby_extensions/hash.rb +157 -91
- data/lib/useful/ruby_extensions/nil_class.rb +26 -0
- data/lib/useful/ruby_extensions/numeric.rb +239 -229
- data/lib/useful/ruby_extensions/object.rb +126 -11
- data/lib/useful/ruby_extensions/string.rb +259 -33
- data/lib/useful/ruby_extensions/true_class.rb +16 -13
- data/lib/useful/ruby_extensions.rb +1 -1
- data/lib/useful/ruby_extensions_with_activesupport.rb +2 -0
- data/lib/useful/shoulda_macros/test_unit.rb +58 -0
- data/lib/useful/version.rb +2 -2
- data/lib/useful.rb +1 -1
- metadata +19 -38
- data/lib/useful/cap_tasks/cache.rb +0 -5
- data/lib/useful/cap_tasks/gemsconfig.rb +0 -14
- data/lib/useful/rails_extensions/environment_tests.rb +0 -60
- data/lib/useful/rails_extensions.rb +0 -3
- data/lib/useful/ruby_extensions_from_rails/date.rb +0 -244
- data/lib/useful/ruby_extensions_from_rails/duration.rb +0 -99
- data/lib/useful/ruby_extensions_from_rails/fixnum.rb +0 -32
- data/lib/useful/ruby_extensions_from_rails/hash.rb +0 -50
- data/lib/useful/ruby_extensions_from_rails/numeric.rb +0 -60
- data/lib/useful/ruby_extensions_from_rails/object.rb +0 -79
- data/lib/useful/ruby_extensions_from_rails/string.rb +0 -174
- data/lib/useful/ruby_extensions_from_rails/time.rb +0 -320
- data/lib/useful/ruby_extensions_from_rails.rb +0 -3
- data/lib/useful/sinatra_helpers/environment_tests.rb +0 -19
- data/lib/useful/sinatra_helpers/erb/error_pages.rb +0 -25
- data/lib/useful/sinatra_helpers/erb/forms.rb +0 -131
- data/lib/useful/sinatra_helpers/erb/helpers.rb +0 -45
- data/lib/useful/sinatra_helpers/erb/links.rb +0 -79
- data/lib/useful/sinatra_helpers/erb/partials.rb +0 -41
- data/lib/useful/sinatra_helpers/erb/tags.rb +0 -56
- data/lib/useful/sinatra_helpers/erb.rb +0 -3
- data/lib/useful/sinatra_helpers/mailer/base.rb +0 -89
- data/lib/useful/sinatra_helpers/mailer/exceptions.rb +0 -17
- data/lib/useful/sinatra_helpers/mailer/tls.rb +0 -73
- data/lib/useful/sinatra_helpers/mailer.rb +0 -3
- data/lib/useful/sinatra_helpers.rb +0 -3
@@ -1,174 +0,0 @@
|
|
1
|
-
module Useful
|
2
|
-
module RubyExtensionsFromRails
|
3
|
-
module String
|
4
|
-
|
5
|
-
module ClassMethods; end
|
6
|
-
def self.included(klass)
|
7
|
-
klass.extend(ClassMethods) if klass.kind_of?(Class)
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
|
12
|
-
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
|
13
|
-
# is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
|
14
|
-
#
|
15
|
-
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
16
|
-
#
|
17
|
-
# Examples:
|
18
|
-
# "active_record".camelize # => "ActiveRecord"
|
19
|
-
# "active_record".camelize(:lower) # => "activeRecord"
|
20
|
-
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
21
|
-
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
22
|
-
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
23
|
-
if first_letter_in_uppercase
|
24
|
-
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
25
|
-
else
|
26
|
-
lower_case_and_underscored_word[0..0].downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
|
31
|
-
#
|
32
|
-
# Changes '::' to '/' to convert namespaces to paths.
|
33
|
-
#
|
34
|
-
# Examples:
|
35
|
-
# "ActiveRecord".underscore # => "active_record"
|
36
|
-
# "ActiveRecord::Errors".underscore # => active_record/errors
|
37
|
-
def underscore(camel_cased_word)
|
38
|
-
camel_cased_word.to_s.gsub(/::/, '/').
|
39
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
40
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
41
|
-
tr("-", "_").
|
42
|
-
downcase
|
43
|
-
end
|
44
|
-
|
45
|
-
# Replaces underscores with dashes in the string.
|
46
|
-
#
|
47
|
-
# Example:
|
48
|
-
# "puni_puni" # => "puni-puni"
|
49
|
-
def dasherize(underscored_word)
|
50
|
-
underscored_word.gsub(/_/, '-')
|
51
|
-
end
|
52
|
-
|
53
|
-
# Removes the module part from the expression in the string.
|
54
|
-
#
|
55
|
-
# Examples:
|
56
|
-
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
|
57
|
-
# "Inflections".demodulize # => "Inflections"
|
58
|
-
def demodulize(class_name_in_module)
|
59
|
-
class_name_in_module.to_s.gsub(/^.*::/, '')
|
60
|
-
end
|
61
|
-
|
62
|
-
# Create a class name from a string like Rails does for table names to models.
|
63
|
-
# Note that this returns a string and not a Class. (To convert to an actual class
|
64
|
-
# follow +classify+ with +constantize+.)
|
65
|
-
#
|
66
|
-
# Examples:
|
67
|
-
# "egg_and_hams".classify # => "EggAndHam"
|
68
|
-
# "posts".classify # => "Post"
|
69
|
-
#
|
70
|
-
# Singular names are not handled correctly:
|
71
|
-
# "business".classify # => "Busines"
|
72
|
-
def classify(class_str)
|
73
|
-
# strip out any leading schema name
|
74
|
-
camelize(class_str.to_s.sub(/.*\./, ''))
|
75
|
-
end
|
76
|
-
|
77
|
-
# Ruby 1.9 introduces an inherit argument for Module#const_get and
|
78
|
-
# #const_defined? and changes their default behavior.
|
79
|
-
if Module.method(:const_get).arity == 1
|
80
|
-
# Tries to find a constant with the name specified in the argument string:
|
81
|
-
#
|
82
|
-
# "Module".constantize # => Module
|
83
|
-
# "Test::Unit".constantize # => Test::Unit
|
84
|
-
#
|
85
|
-
# The name is assumed to be the one of a top-level constant, no matter whether
|
86
|
-
# it starts with "::" or not. No lexical context is taken into account:
|
87
|
-
#
|
88
|
-
# C = 'outside'
|
89
|
-
# module M
|
90
|
-
# C = 'inside'
|
91
|
-
# C # => 'inside'
|
92
|
-
# "C".constantize # => 'outside', same as ::C
|
93
|
-
# end
|
94
|
-
#
|
95
|
-
# NameError is raised when the name is not in CamelCase or the constant is
|
96
|
-
# unknown.
|
97
|
-
def constantize(camel_cased_word)
|
98
|
-
names = camel_cased_word.split('::')
|
99
|
-
names.shift if names.empty? || names.first.empty?
|
100
|
-
|
101
|
-
constant = ::Object
|
102
|
-
names.each do |name|
|
103
|
-
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
104
|
-
end
|
105
|
-
constant
|
106
|
-
end
|
107
|
-
else
|
108
|
-
def constantize(camel_cased_word) #:nodoc:
|
109
|
-
names = camel_cased_word.split('::')
|
110
|
-
names.shift if names.empty? || names.first.empty?
|
111
|
-
|
112
|
-
constant = ::Object
|
113
|
-
names.each do |name|
|
114
|
-
constant = constant.const_get(name, false) || constant.const_missing(name)
|
115
|
-
end
|
116
|
-
constant
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
end #ClassMethods
|
121
|
-
|
122
|
-
def camelize(first_letter = :upper)
|
123
|
-
case first_letter
|
124
|
-
when :upper then ::String.camelize(self, true)
|
125
|
-
when :lower then ::String.camelize(self, false)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
alias_method :camelcase, :camelize
|
129
|
-
|
130
|
-
def underscore
|
131
|
-
self.class.underscore(self)
|
132
|
-
end
|
133
|
-
|
134
|
-
def dasherize
|
135
|
-
self.class.dasherize(self)
|
136
|
-
end
|
137
|
-
|
138
|
-
def demodulize
|
139
|
-
self.class.demodulize(self)
|
140
|
-
end
|
141
|
-
|
142
|
-
def classify
|
143
|
-
self.class.classify(self)
|
144
|
-
end
|
145
|
-
|
146
|
-
def constantize
|
147
|
-
self.class.constantize(self)
|
148
|
-
end
|
149
|
-
|
150
|
-
def ends_with?(suffix)
|
151
|
-
suffix = suffix.to_s
|
152
|
-
self[-suffix.length, suffix.length] == suffix
|
153
|
-
end
|
154
|
-
|
155
|
-
def starts_with?(prefix)
|
156
|
-
prefix = prefix.to_s
|
157
|
-
self[0, prefix.length] == prefix
|
158
|
-
end
|
159
|
-
|
160
|
-
def to_datetime
|
161
|
-
::DateTime.civil(*::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 }) rescue nil
|
162
|
-
end
|
163
|
-
|
164
|
-
def to_date
|
165
|
-
::Date.civil(*::Date._parse(self, false).values_at(:year, :mon, :mday).map { |arg| arg || 0 }) rescue nil
|
166
|
-
end
|
167
|
-
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
class String
|
173
|
-
include Useful::RubyExtensionsFromRails::String
|
174
|
-
end
|
@@ -1,320 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'object') unless Object.new.respond_to?(:acts_like?)
|
2
|
-
require File.join(File.dirname(__FILE__), 'duration')
|
3
|
-
require File.join(File.dirname(__FILE__), 'numeric')
|
4
|
-
|
5
|
-
module Useful
|
6
|
-
module RubyExtensionsFromRails
|
7
|
-
module Time
|
8
|
-
|
9
|
-
def self.included(base) #:nodoc:
|
10
|
-
base.extend ClassMethods
|
11
|
-
base.class_eval do
|
12
|
-
alias_method :to_default_s, :to_s
|
13
|
-
alias_method :to_s, :to_formatted_s
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
18
|
-
DATE_FORMATS = {
|
19
|
-
:db => "%Y-%m-%d %H:%M:%S",
|
20
|
-
:number => "%Y%m%d%H%M%S",
|
21
|
-
:time => "%H:%M",
|
22
|
-
:short => "%d %b %H:%M",
|
23
|
-
:long => "%B %d, %Y %H:%M",
|
24
|
-
:long_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}, %Y %H:%M") },
|
25
|
-
:rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") }
|
26
|
-
}
|
27
|
-
|
28
|
-
module ClassMethods
|
29
|
-
# Return the number of days in the given month.
|
30
|
-
# If no year is specified, it will use the current year.
|
31
|
-
def days_in_month(month, year = now.year)
|
32
|
-
return 29 if month == 2 && ::Date.gregorian_leap?(year)
|
33
|
-
COMMON_YEAR_DAYS_IN_MONTH[month]
|
34
|
-
end
|
35
|
-
|
36
|
-
# Returns a new Time if requested year can be accommodated by Ruby's Time class
|
37
|
-
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
|
38
|
-
# otherwise returns a DateTime
|
39
|
-
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
|
40
|
-
::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
|
41
|
-
end
|
42
|
-
|
43
|
-
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
|
44
|
-
def utc_time(*args)
|
45
|
-
time_with_datetime_fallback(:utc, *args)
|
46
|
-
end
|
47
|
-
|
48
|
-
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
|
49
|
-
def local_time(*args)
|
50
|
-
time_with_datetime_fallback(:local, *args)
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns Time.now
|
54
|
-
def current
|
55
|
-
::Time.now
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
# Enable more predictable duck-typing on Time-like classes. See
|
61
|
-
# Object#acts_like?.
|
62
|
-
def acts_like_time?
|
63
|
-
true
|
64
|
-
end
|
65
|
-
|
66
|
-
# Tells whether the Time object's time lies in the past
|
67
|
-
def past?
|
68
|
-
self < ::Time.current
|
69
|
-
end
|
70
|
-
|
71
|
-
# Tells whether the Time object's time is today
|
72
|
-
def today?
|
73
|
-
self.to_date == ::Date.current
|
74
|
-
end
|
75
|
-
|
76
|
-
# Tells whether the Time object's time lies in the future
|
77
|
-
def future?
|
78
|
-
self > ::Time.current
|
79
|
-
end
|
80
|
-
|
81
|
-
# Seconds since midnight: Time.now.seconds_since_midnight
|
82
|
-
def seconds_since_midnight
|
83
|
-
self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
|
84
|
-
end
|
85
|
-
|
86
|
-
# Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
|
87
|
-
# (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
|
88
|
-
# minute is passed, then sec and usec is set to 0.
|
89
|
-
def change(options)
|
90
|
-
::Time.send(
|
91
|
-
self.utc? ? :utc_time : :local_time,
|
92
|
-
options[:year] || self.year,
|
93
|
-
options[:month] || self.month,
|
94
|
-
options[:day] || self.day,
|
95
|
-
options[:hour] || self.hour,
|
96
|
-
options[:min] || (options[:hour] ? 0 : self.min),
|
97
|
-
options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
|
98
|
-
options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
|
99
|
-
)
|
100
|
-
end
|
101
|
-
|
102
|
-
# Uses Date to provide precise Time calculations for years, months, and days.
|
103
|
-
# The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
|
104
|
-
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
|
105
|
-
# <tt>:minutes</tt>, <tt>:seconds</tt>.
|
106
|
-
def advance(options)
|
107
|
-
unless options[:weeks].nil?
|
108
|
-
options[:weeks], partial_weeks = options[:weeks].divmod(1)
|
109
|
-
options[:days] = (options[:days] || 0) + 7 * partial_weeks
|
110
|
-
end
|
111
|
-
|
112
|
-
unless options[:days].nil?
|
113
|
-
options[:days], partial_days = options[:days].divmod(1)
|
114
|
-
options[:hours] = (options[:hours] || 0) + 24 * partial_days
|
115
|
-
end
|
116
|
-
|
117
|
-
d = to_date.advance(options)
|
118
|
-
time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
119
|
-
seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
|
120
|
-
seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
|
121
|
-
end
|
122
|
-
|
123
|
-
# Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
|
124
|
-
def ago(seconds)
|
125
|
-
self.since(-seconds)
|
126
|
-
end
|
127
|
-
|
128
|
-
# Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
|
129
|
-
# the Numeric extension.
|
130
|
-
def since(seconds)
|
131
|
-
f = seconds.since(self)
|
132
|
-
if Useful::RubyExtensionsFromRails::Duration === seconds
|
133
|
-
f
|
134
|
-
else
|
135
|
-
initial_dst = self.dst? ? 1 : 0
|
136
|
-
final_dst = f.dst? ? 1 : 0
|
137
|
-
(seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
|
138
|
-
end
|
139
|
-
end
|
140
|
-
alias :in :since
|
141
|
-
|
142
|
-
# Returns a new Time representing the time a number of specified months ago
|
143
|
-
def months_ago(months)
|
144
|
-
advance(:months => -months)
|
145
|
-
end
|
146
|
-
|
147
|
-
# Returns a new Time representing the time a number of specified months in the future
|
148
|
-
def months_since(months)
|
149
|
-
advance(:months => months)
|
150
|
-
end
|
151
|
-
|
152
|
-
# Returns a new Time representing the time a number of specified years ago
|
153
|
-
def years_ago(years)
|
154
|
-
advance(:years => -years)
|
155
|
-
end
|
156
|
-
|
157
|
-
# Returns a new Time representing the time a number of specified years in the future
|
158
|
-
def years_since(years)
|
159
|
-
advance(:years => years)
|
160
|
-
end
|
161
|
-
|
162
|
-
# Short-hand for years_ago(1)
|
163
|
-
def last_year
|
164
|
-
years_ago(1)
|
165
|
-
end
|
166
|
-
|
167
|
-
# Short-hand for years_since(1)
|
168
|
-
def next_year
|
169
|
-
years_since(1)
|
170
|
-
end
|
171
|
-
|
172
|
-
# Short-hand for months_ago(1)
|
173
|
-
def last_month
|
174
|
-
months_ago(1)
|
175
|
-
end
|
176
|
-
|
177
|
-
# Short-hand for months_since(1)
|
178
|
-
def next_month
|
179
|
-
months_since(1)
|
180
|
-
end
|
181
|
-
|
182
|
-
# Returns a new Time representing the start of the day (0:00)
|
183
|
-
def beginning_of_day
|
184
|
-
(self - self.seconds_since_midnight).change(:usec => 0)
|
185
|
-
end
|
186
|
-
alias :midnight :beginning_of_day
|
187
|
-
alias :at_midnight :beginning_of_day
|
188
|
-
alias :at_beginning_of_day :beginning_of_day
|
189
|
-
|
190
|
-
# Returns a new Time representing the end of the day (23:59:59)
|
191
|
-
def end_of_day
|
192
|
-
change(:hour => 23, :min => 59, :sec => 59)
|
193
|
-
end
|
194
|
-
alias :at_end_of_day :end_of_day
|
195
|
-
|
196
|
-
# Returns a new Time representing the "start" of this week (Monday, 0:00)
|
197
|
-
def beginning_of_week
|
198
|
-
days_to_monday = self.wday!=0 ? self.wday-1 : 6
|
199
|
-
(self - days_to_monday.days).midnight
|
200
|
-
end
|
201
|
-
alias :monday :beginning_of_week
|
202
|
-
alias :at_beginning_of_week :beginning_of_week
|
203
|
-
|
204
|
-
# Returns a new Time representing the end of this week (Sunday, 23:59:59)
|
205
|
-
def end_of_week
|
206
|
-
days_to_sunday = self.wday!=0 ? 7-self.wday : 0
|
207
|
-
(self + days_to_sunday.days).end_of_day
|
208
|
-
end
|
209
|
-
alias :at_end_of_week :end_of_week
|
210
|
-
|
211
|
-
# Returns a new Time representing the start of the given day in next week (default is Monday).
|
212
|
-
def next_week(day = :monday)
|
213
|
-
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
|
214
|
-
since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
|
215
|
-
end
|
216
|
-
|
217
|
-
# Returns a new Time representing the start of the month (1st of the month, 0:00)
|
218
|
-
def beginning_of_month
|
219
|
-
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
220
|
-
change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
221
|
-
end
|
222
|
-
alias :at_beginning_of_month :beginning_of_month
|
223
|
-
|
224
|
-
# Returns a new Time representing the end of the month (last day of the month, 0:00)
|
225
|
-
def end_of_month
|
226
|
-
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
227
|
-
last_day = ::Time.days_in_month( self.month, self.year )
|
228
|
-
change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0)
|
229
|
-
end
|
230
|
-
alias :at_end_of_month :end_of_month
|
231
|
-
|
232
|
-
# Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
|
233
|
-
def beginning_of_quarter
|
234
|
-
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
|
235
|
-
end
|
236
|
-
alias :at_beginning_of_quarter :beginning_of_quarter
|
237
|
-
|
238
|
-
# Returns a new Time representing the end of the quarter (last day of march, june, september, december, 23:59:59)
|
239
|
-
def end_of_quarter
|
240
|
-
beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
|
241
|
-
end
|
242
|
-
alias :at_end_of_quarter :end_of_quarter
|
243
|
-
|
244
|
-
# Returns a new Time representing the start of the year (1st of january, 0:00)
|
245
|
-
def beginning_of_year
|
246
|
-
change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
247
|
-
end
|
248
|
-
alias :at_beginning_of_year :beginning_of_year
|
249
|
-
|
250
|
-
# Returns a new Time representing the end of the year (31st of december, 23:59:59)
|
251
|
-
def end_of_year
|
252
|
-
change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59)
|
253
|
-
end
|
254
|
-
alias :at_end_of_year :end_of_year
|
255
|
-
|
256
|
-
# Convenience method which returns a new Time representing the time 1 day ago
|
257
|
-
def yesterday
|
258
|
-
advance(:days => -1)
|
259
|
-
end
|
260
|
-
|
261
|
-
# Convenience method which returns a new Time representing the time 1 day since the instance time
|
262
|
-
def tomorrow
|
263
|
-
advance(:days => 1)
|
264
|
-
end
|
265
|
-
|
266
|
-
# Converts to a formatted string. See DATE_FORMATS for builtin formats.
|
267
|
-
#
|
268
|
-
# This method is aliased to <tt>to_s</tt>.
|
269
|
-
#
|
270
|
-
# time = Time.now # => Thu Jan 18 06:10:17 CST 2007
|
271
|
-
#
|
272
|
-
# time.to_formatted_s(:time) # => "06:10:17"
|
273
|
-
# time.to_s(:time) # => "06:10:17"
|
274
|
-
#
|
275
|
-
# time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
|
276
|
-
# time.to_formatted_s(:number) # => "20070118061017"
|
277
|
-
# time.to_formatted_s(:short) # => "18 Jan 06:10"
|
278
|
-
# time.to_formatted_s(:long) # => "January 18, 2007 06:10"
|
279
|
-
# time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
|
280
|
-
# time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
|
281
|
-
#
|
282
|
-
# == Adding your own time formats to +to_formatted_s+
|
283
|
-
# You can add your own formats to the Time::DATE_FORMATS hash.
|
284
|
-
# Use the format name as the hash key and either a strftime string
|
285
|
-
# or Proc instance that takes a time argument as the value.
|
286
|
-
#
|
287
|
-
# # config/initializers/time_formats.rb
|
288
|
-
# Time::DATE_FORMATS[:month_and_year] = "%B %Y"
|
289
|
-
# Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
|
290
|
-
def to_formatted_s(format = :default)
|
291
|
-
return to_default_s unless formatter = DATE_FORMATS[format]
|
292
|
-
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
293
|
-
end
|
294
|
-
|
295
|
-
# Returns the UTC offset as an +HH:MM formatted string.
|
296
|
-
#
|
297
|
-
# Time.local(2000).formatted_offset # => "-06:00"
|
298
|
-
# Time.local(2000).formatted_offset(false) # => "-0600"
|
299
|
-
def formatted_offset(colon = true, alternate_utc_string = nil)
|
300
|
-
utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon)
|
301
|
-
end
|
302
|
-
|
303
|
-
# Converts a Time object to a Date, dropping hour, minute, and second precision.
|
304
|
-
#
|
305
|
-
# my_time = Time.now # => Mon Nov 12 22:59:51 -0500 2007
|
306
|
-
# my_time.to_date # => Mon, 12 Nov 2007
|
307
|
-
#
|
308
|
-
# your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
|
309
|
-
# your_time.to_date # => Tue, 13 Jan 2009
|
310
|
-
def to_date
|
311
|
-
::Date.new(year, month, day)
|
312
|
-
end
|
313
|
-
|
314
|
-
end
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
class Time
|
319
|
-
include Useful::RubyExtensionsFromRails::Time
|
320
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
|
3
|
-
module Useful
|
4
|
-
module SinatraHelpers
|
5
|
-
module EnvironmentTests
|
6
|
-
|
7
|
-
def production?
|
8
|
-
Sinatra::Application.environment.to_s == 'production'
|
9
|
-
end
|
10
|
-
def development?
|
11
|
-
Sinatra::Application.environment.to_s == 'development'
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
Sinatra::Application.helpers Useful::SinatraHelpers::EnvironmentTests
|
19
|
-
Sinatra::Application.register Useful::SinatraHelpers::EnvironmentTests
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
|
3
|
-
module Useful
|
4
|
-
module SinatraHelpers
|
5
|
-
module Erb
|
6
|
-
module ErrorPages
|
7
|
-
|
8
|
-
def self.registered(app)
|
9
|
-
app.configure :production do
|
10
|
-
app.not_found do
|
11
|
-
erb :'404.html'
|
12
|
-
end
|
13
|
-
app.error do
|
14
|
-
@err = request.env['sinatra_error']
|
15
|
-
erb :'500.html'
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
Sinatra::Application.register Useful::SinatraHelpers::Erb::ErrorPages
|
@@ -1,131 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require File.join(File.dirname(__FILE__), 'helpers.rb')
|
3
|
-
require File.join(File.dirname(__FILE__), 'tags.rb')
|
4
|
-
|
5
|
-
module Useful
|
6
|
-
module SinatraHelpers
|
7
|
-
module Erb
|
8
|
-
module Forms
|
9
|
-
|
10
|
-
include Useful::SinatraHelpers::Erb::Helpers
|
11
|
-
|
12
|
-
def form_tag(url, options={}, &block)
|
13
|
-
options[:method] = 'post' unless ['get','post','put','delete'].include?(options[:method])
|
14
|
-
options.update :action => url
|
15
|
-
if multipart = options.delete(:multipart)
|
16
|
-
options[:enctype] = sinatra_erb_helper_multipart_option
|
17
|
-
end
|
18
|
-
if block_given?
|
19
|
-
@_out_buf << tag(:form, options) { sinatra_erb_helper_capture(&block) }
|
20
|
-
else
|
21
|
-
tag(:form, options)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def field_set_tag(legend=nil, options={}, &block)
|
26
|
-
legend_html = legend.nil? ? '' : tag(:legend) { legend.to_s }
|
27
|
-
if block_given?
|
28
|
-
@_out_buf << tag(:fieldset, options) { legend_html + sinatra_erb_helper_capture(&block) }
|
29
|
-
else
|
30
|
-
tag(:fieldset, options) { legend_html }
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def label_tag(name, value=nil, options={})
|
35
|
-
value ||= name.to_s.capitalize
|
36
|
-
options.update :for => name.to_s
|
37
|
-
tag(:label, options) { value }
|
38
|
-
end
|
39
|
-
|
40
|
-
def hidden_field_tag(name, value=nil, options={})
|
41
|
-
input_tag('hidden', name, value, options)
|
42
|
-
end
|
43
|
-
|
44
|
-
def password_field_tag(name="password", value=nil, options={})
|
45
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
46
|
-
input_tag('password', name, value, options)
|
47
|
-
end
|
48
|
-
|
49
|
-
def file_field_tag(name, options={})
|
50
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
51
|
-
input_tag('file', name, nil, options)
|
52
|
-
end
|
53
|
-
|
54
|
-
def check_box_tag(name, label=nil, value='1', checked=false, options={})
|
55
|
-
tag_name = options.delete(:tag) || :div
|
56
|
-
if options.has_key?(:class)
|
57
|
-
options[:class] += ' checkbox'
|
58
|
-
else
|
59
|
-
options[:class] = 'checkbox'
|
60
|
-
end
|
61
|
-
options[:id] ||= sinatra_erb_helper_safe_id(rand(1000).to_s)
|
62
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
63
|
-
options[:checked] = sinatra_erb_helper_checked_option if checked
|
64
|
-
input_str = input_tag('checkbox', name, value, options)
|
65
|
-
if label.nil?
|
66
|
-
input_str
|
67
|
-
else
|
68
|
-
tag(tag_name, :class => 'checkbox') { input_str + label_tag(options[:id], label) }
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def radio_button_tag(name, value, label=nil, checked=false, options={})
|
73
|
-
tag_name = options.delete(:tag) || :span
|
74
|
-
if options.has_key?(:class)
|
75
|
-
options[:class] += ' radiobutton'
|
76
|
-
else
|
77
|
-
options[:class] = 'radiobutton'
|
78
|
-
end
|
79
|
-
label ||= value.to_s.capitalize
|
80
|
-
options[:id] ||= sinatra_erb_helper_safe_id(rand(1000).to_s)
|
81
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
82
|
-
options[:checked] = sinatra_erb_helper_checked_option if checked
|
83
|
-
input_str = input_tag('radio', name, value, options)
|
84
|
-
if label.nil?
|
85
|
-
input_str
|
86
|
-
else
|
87
|
-
label_tag(options[:id], input_str + tag(tag_name) { label }, :class => options.delete(:class))
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def select_tag(name, options={}, &block)
|
92
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
93
|
-
html_name = (options[:multiple] == true && !name.to_s[(name.to_s.length-2)..-1] == "[]") ? "#{name}[]" : name
|
94
|
-
options[:multiple] = sinatra_erb_helper_multiple_option if options[:multiple] == true
|
95
|
-
options[:tag] = 'select'
|
96
|
-
if block_given?
|
97
|
-
@_out_buf << input_tag(:select, html_name, nil, options) { sinatra_erb_helper_capture(&block) }
|
98
|
-
else
|
99
|
-
input_tag(:select, html_name, nil, options)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def text_area_tag(name, content=nil, options={})
|
104
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
105
|
-
options[:tag] = 'textarea'
|
106
|
-
input_tag(nil, name, nil, options) { content || '' }
|
107
|
-
end
|
108
|
-
|
109
|
-
def text_field_tag(name, value=nil, options={})
|
110
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
111
|
-
input_tag('text', name, value, options)
|
112
|
-
end
|
113
|
-
|
114
|
-
def submit_tag(value="Save", options={})
|
115
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
116
|
-
input_tag('submit', 'commit', value, options)
|
117
|
-
end
|
118
|
-
|
119
|
-
def image_submit_tag(source, options={})
|
120
|
-
options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
|
121
|
-
options[:src] = source
|
122
|
-
options[:alt] ||= 'Save'
|
123
|
-
input_tag('image', nil, nil, options)
|
124
|
-
end
|
125
|
-
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
Sinatra::Application.helpers Useful::SinatraHelpers::Erb::Forms
|