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
@@ -0,0 +1,156 @@
|
|
1
|
+
require 'useful/erb_helpers/common'
|
2
|
+
require 'useful/erb_helpers/tags'
|
3
|
+
require 'useful/ruby_extensions/string' unless ::String.new.respond_to?('cgi_escape')
|
4
|
+
require 'useful/ruby_extensions/hash' unless ::Hash.new.respond_to?('to_http_query_str')
|
5
|
+
|
6
|
+
module Useful; end
|
7
|
+
module Useful::ErbHelpers; end
|
8
|
+
|
9
|
+
module Useful::ErbHelpers::Links
|
10
|
+
|
11
|
+
include Useful::ErbHelpers::Common
|
12
|
+
|
13
|
+
# helper to emulate action view's 'link_to'
|
14
|
+
# EX : link_to "http://google.com"
|
15
|
+
# => <a href="http://google.com">http://google.com</a>
|
16
|
+
# EX : link_to "google", "http://google.com"
|
17
|
+
# => <a href="http://google.com">google</a>
|
18
|
+
# EX : link_to "google", "http://google.com", :class => "awesome"
|
19
|
+
# => <a href="http://google.com" class="awesome">google</a>
|
20
|
+
# EX : link_to "http://google.com", :popup => true
|
21
|
+
# => <a href="http://google.com" onclick="javascript: window.open(this.href); return false;">http://google.com</a>
|
22
|
+
# EX : link_to "http://google.com", :popup => ['new_window_name', 'height=300,width=600']
|
23
|
+
# => <a href="http://google.com" onclick="javascript: window.open(this.href,'new_window_name','height=300,width=600'); return false;">http://google.com</a>
|
24
|
+
# EX : link_to "http://google.com", :confirm => "Are you sure?"
|
25
|
+
# => <a href="http://google.com" onclick="javascript: return confirm('Are you sure?');">http://google.com</a>
|
26
|
+
# EX : link_to "http://google.com", :confirm => "Are you sure?", :popup => true
|
27
|
+
# => <a href="http://google.com" onclick="javascript: if (confirm('Are you sure?')) { window.open(this.href); }; return false;">http://google.com</a>
|
28
|
+
def link_to(*args)
|
29
|
+
content, href, options = link_content_href_opts(args)
|
30
|
+
options.update :href => href
|
31
|
+
erb_helper_convert_options_to_javascript!(options)
|
32
|
+
tag(:a, options) { content }
|
33
|
+
end
|
34
|
+
|
35
|
+
# helper for generating a mailto:
|
36
|
+
# EX: mail_to "me@domain.com"
|
37
|
+
# # => <a href="mailto:me@domain.com">me@domain.com</a>
|
38
|
+
# EX: mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
|
39
|
+
# # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>
|
40
|
+
# EX: mail_to "me@domain.com", nil, :replace_at => " [at]", :replace_dot => " [dot] ", :disabled => true
|
41
|
+
# # => me [at] domain [dot] com
|
42
|
+
# EX: mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com", :subject => "This is an example email"
|
43
|
+
# # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
|
44
|
+
def mail_to(*args)
|
45
|
+
content, email, options = link_content_href_opts(args)
|
46
|
+
email_args = [:cc, :bcc, :subject, :body].inject({}) do |args, arg|
|
47
|
+
args[arg] = options.delete(arg) if options.has_key?(arg)
|
48
|
+
args
|
49
|
+
end
|
50
|
+
content.gsub!(/@/, options.delete(:replace_at)) if options.has_key?(:replace_at)
|
51
|
+
content.gsub!(/\./, options.delete(:replace_dot)) if options.has_key?(:replace_dot)
|
52
|
+
options.delete(:disabled) ? content : link_to(content, "mailto: #{email}#{email_args.to_http_query_str}", options)
|
53
|
+
end
|
54
|
+
|
55
|
+
def link_to_function(content, function, opts={})
|
56
|
+
opts ||= {}
|
57
|
+
opts[:href] ||= 'javascript: void(0);'
|
58
|
+
opts[:onclick] = "javascript: #{function}; return false;"
|
59
|
+
link_to content, opts[:href], opts
|
60
|
+
end
|
61
|
+
|
62
|
+
# helper to emulate 'image_tag'
|
63
|
+
# EX : image_tag 'logo.jpg'
|
64
|
+
# => <img src="/images/logo.jpg" />
|
65
|
+
# EX : image_tag '/better/logo.jpg'
|
66
|
+
# => <img src="/better/logo.jpg" />
|
67
|
+
def image_tag(source,options={})
|
68
|
+
options[:src] = ['/'].include?(source[0..0]) ? source : "/images/#{source}"
|
69
|
+
tag(:img, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
# helper to emulate activesupport's 'stylesheet_link_tag'
|
73
|
+
# EX : stylesheet_link_tag 'default'
|
74
|
+
# => <link rel="stylesheet" type="text/css" media="all" href="/stylesheets/default.css">
|
75
|
+
# EX : stylesheet_link_tag 'default', :environment => 'development'
|
76
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css?12345678">
|
77
|
+
# EX : stylesheet_link_tag 'default', :media => 'screen'
|
78
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
|
79
|
+
# EX : stylesheet_link_tag 'default', 'test, :media => 'screen'
|
80
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
|
81
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/test.css">
|
82
|
+
# EX : stylesheet_link_tag ['default', 'test], :media => 'screen'
|
83
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
|
84
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/test.css">
|
85
|
+
# EX : stylesheet_link_tag 'default', '/other_sheets/other', '/other_sheets/another.css'
|
86
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
|
87
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/other_sheets/other.css">
|
88
|
+
# => <link rel="stylesheet" type="text/css" media="screen" href="/other_sheets/another.css">
|
89
|
+
def stylesheet_link_tag(*args)
|
90
|
+
srcs, options = handle_srcs_options(args)
|
91
|
+
options[:rel] ||= "stylesheet"
|
92
|
+
options[:type] ||= "text/css"
|
93
|
+
options[:media] ||= "all"
|
94
|
+
Array(srcs).collect do |src|
|
95
|
+
#TODO: write sinatra helper to auto set env with Sinatra::Application.environment.to_s
|
96
|
+
options[:href] = build_src_href(src, :default_path => "stylesheets", :extension => ".css", :environment => options.delete(:environment))
|
97
|
+
tag(:link, options)
|
98
|
+
end.join("\n")
|
99
|
+
end
|
100
|
+
|
101
|
+
# helper to emulate 'javascript_tag'
|
102
|
+
def javascript_tag(options={})
|
103
|
+
options[:type] ||= "text/javascript"
|
104
|
+
tag(:script, options) { yield }
|
105
|
+
end
|
106
|
+
|
107
|
+
# helper to emulate 'javascript_include_tag'
|
108
|
+
# EX : javascript_include_tag 'app'
|
109
|
+
# => <script src="/js/app.js" type="text/javascript" />
|
110
|
+
# EX : javascript_include_tag ['app', 'jquery']
|
111
|
+
# => <script src="/js/app.js" type="text/javascript" />
|
112
|
+
# => <script src="/js/jquery.js" type="text/javascript" />
|
113
|
+
def javascript_include_tag(*args)
|
114
|
+
srcs, options = handle_srcs_options(args)
|
115
|
+
options[:type] ||= "text/javascript"
|
116
|
+
Array(srcs).collect do |src|
|
117
|
+
#TODO: write sinatra helper to auto set env with Sinatra::Application.environment.to_s
|
118
|
+
options[:src] = build_src_href(src, :default_path => "javascripts", :extension => ".js", :environment => options.delete(:environment))
|
119
|
+
tag(:script, options) { '' }
|
120
|
+
end.join("\n")
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.included(receiver)
|
124
|
+
receiver.send :include, Useful::ErbHelpers::Tags
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def link_content_href_opts(args)
|
130
|
+
if args[1] && !args[1].kind_of?(::Hash)
|
131
|
+
[args[0], args[1], (args[2] || {})]
|
132
|
+
else
|
133
|
+
[args[0], args[0].dup, (args[1] || {})]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def handle_srcs_options(args)
|
138
|
+
the_args = args.flatten
|
139
|
+
if the_args.last && the_args.last.kind_of?(::Hash)
|
140
|
+
[the_args[0..-2], the_args.last]
|
141
|
+
else
|
142
|
+
[the_args, {}]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def build_src_href(src, options)
|
147
|
+
href = ""
|
148
|
+
src = src.to_s
|
149
|
+
href += ['/'].include?(src[0..0]) ? src : "/#{options[:default_path]}/#{src}"
|
150
|
+
href += options[:extension] unless src.include?(options[:extension])
|
151
|
+
href += "?#{Time.now.to_i}" if options[:environment].to_s == 'development'
|
152
|
+
href
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# Note: these helpers are designed to be actionpack safe
|
2
|
+
# => these helpers are included in the useful/rails_extensions/erb
|
3
|
+
# => don't put any actionpack helpers in this module
|
4
|
+
|
5
|
+
require 'useful/erb_helpers/common'
|
6
|
+
require 'useful/erb_helpers/tags'
|
7
|
+
require 'useful/ruby_extensions/string' unless ::String.new.respond_to?('ends_with?')
|
8
|
+
unless ::Object.new.respond_to?('blank?') && ::Object.new.respond_to?('is_true?') && ::Object.new.respond_to?('returning')
|
9
|
+
require 'useful/ruby_extensions/object'
|
10
|
+
end
|
11
|
+
|
12
|
+
module Useful; end
|
13
|
+
module Useful::ErbHelpers; end
|
14
|
+
|
15
|
+
module Useful::ErbHelpers::Proper
|
16
|
+
|
17
|
+
include Useful::ErbHelpers::Common
|
18
|
+
|
19
|
+
# This one's a little different than the corresponding actionpack version:
|
20
|
+
# => ie. you don't pass an options string as the 2nd argument
|
21
|
+
# => you, instead, pass a block that should return the desired options string
|
22
|
+
# => ie, proper_select_tag('user') { '<option>test</option>' }
|
23
|
+
def proper_select_tag(name, options={}, &block)
|
24
|
+
html_name = (options[:multiple].is_true? && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
|
25
|
+
options[:multiple] = OPTIONS[:multiple] if options[:multiple] == true
|
26
|
+
options[:tag] = 'select'
|
27
|
+
@_out_buf ||= ''
|
28
|
+
@_out_buf << input_tag(nil, html_name, nil, options, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
# TODO: write tests
|
32
|
+
def proper_check_box_tag(*args)
|
33
|
+
name, value, checked, options = proper_check_radio_options('1', args)
|
34
|
+
options[:id] ||= erb_helper_common_safe_id(name.to_s)
|
35
|
+
options[:checked] = OPTIONS[:checked] if checked
|
36
|
+
label_text = options.delete(:label)
|
37
|
+
unchecked_value = options.delete(:unchecked_value)
|
38
|
+
unchecked_value = '0' if unchecked_value.nil?
|
39
|
+
disable_unchecked_value = options.delete(:disable_unchecked_value)
|
40
|
+
returning html = '' do
|
41
|
+
html << input_tag(:hidden, name, unchecked_value, :id => "#{options[:id]}_hidden") unless disable_unchecked_value.is_true?
|
42
|
+
html << input_tag(:checkbox, name, value, options)
|
43
|
+
html << tag(:label, :for => options[:id]) { label_text } unless label_text.blank?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# TODO: write tests
|
48
|
+
def proper_radio_button_tag(*args)
|
49
|
+
name, value, checked, options = proper_check_radio_options(nil, args)
|
50
|
+
options[:id] ||= erb_helper_common_safe_id(name.to_s)
|
51
|
+
options[:checked] = OPTIONS[:checked] if checked
|
52
|
+
label_text = options.delete(:label) || value.to_s.humanize
|
53
|
+
label_container_tag = options.delete(:tag) || :span
|
54
|
+
radio_button_str = input_tag(:radio, name, value, options)
|
55
|
+
returning html = '' do
|
56
|
+
html << if label_text.blank?
|
57
|
+
radio_button_str
|
58
|
+
else
|
59
|
+
tag(:label, :for => options[:id]) do
|
60
|
+
radio_button_str + tag(label_container_tag) { label_text }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.included(receiver)
|
67
|
+
receiver.send :include, Useful::ErbHelpers::Tags
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def proper_check_radio_options(default_value, args)
|
73
|
+
# args should be passed to proper checkbox and radiobutton tags like this:
|
74
|
+
# => name, value, checked, options={}
|
75
|
+
# this allows user to pass only args they care about,
|
76
|
+
# and default the ones they don't care about
|
77
|
+
if case_arg_options_for_length(4, args)
|
78
|
+
# args: name, value, checked, options --OR-- name, value, checked
|
79
|
+
args[1] ||= default_value # default the value
|
80
|
+
args[2] ||= false # default whether checked
|
81
|
+
args[3] ||= {} # default the options
|
82
|
+
[args[0], args[1], args[2], args[3]]
|
83
|
+
elsif case_arg_options_for_length(3, args)
|
84
|
+
# args: name, value, options --OR-- name, value
|
85
|
+
args[1] ||= default_value # default the value
|
86
|
+
args[2] ||= {} # default the options
|
87
|
+
checked = (args[2].delete(:checked) || false) rescue false
|
88
|
+
[args[0], args[1], checked, args[2]]
|
89
|
+
elsif case_arg_options_for_length(2, args)
|
90
|
+
# args: name, options --OR-- name
|
91
|
+
args[1] ||= {} # default the options
|
92
|
+
value = (args[1].delete(:value) || default_value) rescue default_value
|
93
|
+
checked = (args[1].delete(:checked) || false) rescue false
|
94
|
+
[args[0], value, checked, args[1]]
|
95
|
+
elsif case_arg_options_for_length(1, args)
|
96
|
+
# args: name
|
97
|
+
# => default everything
|
98
|
+
[args[0], default_value, false, {}]
|
99
|
+
else
|
100
|
+
# this is invalid
|
101
|
+
raise ArgumentError, "please specify 1 to 4 arguments for name, value, checked, and options"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def case_arg_options_for_length(length, args)
|
106
|
+
args.length == length || (args.length == (length-1) && !(args[(length-2)].kind_of?(::Hash) || args[(length-2)].nil?))
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Note: these helpers are designed to be actionpack safe
|
2
|
+
# => these helpers are included in the useful/rails_extensions/erb
|
3
|
+
# => don't put any actionpack helpers in this module
|
4
|
+
|
5
|
+
require 'useful/erb_helpers/common'
|
6
|
+
require 'useful/ruby_extensions/object' unless ::Object.new.respond_to?('blank?')
|
7
|
+
require 'useful/ruby_extensions/hash' unless ::Hash.new.respond_to?('to_html_attrs')
|
8
|
+
|
9
|
+
module Useful; end
|
10
|
+
module Useful::ErbHelpers; end
|
11
|
+
|
12
|
+
module Useful::ErbHelpers::Tags
|
13
|
+
|
14
|
+
include Useful::ErbHelpers::Common
|
15
|
+
|
16
|
+
def clear_tag(options={})
|
17
|
+
options[:tag] ||= :div
|
18
|
+
options[:style] ||= ''
|
19
|
+
options[:style] = "clear:both;#{" #{options[:style]}" unless options[:style].blank?}"
|
20
|
+
tag(options.delete(:tag), options) { '' }
|
21
|
+
end
|
22
|
+
|
23
|
+
# helpers to escape text for html
|
24
|
+
if defined?(::Rack::Utils)
|
25
|
+
include ::Rack::Utils
|
26
|
+
alias_method(:h, :escape_html)
|
27
|
+
end
|
28
|
+
if defined?(:h)
|
29
|
+
# escape tag text content and format for text-like display
|
30
|
+
def h_text(text, opts={})
|
31
|
+
h(text.to_s).
|
32
|
+
gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
|
33
|
+
split("\n").collect do |line|
|
34
|
+
line.nil? ? '': line.sub(/(\s+)?\S*/) {|lead| lead.gsub(/\s/,' ')}
|
35
|
+
end.join("\n"). # change any leading white spaces on a line to ' '
|
36
|
+
gsub(/\n/,'\1<br />') # newlines -> br added
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def input_tag(type, name, value=nil, options={}, &block)
|
41
|
+
options[:tag] ||= :input
|
42
|
+
options[:type] = type unless type.nil?
|
43
|
+
unless name.nil?
|
44
|
+
options[:name] = name
|
45
|
+
options[:id] ||= erb_helper_common_safe_id(name)
|
46
|
+
end
|
47
|
+
options[:value] = value unless value.nil?
|
48
|
+
options[:disabled] = OPTIONS[:disabled] if options[:disabled]
|
49
|
+
if block_given?
|
50
|
+
tag(options.delete(:tag), options) { erb_helper_common_capture(&block) }
|
51
|
+
else
|
52
|
+
tag(options.delete(:tag), options)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# emulator for rails' 'content_tag'
|
57
|
+
# EX : tag(:h1, :title => "shizam") { "shizam" }
|
58
|
+
# => <h1 title="shizam">shizam</h1>
|
59
|
+
def tag(name, options={})
|
60
|
+
"<#{name.to_s}#{" #{options.to_html_attrs}" unless options.empty?}#{block_given? ? ">#{yield}</#{name}" : " /"}>"
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RailsExtensions; end
|
3
|
+
|
4
|
+
module Useful::RailsExtensions::EnvironmentTests
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
def production?
|
9
|
+
RAILS_ENV == 'production'
|
10
|
+
end
|
11
|
+
|
12
|
+
def development?
|
13
|
+
RAILS_ENV == 'development'
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
module InstanceMethods
|
19
|
+
|
20
|
+
def production?
|
21
|
+
self.class.production?
|
22
|
+
end
|
23
|
+
|
24
|
+
def development?
|
25
|
+
self.class.development?
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.included(receiver)
|
31
|
+
receiver.extend ClassMethods
|
32
|
+
receiver.send :include, InstanceMethods
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
module ActiveRecord
|
38
|
+
class Base
|
39
|
+
include Useful::RailsExtensions::EnvironmentTests
|
40
|
+
end
|
41
|
+
class Observer
|
42
|
+
include Useful::RailsExtensions::EnvironmentTests
|
43
|
+
end
|
44
|
+
end
|
45
|
+
module ActionController
|
46
|
+
class Base
|
47
|
+
include Useful::RailsExtensions::EnvironmentTests
|
48
|
+
end
|
49
|
+
end
|
50
|
+
module ActionView
|
51
|
+
class Base
|
52
|
+
include Useful::RailsExtensions::EnvironmentTests
|
53
|
+
end
|
54
|
+
end
|
55
|
+
module ActionMailer
|
56
|
+
class Base
|
57
|
+
include Useful::RailsExtensions::EnvironmentTests
|
58
|
+
end
|
59
|
+
end
|
@@ -1,43 +1,42 @@
|
|
1
|
-
module Useful
|
2
|
-
|
3
|
-
module Array
|
4
|
-
|
5
|
-
module ClassMethods; end
|
6
|
-
def self.included(klass)
|
7
|
-
klass.extend(ClassMethods) if klass.kind_of?(Class)
|
8
|
-
end
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RubyExtensions; end
|
9
3
|
|
10
|
-
|
4
|
+
module Useful::RubyExtensions::Array
|
5
|
+
|
6
|
+
module ClassMethods; end
|
7
|
+
def self.included(klass)
|
8
|
+
klass.extend(ClassMethods) if klass.kind_of?(Class)
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
def merge(a1,a2)
|
14
|
-
a2.each{|item| a1 << item unless a1.include?(item)}
|
15
|
-
a1
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
11
|
+
module ClassMethods
|
19
12
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
13
|
+
# adds the contents of a2 to a1, removing duplicates
|
14
|
+
def merge(a1,a2)
|
15
|
+
a2.each{|item| a1 << item unless a1.include?(item)}
|
16
|
+
a1
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
alias chunks groups
|
21
|
+
# returns a new array, containing the contents of an_a with the contents of this array, removing duplicates
|
22
|
+
def merge(an_a)
|
23
|
+
self.class.merge(self.clone, an_a)
|
24
|
+
end
|
25
|
+
# adds the contents of an_a to this array, removing duplicates (inline version of #merge)
|
26
|
+
def merge!(an_a)
|
27
|
+
self.class.merge(self, an_a)
|
28
|
+
end
|
38
29
|
|
39
|
-
|
30
|
+
# split into an array of sized-arrays
|
31
|
+
def groups(size=1)
|
32
|
+
return [] if size <= 0
|
33
|
+
n,r = self.size.divmod(size)
|
34
|
+
grps = (0..(n-1)).collect{|i| self[i*size,size]}
|
35
|
+
r > 0 ? grps << self[-r,r] : grps
|
40
36
|
end
|
37
|
+
alias / groups
|
38
|
+
alias chunks groups
|
39
|
+
|
41
40
|
end
|
42
41
|
|
43
42
|
class Array
|
@@ -1,16 +1,16 @@
|
|
1
|
-
module Useful
|
2
|
-
|
3
|
-
module Date
|
4
|
-
|
5
|
-
def week_days_until(end_date)
|
6
|
-
week_days = (1..5)
|
7
|
-
raise ::ArgumentError, "End date cannot be nil." if end_date.nil?
|
8
|
-
raise ::ArgumentError, "End date cannot come before questioned date." if end_date < self
|
9
|
-
(self..end_date).to_a.select{|date| week_days.include?(date.wday)}.length
|
10
|
-
end
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RubyExtensions; end
|
11
3
|
|
12
|
-
|
4
|
+
module Useful::RubyExtensions::Date
|
5
|
+
|
6
|
+
WEEK_DAYS = (1..5)
|
7
|
+
|
8
|
+
def week_days_between(end_date)
|
9
|
+
raise ::ArgumentError, "End date cannot be nil." if end_date.nil?
|
10
|
+
raise ::ArgumentError, "End date cannot come before questioned date." if end_date < self
|
11
|
+
(self..end_date).to_a.select{|date| WEEK_DAYS.include?(date.wday)}.length
|
13
12
|
end
|
13
|
+
|
14
14
|
end
|
15
15
|
|
16
16
|
class Date
|
@@ -1,18 +1,21 @@
|
|
1
|
-
module Useful
|
2
|
-
|
3
|
-
module FalseClass
|
4
|
-
|
5
|
-
def to_affirmative
|
6
|
-
"No"
|
7
|
-
end
|
8
|
-
alias :to_casual_s :to_affirmative
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RubyExtensions; end
|
9
3
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
4
|
+
module Useful::RubyExtensions::FalseClass
|
5
|
+
|
6
|
+
def to_affirmative(abbreviate=false)
|
7
|
+
abbreviate ? "N" : "No"
|
15
8
|
end
|
9
|
+
alias :to_casual_s :to_affirmative
|
10
|
+
|
11
|
+
def to_i
|
12
|
+
0
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_boolean
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
16
19
|
end
|
17
20
|
|
18
21
|
class FalseClass
|
@@ -1,28 +1,59 @@
|
|
1
|
-
module Useful
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RubyExtensions; end
|
3
|
+
|
4
|
+
module Useful::RubyExtensions::Fixnum
|
5
|
+
|
6
|
+
# returns a string reprensentation of the number padded with pad_num to a specified length
|
7
|
+
def pad(length = 3, pad_num = 0)
|
8
|
+
self.to_s.rjust(length,pad_num.to_s) rescue self.to_s
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
# return the value in values that is nearest to the number
|
12
|
+
def to_nearest_value(values = [])
|
13
|
+
return self if values.length == 0
|
14
|
+
value = values.first.to_i rescue self
|
15
|
+
diff = (self-value).abs
|
16
|
+
values.each do |val|
|
17
|
+
if (self-val.to_i).abs < diff
|
18
|
+
diff = (self-val.to_i).abs
|
19
|
+
value = val.to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
value
|
23
|
+
end
|
24
|
+
|
25
|
+
module FromActivesupport
|
26
|
+
# All methods here will yield to their Activesupport versions, if defined
|
27
|
+
|
28
|
+
# Turns a number into an ordinal string used to denote the position in an
|
29
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
30
|
+
#
|
31
|
+
# Examples:
|
32
|
+
# 1.ordinalize # => "1st"
|
33
|
+
# 2.ordinalize # => "2nd"
|
34
|
+
# 1003.ordinalize # => "1003rd"
|
35
|
+
# 1004.ordinalize # => "1004th"
|
36
|
+
unless 1.respond_to?('ordinalize')
|
37
|
+
def ordinalize
|
38
|
+
if (11..13).include?(self.to_i % 100)
|
39
|
+
"#{self}th"
|
40
|
+
else
|
41
|
+
case self.to_i % 10
|
42
|
+
when 1; "#{self}st"
|
43
|
+
when 2; "#{self}nd"
|
44
|
+
when 3; "#{self}rd"
|
45
|
+
else "#{self}th"
|
19
46
|
end
|
20
47
|
end
|
21
|
-
value
|
22
48
|
end
|
23
|
-
|
24
49
|
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.included(receiver)
|
54
|
+
receiver.send :include, FromActivesupport
|
25
55
|
end
|
56
|
+
|
26
57
|
end
|
27
58
|
|
28
59
|
class Fixnum
|