automate-it 0.9.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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.hgignore +10 -0
- data/.loadpath +5 -0
- data/.project +17 -0
- data/CHANGES.txt +314 -0
- data/Hoe.rake +40 -0
- data/Manifest.txt +164 -0
- data/README.txt +40 -0
- data/Rakefile +256 -0
- data/TESTING.txt +57 -0
- data/TODO.txt +50 -0
- data/TUTORIAL.txt +391 -0
- data/automate-it.gemspec +25 -0
- data/bin/ai +3 -0
- data/bin/aifield +75 -0
- data/bin/aissh +93 -0
- data/bin/aitag +134 -0
- data/bin/automateit +133 -0
- data/docs/friendly_errors.txt +50 -0
- data/docs/previews.txt +86 -0
- data/examples/basic/Rakefile +26 -0
- data/examples/basic/config/automateit_env.rb +16 -0
- data/examples/basic/config/fields.yml +3 -0
- data/examples/basic/config/tags.yml +7 -0
- data/examples/basic/dist/README.txt +9 -0
- data/examples/basic/dist/myapp_server.erb +30 -0
- data/examples/basic/install.log +15 -0
- data/examples/basic/lib/README.txt +10 -0
- data/examples/basic/recipes/README.txt +4 -0
- data/examples/basic/recipes/install.rb +61 -0
- data/examples/basic/recipes/uninstall.rb +6 -0
- data/gpl.txt +674 -0
- data/helpers/cpan_wrapper.pl +220 -0
- data/helpers/which.cmd +7 -0
- data/lib/automateit.rb +55 -0
- data/lib/automateit/account_manager.rb +114 -0
- data/lib/automateit/account_manager/base.rb +138 -0
- data/lib/automateit/account_manager/etc.rb +128 -0
- data/lib/automateit/account_manager/nscd.rb +33 -0
- data/lib/automateit/account_manager/passwd_expect.rb +40 -0
- data/lib/automateit/account_manager/passwd_pty.rb +69 -0
- data/lib/automateit/account_manager/posix.rb +138 -0
- data/lib/automateit/address_manager.rb +88 -0
- data/lib/automateit/address_manager/base.rb +171 -0
- data/lib/automateit/address_manager/bsd.rb +28 -0
- data/lib/automateit/address_manager/freebsd.rb +59 -0
- data/lib/automateit/address_manager/linux.rb +42 -0
- data/lib/automateit/address_manager/openbsd.rb +66 -0
- data/lib/automateit/address_manager/portable.rb +37 -0
- data/lib/automateit/address_manager/sunos.rb +34 -0
- data/lib/automateit/cli.rb +85 -0
- data/lib/automateit/common.rb +65 -0
- data/lib/automateit/constants.rb +35 -0
- data/lib/automateit/download_manager.rb +48 -0
- data/lib/automateit/edit_manager.rb +321 -0
- data/lib/automateit/error.rb +10 -0
- data/lib/automateit/field_manager.rb +103 -0
- data/lib/automateit/interpreter.rb +631 -0
- data/lib/automateit/package_manager.rb +257 -0
- data/lib/automateit/package_manager/apt.rb +27 -0
- data/lib/automateit/package_manager/cpan.rb +101 -0
- data/lib/automateit/package_manager/dpkg.rb +54 -0
- data/lib/automateit/package_manager/egg.rb +64 -0
- data/lib/automateit/package_manager/gem.rb +201 -0
- data/lib/automateit/package_manager/pear.rb +95 -0
- data/lib/automateit/package_manager/pecl.rb +80 -0
- data/lib/automateit/package_manager/portage.rb +69 -0
- data/lib/automateit/package_manager/yum.rb +65 -0
- data/lib/automateit/platform_manager.rb +49 -0
- data/lib/automateit/platform_manager/darwin.rb +30 -0
- data/lib/automateit/platform_manager/debian.rb +26 -0
- data/lib/automateit/platform_manager/freebsd.rb +29 -0
- data/lib/automateit/platform_manager/gentoo.rb +26 -0
- data/lib/automateit/platform_manager/lsb.rb +44 -0
- data/lib/automateit/platform_manager/openbsd.rb +28 -0
- data/lib/automateit/platform_manager/struct.rb +80 -0
- data/lib/automateit/platform_manager/sunos.rb +39 -0
- data/lib/automateit/platform_manager/uname.rb +29 -0
- data/lib/automateit/platform_manager/windows.rb +40 -0
- data/lib/automateit/plugin.rb +7 -0
- data/lib/automateit/plugin/base.rb +32 -0
- data/lib/automateit/plugin/driver.rb +256 -0
- data/lib/automateit/plugin/manager.rb +224 -0
- data/lib/automateit/project.rb +493 -0
- data/lib/automateit/root.rb +17 -0
- data/lib/automateit/service_manager.rb +93 -0
- data/lib/automateit/service_manager/chkconfig.rb +39 -0
- data/lib/automateit/service_manager/rc_update.rb +37 -0
- data/lib/automateit/service_manager/sysv.rb +139 -0
- data/lib/automateit/service_manager/update_rcd.rb +35 -0
- data/lib/automateit/shell_manager.rb +316 -0
- data/lib/automateit/shell_manager/base_link.rb +67 -0
- data/lib/automateit/shell_manager/link.rb +24 -0
- data/lib/automateit/shell_manager/portable.rb +523 -0
- data/lib/automateit/shell_manager/symlink.rb +32 -0
- data/lib/automateit/shell_manager/which_base.rb +30 -0
- data/lib/automateit/shell_manager/which_unix.rb +16 -0
- data/lib/automateit/shell_manager/which_windows.rb +20 -0
- data/lib/automateit/tag_manager.rb +127 -0
- data/lib/automateit/tag_manager/struct.rb +121 -0
- data/lib/automateit/tag_manager/tag_parser.rb +93 -0
- data/lib/automateit/tag_manager/yaml.rb +29 -0
- data/lib/automateit/template_manager.rb +56 -0
- data/lib/automateit/template_manager/base.rb +181 -0
- data/lib/automateit/template_manager/erb.rb +17 -0
- data/lib/ext/metaclass.rb +17 -0
- data/lib/ext/object.rb +18 -0
- data/lib/ext/shell_escape.rb +7 -0
- data/lib/hashcache.rb +22 -0
- data/lib/helpful_erb.rb +63 -0
- data/lib/inactive_support.rb +53 -0
- data/lib/inactive_support/basic_object.rb +6 -0
- data/lib/inactive_support/clean_logger.rb +127 -0
- data/lib/inactive_support/core_ext/array/extract_options.rb +19 -0
- data/lib/inactive_support/core_ext/blank.rb +50 -0
- data/lib/inactive_support/core_ext/class/attribute_accessors.rb +48 -0
- data/lib/inactive_support/core_ext/class/inheritable_attributes.rb +140 -0
- data/lib/inactive_support/core_ext/enumerable.rb +63 -0
- data/lib/inactive_support/core_ext/hash/keys.rb +54 -0
- data/lib/inactive_support/core_ext/module/aliasing.rb +70 -0
- data/lib/inactive_support/core_ext/numeric/time.rb +91 -0
- data/lib/inactive_support/core_ext/string/inflections.rb +153 -0
- data/lib/inactive_support/core_ext/symbol.rb +14 -0
- data/lib/inactive_support/core_ext/time/conversions.rb +96 -0
- data/lib/inactive_support/duration.rb +96 -0
- data/lib/inactive_support/inflections.rb +53 -0
- data/lib/inactive_support/inflector.rb +282 -0
- data/lib/nested_error.rb +33 -0
- data/lib/nitpick.rb +33 -0
- data/lib/queued_logger.rb +68 -0
- data/lib/tempster.rb +250 -0
- data/misc/index_gem_repository.rb +304 -0
- data/misc/setup_egg.rb +12 -0
- data/misc/setup_gem_dependencies.sh +6 -0
- data/misc/setup_rubygems.sh +21 -0
- metadata +279 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
module InactiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Hash #:nodoc:
|
4
|
+
module Keys
|
5
|
+
# Return a new hash with all keys converted to strings.
|
6
|
+
def stringify_keys
|
7
|
+
inject({}) do |options, (key, value)|
|
8
|
+
options[key.to_s] = value
|
9
|
+
options
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Destructively convert all keys to strings.
|
14
|
+
def stringify_keys!
|
15
|
+
keys.each do |key|
|
16
|
+
unless key.class.to_s == "String" # weird hack to make the tests run when string_ext_test.rb is also running
|
17
|
+
self[key.to_s] = self[key]
|
18
|
+
delete(key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return a new hash with all keys converted to symbols.
|
25
|
+
def symbolize_keys
|
26
|
+
inject({}) do |options, (key, value)|
|
27
|
+
options[key.to_sym || key] = value
|
28
|
+
options
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Destructively convert all keys to symbols.
|
33
|
+
def symbolize_keys!
|
34
|
+
self.replace(self.symbolize_keys)
|
35
|
+
end
|
36
|
+
|
37
|
+
alias_method :to_options, :symbolize_keys
|
38
|
+
alias_method :to_options!, :symbolize_keys!
|
39
|
+
|
40
|
+
# Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
|
41
|
+
# Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbol
|
42
|
+
# as keys, this will fail.
|
43
|
+
# examples:
|
44
|
+
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
|
45
|
+
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): years, name"
|
46
|
+
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
|
47
|
+
def assert_valid_keys(*valid_keys)
|
48
|
+
unknown_keys = keys - [valid_keys].flatten
|
49
|
+
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Module
|
2
|
+
# Encapsulates the common pattern of:
|
3
|
+
#
|
4
|
+
# alias_method :foo_without_feature, :foo
|
5
|
+
# alias_method :foo, :foo_with_feature
|
6
|
+
#
|
7
|
+
# With this, you simply do:
|
8
|
+
#
|
9
|
+
# alias_method_chain :foo, :feature
|
10
|
+
#
|
11
|
+
# And both aliases are set up for you.
|
12
|
+
#
|
13
|
+
# Query and bang methods (foo?, foo!) keep the same punctuation:
|
14
|
+
#
|
15
|
+
# alias_method_chain :foo?, :feature
|
16
|
+
#
|
17
|
+
# is equivalent to
|
18
|
+
#
|
19
|
+
# alias_method :foo_without_feature?, :foo?
|
20
|
+
# alias_method :foo?, :foo_with_feature?
|
21
|
+
#
|
22
|
+
# so you can safely chain foo, foo?, and foo! with the same feature.
|
23
|
+
def alias_method_chain(target, feature)
|
24
|
+
# Strip out punctuation on predicates or bang methods since
|
25
|
+
# e.g. target?_without_feature is not a valid method name.
|
26
|
+
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
27
|
+
yield(aliased_target, punctuation) if block_given?
|
28
|
+
|
29
|
+
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
|
30
|
+
|
31
|
+
alias_method without_method, target
|
32
|
+
alias_method target, with_method
|
33
|
+
|
34
|
+
case
|
35
|
+
when public_method_defined?(without_method)
|
36
|
+
public target
|
37
|
+
when protected_method_defined?(without_method)
|
38
|
+
protected target
|
39
|
+
when private_method_defined?(without_method)
|
40
|
+
private target
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Allows you to make aliases for attributes, which includes
|
45
|
+
# getter, setter, and query methods.
|
46
|
+
#
|
47
|
+
# Example:
|
48
|
+
#
|
49
|
+
# class Content < ActiveRecord::Base
|
50
|
+
# # has a title attribute
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# class Email < Content
|
54
|
+
# alias_attribute :subject, :title
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# e = Email.find(1)
|
58
|
+
# e.title # => "Superstars"
|
59
|
+
# e.subject # => "Superstars"
|
60
|
+
# e.subject? # => true
|
61
|
+
# e.subject = "Megastars"
|
62
|
+
# e.title # => "Megastars"
|
63
|
+
def alias_attribute(new_name, old_name)
|
64
|
+
module_eval <<-STR, __FILE__, __LINE__+1
|
65
|
+
def #{new_name}; self.#{old_name}; end
|
66
|
+
def #{new_name}?; self.#{old_name}?; end
|
67
|
+
def #{new_name}=(v); self.#{old_name} = v; end
|
68
|
+
STR
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module InactiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Numeric #:nodoc:
|
4
|
+
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
5
|
+
#
|
6
|
+
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
7
|
+
# as well as adding or subtracting their results from a Time object. For example:
|
8
|
+
#
|
9
|
+
# # equivalent to Time.now.advance(:months => 1)
|
10
|
+
# 1.month.from_now
|
11
|
+
#
|
12
|
+
# # equivalent to Time.now.advance(:years => 2)
|
13
|
+
# 2.years.from_now
|
14
|
+
#
|
15
|
+
# # equivalent to Time.now.advance(:months => 4, :years => 5)
|
16
|
+
# (4.months + 5.years).from_now
|
17
|
+
#
|
18
|
+
# While these methods provide precise calculation when used as in the examples above, care
|
19
|
+
# should be taken to note that this is not true if the result of `months', `years', etc is
|
20
|
+
# converted before use:
|
21
|
+
#
|
22
|
+
# # equivalent to 30.days.to_i.from_now
|
23
|
+
# 1.month.to_i.from_now
|
24
|
+
#
|
25
|
+
# # equivalent to 365.25.days.to_f.from_now
|
26
|
+
# 1.year.to_f.from_now
|
27
|
+
#
|
28
|
+
# In such cases, Ruby's core
|
29
|
+
# Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
|
30
|
+
# Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
|
31
|
+
# date and time arithmetic
|
32
|
+
module Time
|
33
|
+
def seconds
|
34
|
+
InactiveSupport::Duration.new(self, [[:seconds, self]])
|
35
|
+
end
|
36
|
+
alias :second :seconds
|
37
|
+
|
38
|
+
def minutes
|
39
|
+
InactiveSupport::Duration.new(self * 60, [[:seconds, self * 60]])
|
40
|
+
end
|
41
|
+
alias :minute :minutes
|
42
|
+
|
43
|
+
def hours
|
44
|
+
InactiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
|
45
|
+
end
|
46
|
+
alias :hour :hours
|
47
|
+
|
48
|
+
def days
|
49
|
+
InactiveSupport::Duration.new(self * 24.hours, [[:days, self]])
|
50
|
+
end
|
51
|
+
alias :day :days
|
52
|
+
|
53
|
+
def weeks
|
54
|
+
InactiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
|
55
|
+
end
|
56
|
+
alias :week :weeks
|
57
|
+
|
58
|
+
def fortnights
|
59
|
+
InactiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]])
|
60
|
+
end
|
61
|
+
alias :fortnight :fortnights
|
62
|
+
|
63
|
+
def months
|
64
|
+
InactiveSupport::Duration.new(self * 30.days, [[:months, self]])
|
65
|
+
end
|
66
|
+
alias :month :months
|
67
|
+
|
68
|
+
def years
|
69
|
+
InactiveSupport::Duration.new(self * 365.25.days, [[:years, self]])
|
70
|
+
end
|
71
|
+
alias :year :years
|
72
|
+
|
73
|
+
# Reads best without arguments: 10.minutes.ago
|
74
|
+
def ago(time = ::Time.now)
|
75
|
+
time - self
|
76
|
+
end
|
77
|
+
|
78
|
+
# Reads best with argument: 10.minutes.until(time)
|
79
|
+
alias :until :ago
|
80
|
+
|
81
|
+
# Reads best with argument: 10.minutes.since(time)
|
82
|
+
def since(time = ::Time.now)
|
83
|
+
time + self
|
84
|
+
end
|
85
|
+
|
86
|
+
# Reads best without arguments: 10.minutes.from_now
|
87
|
+
alias :from_now :since
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'inactive_support/inflector'
|
2
|
+
|
3
|
+
module InactiveSupport #:nodoc:
|
4
|
+
module CoreExtensions #:nodoc:
|
5
|
+
module String #:nodoc:
|
6
|
+
# String inflections define new methods on the String class to transform names for different purposes.
|
7
|
+
# For instance, you can figure out the name of a database from the name of a class.
|
8
|
+
# "ScaleScore".tableize => "scale_scores"
|
9
|
+
module Inflections
|
10
|
+
# Returns the plural form of the word in the string.
|
11
|
+
#
|
12
|
+
# Examples
|
13
|
+
# "post".pluralize #=> "posts"
|
14
|
+
# "octopus".pluralize #=> "octopi"
|
15
|
+
# "sheep".pluralize #=> "sheep"
|
16
|
+
# "words".pluralize #=> "words"
|
17
|
+
# "the blue mailman".pluralize #=> "the blue mailmen"
|
18
|
+
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
19
|
+
def pluralize
|
20
|
+
Inflector.pluralize(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
# The reverse of pluralize, returns the singular form of a word in a string.
|
24
|
+
#
|
25
|
+
# Examples
|
26
|
+
# "posts".singularize #=> "post"
|
27
|
+
# "octopi".singularize #=> "octopus"
|
28
|
+
# "sheep".singluarize #=> "sheep"
|
29
|
+
# "word".singluarize #=> "word"
|
30
|
+
# "the blue mailmen".singularize #=> "the blue mailman"
|
31
|
+
# "CamelOctopi".singularize #=> "CamelOctopus"
|
32
|
+
def singularize
|
33
|
+
Inflector.singularize(self)
|
34
|
+
end
|
35
|
+
|
36
|
+
# By default, camelize converts strings to UpperCamelCase. If the argument to camelize
|
37
|
+
# is set to ":lower" then camelize produces lowerCamelCase.
|
38
|
+
#
|
39
|
+
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
40
|
+
#
|
41
|
+
# Examples
|
42
|
+
# "active_record".camelize #=> "ActiveRecord"
|
43
|
+
# "active_record".camelize(:lower) #=> "activeRecord"
|
44
|
+
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
45
|
+
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
|
46
|
+
def camelize(first_letter = :upper)
|
47
|
+
case first_letter
|
48
|
+
when :upper then Inflector.camelize(self, true)
|
49
|
+
when :lower then Inflector.camelize(self, false)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
alias_method :camelcase, :camelize
|
53
|
+
|
54
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
55
|
+
# a nicer looking title. Titleize is meant for creating pretty output. It is not
|
56
|
+
# used in the Rails internals.
|
57
|
+
#
|
58
|
+
# titleize is also aliased as as titlecase
|
59
|
+
#
|
60
|
+
# Examples
|
61
|
+
# "man from the boondocks".titleize #=> "Man From The Boondocks"
|
62
|
+
# "x-men: the last stand".titleize #=> "X Men: The Last Stand"
|
63
|
+
def titleize
|
64
|
+
Inflector.titleize(self)
|
65
|
+
end
|
66
|
+
alias_method :titlecase, :titleize
|
67
|
+
|
68
|
+
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
|
69
|
+
#
|
70
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
71
|
+
#
|
72
|
+
# Examples
|
73
|
+
# "ActiveRecord".underscore #=> "active_record"
|
74
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
75
|
+
def underscore
|
76
|
+
Inflector.underscore(self)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Replaces underscores with dashes in the string.
|
80
|
+
#
|
81
|
+
# Example
|
82
|
+
# "puni_puni" #=> "puni-puni"
|
83
|
+
def dasherize
|
84
|
+
Inflector.dasherize(self)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Removes the module part from the expression in the string
|
88
|
+
#
|
89
|
+
# Examples
|
90
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
91
|
+
# "Inflections".demodulize #=> "Inflections"
|
92
|
+
def demodulize
|
93
|
+
Inflector.demodulize(self)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Create the name of a table like Rails does for models to table names. This method
|
97
|
+
# uses the pluralize method on the last word in the string.
|
98
|
+
#
|
99
|
+
# Examples
|
100
|
+
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
101
|
+
# "egg_and_ham".tableize #=> "egg_and_hams"
|
102
|
+
# "fancyCategory".tableize #=> "fancy_categories"
|
103
|
+
def tableize
|
104
|
+
Inflector.tableize(self)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Create a class name from a table name like Rails does for table names to models.
|
108
|
+
# Note that this returns a string and not a Class. (To convert to an actual class
|
109
|
+
# follow classify with constantize.)
|
110
|
+
#
|
111
|
+
# Examples
|
112
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
113
|
+
# "post".classify #=> "Post"
|
114
|
+
def classify
|
115
|
+
Inflector.classify(self)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
119
|
+
# Like titleize, this is meant for creating pretty output.
|
120
|
+
#
|
121
|
+
# Examples
|
122
|
+
# "employee_salary" #=> "Employee salary"
|
123
|
+
# "author_id" #=> "Author"
|
124
|
+
def humanize
|
125
|
+
Inflector.humanize(self)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Creates a foreign key name from a class name.
|
129
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
130
|
+
# the method should put '_' between the name and 'id'.
|
131
|
+
#
|
132
|
+
# Examples
|
133
|
+
# "Message".foreign_key #=> "message_id"
|
134
|
+
# "Message".foreign_key(false) #=> "messageid"
|
135
|
+
# "Admin::Post".foreign_key #=> "post_id"
|
136
|
+
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
137
|
+
Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Constantize tries to find a declared constant with the name specified
|
141
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
142
|
+
# or is not initialized.
|
143
|
+
#
|
144
|
+
# Examples
|
145
|
+
# "Module".constantize #=> Module
|
146
|
+
# "Class".constantize #=> Class
|
147
|
+
def constantize
|
148
|
+
Inflector.constantize(self)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
unless :test.respond_to?(:to_proc)
|
2
|
+
class Symbol
|
3
|
+
# Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
|
4
|
+
#
|
5
|
+
# # The same as people.collect { |p| p.name }
|
6
|
+
# people.collect(&:name)
|
7
|
+
#
|
8
|
+
# # The same as people.select { |p| p.manager? }.collect { |p| p.salary }
|
9
|
+
# people.select(&:manager?).collect(&:salary)
|
10
|
+
def to_proc
|
11
|
+
Proc.new { |*args| args.shift.__send__(self, *args) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module InactiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Time #:nodoc:
|
4
|
+
# Getting times in different convenient string representations and other objects
|
5
|
+
module Conversions
|
6
|
+
DATE_FORMATS = {
|
7
|
+
:db => "%Y-%m-%d %H:%M:%S",
|
8
|
+
:time => "%H:%M",
|
9
|
+
:short => "%d %b %H:%M",
|
10
|
+
:long => "%B %d, %Y %H:%M",
|
11
|
+
:long_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}, %Y %H:%M") },
|
12
|
+
:rfc822 => "%a, %d %b %Y %H:%M:%S %z"
|
13
|
+
}
|
14
|
+
|
15
|
+
def self.included(base)
|
16
|
+
base.class_eval do
|
17
|
+
unless instance_methods.include? "to_default_s"
|
18
|
+
alias_method :to_default_s, :to_s
|
19
|
+
alias_method :to_s, :to_formatted_s
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Convert to a formatted string - see DATE_FORMATS for predefined formats.
|
25
|
+
# You can also add your own formats to the DATE_FORMATS constant and use them with this method.
|
26
|
+
#
|
27
|
+
# This method is also aliased as <tt>to_s</tt>.
|
28
|
+
#
|
29
|
+
# ==== Examples:
|
30
|
+
# time = Time.now # => Thu Jan 18 06:10:17 CST 2007
|
31
|
+
#
|
32
|
+
# time.to_formatted_s(:time) # => "06:10:17"
|
33
|
+
# time.to_s(:time) # => "06:10:17"
|
34
|
+
#
|
35
|
+
# time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
|
36
|
+
# time.to_formatted_s(:short) # => "18 Jan 06:10"
|
37
|
+
# time.to_formatted_s(:long) # => "January 18, 2007 06:10"
|
38
|
+
# time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
|
39
|
+
# time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
|
40
|
+
def to_formatted_s(format = :default)
|
41
|
+
if formatter = DATE_FORMATS[format]
|
42
|
+
if formatter.respond_to?(:call)
|
43
|
+
formatter.call(self).to_s
|
44
|
+
else
|
45
|
+
strftime(formatter)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
to_default_s
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Convert a Time object to a Date, dropping hour, minute, and second precision.
|
53
|
+
#
|
54
|
+
# ==== Examples
|
55
|
+
# my_time = Time.now
|
56
|
+
# # => Mon Nov 12 22:59:51 -0500 2007
|
57
|
+
#
|
58
|
+
# my_time.to_date
|
59
|
+
# #=> Mon, 12 Nov 2007
|
60
|
+
#
|
61
|
+
# your_time = Time.parse("1/13/2009 1:13:03 P.M.")
|
62
|
+
# # => Tue Jan 13 13:13:03 -0500 2009
|
63
|
+
#
|
64
|
+
# your_time.to_date
|
65
|
+
# # => Tue, 13 Jan 2009
|
66
|
+
def to_date
|
67
|
+
::Date.new(year, month, day)
|
68
|
+
end
|
69
|
+
|
70
|
+
# A method to keep Time, Date and DateTime instances interchangeable on conversions.
|
71
|
+
# In this case, it simply returns +self+.
|
72
|
+
def to_time
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
# Converts a Time instance to a Ruby DateTime instance, preserving UTC offset.
|
77
|
+
#
|
78
|
+
# ==== Examples
|
79
|
+
# my_time = Time.now
|
80
|
+
# # => Mon Nov 12 23:04:21 -0500 2007
|
81
|
+
#
|
82
|
+
# my_time.to_datetime
|
83
|
+
# # => Mon, 12 Nov 2007 23:04:21 -0500
|
84
|
+
#
|
85
|
+
# your_time = Time.parse("1/13/2009 1:13:03 P.M.")
|
86
|
+
# # => Tue Jan 13 13:13:03 -0500 2009
|
87
|
+
#
|
88
|
+
# your_time.to_datetime
|
89
|
+
# # => Tue, 13 Jan 2009 13:13:03 -0500
|
90
|
+
def to_datetime
|
91
|
+
::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|