| 1 |
require 'builder' |
|---|
| 2 |
|
|---|
| 3 |
module ActiveSupport |
|---|
| 4 |
module CoreExtensions |
|---|
| 5 |
module Array |
|---|
| 6 |
module Conversions |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
def to_sentence(options = {}) |
|---|
| 11 |
options.assert_valid_keys(:connector, :skip_last_comma) |
|---|
| 12 |
options.reverse_merge! :connector => 'and', :skip_last_comma => false |
|---|
| 13 |
options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == '' |
|---|
| 14 |
|
|---|
| 15 |
case length |
|---|
| 16 |
when 0 |
|---|
| 17 |
"" |
|---|
| 18 |
when 1 |
|---|
| 19 |
self[0].to_s |
|---|
| 20 |
when 2 |
|---|
| 21 |
"#{self[0]} #{options[:connector]}#{self[1]}" |
|---|
| 22 |
else |
|---|
| 23 |
"#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}" |
|---|
| 24 |
end |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
def to_param |
|---|
| 30 |
map(&:to_param).join '/' |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
def to_query(key) |
|---|
| 38 |
collect { |value| value.to_query("#{key}[]") } * '&' |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
def self.included(base) |
|---|
| 42 |
base.class_eval do |
|---|
| 43 |
alias_method :to_default_s, :to_s |
|---|
| 44 |
alias_method :to_s, :to_formatted_s |
|---|
| 45 |
end |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
def to_formatted_s(format = :default) |
|---|
| 58 |
case format |
|---|
| 59 |
when :db |
|---|
| 60 |
if respond_to?(:empty?) && self.empty? |
|---|
| 61 |
"null" |
|---|
| 62 |
else |
|---|
| 63 |
collect { |element| element.id }.join(",") |
|---|
| 64 |
end |
|---|
| 65 |
else |
|---|
| 66 |
to_default_s |
|---|
| 67 |
end |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
def to_xml(options = {}) |
|---|
| 111 |
raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } |
|---|
| 112 |
|
|---|
| 113 |
options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records" |
|---|
| 114 |
options[:children] ||= options[:root].singularize |
|---|
| 115 |
options[:indent] ||= 2 |
|---|
| 116 |
options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) |
|---|
| 117 |
|
|---|
| 118 |
root = options.delete(:root).to_s |
|---|
| 119 |
children = options.delete(:children) |
|---|
| 120 |
|
|---|
| 121 |
if !options.has_key?(:dasherize) || options[:dasherize] |
|---|
| 122 |
root = root.dasherize |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
options[:builder].instruct! unless options.delete(:skip_instruct) |
|---|
| 126 |
|
|---|
| 127 |
opts = options.merge({ :root => children }) |
|---|
| 128 |
|
|---|
| 129 |
xml = options[:builder] |
|---|
| 130 |
if empty? |
|---|
| 131 |
xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) |
|---|
| 132 |
else |
|---|
| 133 |
xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { |
|---|
| 134 |
yield xml if block_given? |
|---|
| 135 |
each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) } |
|---|
| 136 |
} |
|---|
| 137 |
end |
|---|
| 138 |
end |
|---|
| 139 |
|
|---|
| 140 |
end |
|---|
| 141 |
end |
|---|
| 142 |
end |
|---|
| 143 |
end |
|---|