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,45 +0,0 @@
|
|
1
|
-
module Useful
|
2
|
-
module SinatraHelpers
|
3
|
-
module Erb
|
4
|
-
module Helpers
|
5
|
-
|
6
|
-
def sinatra_erb_helper_safe_id(id)
|
7
|
-
id.gsub(/\W/,'')
|
8
|
-
end
|
9
|
-
|
10
|
-
def sinatra_erb_helper_capture(*args, &block)
|
11
|
-
sinatra_erb_helper_with_output_buffer { block.call(*args) }
|
12
|
-
end
|
13
|
-
|
14
|
-
def sinatra_erb_helper_with_output_buffer(buf = '') #:nodoc:
|
15
|
-
@_out_buf, old_buffer = buf, @_out_buf
|
16
|
-
yield
|
17
|
-
@_out_buf
|
18
|
-
ensure
|
19
|
-
@_out_buf = old_buffer
|
20
|
-
end
|
21
|
-
|
22
|
-
def sinatra_erb_helper_hash_to_html_attrs(a_hash)
|
23
|
-
a_hash.collect{|key, val| "#{key}=\"#{val}\""}.join(' ')
|
24
|
-
end
|
25
|
-
|
26
|
-
def sinatra_erb_helper_disabled_option
|
27
|
-
'disabled'
|
28
|
-
end
|
29
|
-
|
30
|
-
def sinatra_erb_helper_checked_option
|
31
|
-
'checked'
|
32
|
-
end
|
33
|
-
|
34
|
-
def sinatra_erb_helper_multiple_option
|
35
|
-
'multiple'
|
36
|
-
end
|
37
|
-
|
38
|
-
def sinatra_erb_helper_multipart_option
|
39
|
-
'multipart/form-data'
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require File.join(File.dirname(__FILE__), 'tags.rb')
|
3
|
-
|
4
|
-
module Sinatra
|
5
|
-
module SinatraHelpers
|
6
|
-
module Erb
|
7
|
-
module Links
|
8
|
-
|
9
|
-
# helper to emulate action view's 'link_to'
|
10
|
-
# EX : link_to "google", "http://google.com"
|
11
|
-
# => <a href="http://google.com">google</a>
|
12
|
-
def link_to(content,href,options={})
|
13
|
-
options.update :href => href
|
14
|
-
tag(:a, options) { content }
|
15
|
-
end
|
16
|
-
|
17
|
-
def open_link_to(content, href, options={})
|
18
|
-
options[:onclick] = "javascript: window.open('#{href}'); return false;"
|
19
|
-
link_to(content, href, options)
|
20
|
-
end
|
21
|
-
|
22
|
-
def mail_link_to(email, options={})
|
23
|
-
link_to options[:label] || email, "mailto: #{email}"
|
24
|
-
end
|
25
|
-
|
26
|
-
def link_to_function(content, function, opts={})
|
27
|
-
opts ||= {}
|
28
|
-
opts[:href] ||= 'javascript: void(0);'
|
29
|
-
opts[:onclick] = "javascript: #{function}; return false;"
|
30
|
-
link_to content, opts[:href], opts
|
31
|
-
end
|
32
|
-
|
33
|
-
# helper to emulate 'image_tag'
|
34
|
-
# EX : image_tag 'logo.jpg'
|
35
|
-
# => <img src="images/logo.jpg" />
|
36
|
-
def image_tag(src,options={})
|
37
|
-
options[:src] = ['/'].include?(src[0..0]) ? src : "/images/#{src}"
|
38
|
-
tag(:img, options)
|
39
|
-
end
|
40
|
-
|
41
|
-
# helper to emulate 'stylesheet_link_tag'
|
42
|
-
# EX : stylesheet_link_tag 'default'
|
43
|
-
# => <link rel="stylesheet" href="/stylesheets/default.css" type="text/css" media="all" title="no title" charset="utf-8">
|
44
|
-
def stylesheet_link_tag(srcs,options={})
|
45
|
-
options[:media] ||= "screen"
|
46
|
-
options[:type] ||= "text/css"
|
47
|
-
options[:rel] ||= "stylesheet"
|
48
|
-
srcs.to_a.collect do |src|
|
49
|
-
options[:href] = "/stylesheets/#{src}.css#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
50
|
-
tag(:link, options)
|
51
|
-
end.join("\n")
|
52
|
-
end
|
53
|
-
|
54
|
-
# helper to emulate 'javascript_include_tag'
|
55
|
-
# EX : javascript_include_tag 'app'
|
56
|
-
# => <script src="/js/app.js" type="text/javascript" />
|
57
|
-
# EX : javascript_include_tag ['app', 'jquery']
|
58
|
-
# => <script src="/js/app.js" type="text/javascript" />
|
59
|
-
# => <script src="/js/jquery.js" type="text/javascript" />
|
60
|
-
def javascript_include_tag(srcs,options={})
|
61
|
-
options[:type] ||= "text/javascript"
|
62
|
-
srcs.to_a.collect do |src|
|
63
|
-
options[:src] = "/javascripts/#{src}.js#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
64
|
-
tag(:script, options) { '' }
|
65
|
-
end.join("\n")
|
66
|
-
end
|
67
|
-
|
68
|
-
# helper to emulate 'javascript_tag'
|
69
|
-
def javascript_tag(options={})
|
70
|
-
options[:type] ||= "text/javascript"
|
71
|
-
tag(:script, options) { yield }
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
Sinatra::Application.helpers Sinatra::SinatraHelpers::Erb::Links
|
79
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
|
3
|
-
module Useful
|
4
|
-
module SinatraHelpers
|
5
|
-
module Erb
|
6
|
-
module Partials
|
7
|
-
|
8
|
-
# helper to emulate rails 'render :partial' helper, using erb
|
9
|
-
# => taken from the sinatra book, http://sinatra-book.gittr.com/#implemention_of_rails_style_partials
|
10
|
-
# Render the page once:
|
11
|
-
# Usage: partial :foo
|
12
|
-
#
|
13
|
-
# foo will be rendered once for each element in the array, passing in a local variable named "foo"
|
14
|
-
# Usage: partial :foo, :collection => @my_foos
|
15
|
-
def partial(template, options={})
|
16
|
-
options.merge!(:layout => false)
|
17
|
-
path = template.to_s.split(File::SEPARATOR)
|
18
|
-
object = path[-1].to_sym
|
19
|
-
path[-1] = "_#{path[-1]}"
|
20
|
-
template = File.join(path).to_sym
|
21
|
-
raise 'partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
|
22
|
-
if collection = options.delete(:collection)
|
23
|
-
counter = 0
|
24
|
-
collection.inject([]) do |buffer, member|
|
25
|
-
counter += 1
|
26
|
-
buffer << erb(template, options.merge(:locals => {object => member, "#{object}_counter".to_sym => counter}))
|
27
|
-
end.join("\n")
|
28
|
-
else
|
29
|
-
if member = options.delete(:object)
|
30
|
-
options.merge!(:locals => {object => member})
|
31
|
-
end
|
32
|
-
erb(template, options)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
Sinatra::Application.helpers Useful::SinatraHelpers::Erb::Partials
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require File.join(File.dirname(__FILE__), 'helpers.rb')
|
3
|
-
|
4
|
-
module Useful
|
5
|
-
module SinatraHelpers
|
6
|
-
module Erb
|
7
|
-
module Tags
|
8
|
-
|
9
|
-
include Useful::SinatraHelpers::Erb::Helpers
|
10
|
-
|
11
|
-
def input_tag(type, name, value, options={}, &block)
|
12
|
-
options[:tag] ||= :input
|
13
|
-
options[:type] = type unless type.nil?
|
14
|
-
unless name.nil?
|
15
|
-
options[:name] = name
|
16
|
-
options[:id] ||= sinatra_erb_helper_safe_id(name)
|
17
|
-
end
|
18
|
-
options[:value] = value unless value.nil?
|
19
|
-
tag(options.delete(:tag), options, &block)
|
20
|
-
end
|
21
|
-
|
22
|
-
def clear_tag(options={})
|
23
|
-
options[:tag] ||= :div
|
24
|
-
options[:style] ||= ''
|
25
|
-
options[:style] = "clear: both;#{options[:style]}"
|
26
|
-
tag(options.delete(:tag), options) { '' }
|
27
|
-
end
|
28
|
-
|
29
|
-
# helpers to escape tag text content
|
30
|
-
include Rack::Utils
|
31
|
-
alias_method :h, :escape_html
|
32
|
-
|
33
|
-
# escape tag text content and format for text like display
|
34
|
-
def h_text(text, opts={})
|
35
|
-
h(text.to_s).
|
36
|
-
gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
|
37
|
-
split("\n").collect do |line|
|
38
|
-
line.nil? ? '': line.sub(/(\s+)?\S*/) {|lead| lead.gsub(/\s/,' ')}
|
39
|
-
end.join("\n"). # change any leading white spaces on a line to ' '
|
40
|
-
gsub(/\n/,'\1<br />') # newlines -> br added
|
41
|
-
end
|
42
|
-
|
43
|
-
# emulator for 'tag'
|
44
|
-
# EX : tag :h1, "shizam", :title => "shizam"
|
45
|
-
# => <h1 title="shizam">shizam</h1>
|
46
|
-
def tag(name, options={})
|
47
|
-
"<#{name.to_s} #{sinatra_erb_helper_hash_to_html_attrs(options)} #{block_given? ? ">#{yield}</#{name}" : "/"}>"
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
Sinatra::Application.helpers Useful::SinatraHelpers::Erb::Tags
|
56
|
-
|
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'openssl'
|
2
|
-
require 'net/smtp'
|
3
|
-
require 'tmail'
|
4
|
-
require 'sinatra/base'
|
5
|
-
require File.join([File.dirname(__FILE__), 'exceptions'])
|
6
|
-
require File.join([File.dirname(__FILE__), 'tls'])
|
7
|
-
|
8
|
-
module Useful
|
9
|
-
module SinatraHelpers
|
10
|
-
module Mailer
|
11
|
-
|
12
|
-
class Base
|
13
|
-
|
14
|
-
CONFIGS = [:server, :domain, :port, :reply_to, :username, :password, :authentication, :content_type, :charset]
|
15
|
-
CONFIGS.each do |config|
|
16
|
-
attr_reader config
|
17
|
-
end
|
18
|
-
attr_accessor :logger
|
19
|
-
|
20
|
-
def initialize(configs={})
|
21
|
-
CONFIGS.each do |config|
|
22
|
-
instance_variable_set("@#{config}", configs[config])
|
23
|
-
end
|
24
|
-
@authentication ||= :login
|
25
|
-
@reply_to ||= @username
|
26
|
-
@content_type ||= 'text/plain'
|
27
|
-
@charset ||= 'UTF-8'
|
28
|
-
@logger = block_given? ? yield : nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def send(settings={})
|
32
|
-
check_configs
|
33
|
-
[:to, :subject].each do |setting|
|
34
|
-
raise Useful::SinatraHelpers::Mailer::SendError, "cannot send, #{setting} not specified." unless settings[setting]
|
35
|
-
end
|
36
|
-
mail = generate_mail(settings)
|
37
|
-
mail.body = yield(mail) if block_given?
|
38
|
-
mail.body ||= ''
|
39
|
-
Sinatra::Application.environment.to_s == 'production' ? send_mail(mail) : log_mail(mail)
|
40
|
-
log(:info, "Sent '#{mail.subject}' to #{mail.to.join(',')}")
|
41
|
-
mail
|
42
|
-
end
|
43
|
-
|
44
|
-
protected
|
45
|
-
|
46
|
-
def check_configs
|
47
|
-
CONFIGS.each do |config|
|
48
|
-
raise Useful::SinatraHelpers::Mailer::ConfigError, "#{config} not configured." unless instance_variable_get("@#{config}")
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def generate_mail(settings)
|
53
|
-
mail = TMail::Mail.new
|
54
|
-
mail.to = Array.new([settings[:to]])
|
55
|
-
mail.from = @reply_to
|
56
|
-
mail.reply_to = @reply_to
|
57
|
-
mail.subject = settings[:subject]
|
58
|
-
mail.date = Time.now
|
59
|
-
mail.set_content_type @content_type
|
60
|
-
mail.charset = @charset
|
61
|
-
mail
|
62
|
-
end
|
63
|
-
|
64
|
-
def send_mail(mail)
|
65
|
-
raise Useful::SinatraHelpers::Mailer::SendError, "cannot send, bad (or empty) mail object given." unless mail
|
66
|
-
Net::SMTP.start(@server, @port, @domain, @username, @password, @authentication) do |server|
|
67
|
-
mail.to.each {|recipient| server.send_message(mail.to_s, mail.from, recipient) }
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def log_mail(mail)
|
72
|
-
log(:debug, mail.to_s)
|
73
|
-
end
|
74
|
-
|
75
|
-
def log(level, msg)
|
76
|
-
if(msg)
|
77
|
-
if @logger && @logger.respond_to?(level)
|
78
|
-
@logger.send(level.to_s, msg)
|
79
|
-
else
|
80
|
-
puts "** [#{level.to_s.upcase}]: [Mailer] #{msg}"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
# Note: This is a complete rip of http://github.com/ambethia/smtp-tls/tree/master
|
2
|
-
# => I chose to copy the source in here instead of add yet another gem depencency
|
3
|
-
# => I take no credit for this work, check out link for more info.
|
4
|
-
|
5
|
-
require 'net/smtp'
|
6
|
-
|
7
|
-
Net::SMTP.class_eval do
|
8
|
-
private
|
9
|
-
def do_start(helodomain, user, secret, authtype)
|
10
|
-
raise IOError, 'SMTP session already started' if @started
|
11
|
-
|
12
|
-
if RUBY_VERSION > "1.8.6"
|
13
|
-
check_auth_args user, secret if user or secret
|
14
|
-
else
|
15
|
-
check_auth_args user, secret, authtype if user or secret
|
16
|
-
end
|
17
|
-
|
18
|
-
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
|
19
|
-
@socket = Net::InternetMessageIO.new(sock)
|
20
|
-
@socket.read_timeout = 60 #@read_timeout
|
21
|
-
|
22
|
-
check_response(critical { recv_response() })
|
23
|
-
do_helo(helodomain)
|
24
|
-
|
25
|
-
if starttls
|
26
|
-
raise 'openssl library not installed' unless defined?(OpenSSL)
|
27
|
-
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
28
|
-
ssl.sync_close = true
|
29
|
-
ssl.connect
|
30
|
-
@socket = Net::InternetMessageIO.new(ssl)
|
31
|
-
@socket.read_timeout = 60 #@read_timeout
|
32
|
-
do_helo(helodomain)
|
33
|
-
end
|
34
|
-
|
35
|
-
authenticate user, secret, authtype if user
|
36
|
-
@started = true
|
37
|
-
ensure
|
38
|
-
unless @started
|
39
|
-
# authentication failed, cancel connection.
|
40
|
-
@socket.close if not @started and @socket and not @socket.closed?
|
41
|
-
@socket = nil
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def do_helo(helodomain)
|
46
|
-
begin
|
47
|
-
if @esmtp
|
48
|
-
ehlo helodomain
|
49
|
-
else
|
50
|
-
helo helodomain
|
51
|
-
end
|
52
|
-
rescue Net::ProtocolError
|
53
|
-
if @esmtp
|
54
|
-
@esmtp = false
|
55
|
-
@error_occured = false
|
56
|
-
retry
|
57
|
-
end
|
58
|
-
raise
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def starttls
|
63
|
-
getok('STARTTLS') rescue return false
|
64
|
-
return true
|
65
|
-
end
|
66
|
-
|
67
|
-
def quit
|
68
|
-
begin
|
69
|
-
getok('QUIT')
|
70
|
-
rescue EOFError
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|