lib/ruboss4ruby/active_foo.rb in dima-ruboss4ruby-1.0.5 vs lib/ruboss4ruby/active_foo.rb in dima-ruboss4ruby-1.1.0
- old
+ new
@@ -10,13 +10,10 @@
module ActiveSupport
module CoreExtensions
module Hash
module Conversions
def to_fxml(options = {})
- if self.empty? && !options[:root]
- raise "empty hash being converted to FXML must specify :root option, e.g. <class_name>.to_s.underscore.pluralize"
- end
options.merge!(:dasherize => false)
options[:indent] ||= 2
options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
:root => "hash" })
options[:builder].instruct! unless options.delete(:skip_instruct)
@@ -69,13 +66,10 @@
end
end
module Array
module Conversions
def to_fxml(options = {})
- if self.empty? && !options[:root]
- raise "empty array being converted to FXML must specify :root option, e.g. <class_name>.to_s.underscore.pluralize"
- end
raise "Not all elements respond to to_fxml" unless all? { |e| e.respond_to? :to_fxml }
options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
options[:children] ||= options[:root].singularize
options[:indent] ||= 2
@@ -108,67 +102,9 @@
end
end
module ActiveRecord
# Flex friendly XML serialization patches
- class Base
- class << self
- # TODO: this doesn't work with hash based to_fxml(:include) options, only array based
- def default_fxml_methods(*args)
- methods = *args.dup
- module_eval <<-END
- def self.default_fxml_methods_array
- return [#{methods.inspect}].flatten
- end
- END
- end
-
- def default_fxml_includes(*args)
- includes = *args.dup
- module_eval <<-END
- def self.default_fxml_include_params
- return [#{includes.inspect}].flatten
- end
- END
- end
-
- def default_fxml_hash(already_included = [])
- # return {} unless self.class.respond_to?(:default_fxml_include_params) || self.class.respond_to?(:default_fxml_methods_array)
- default_hash = {:include => {}}
- default_hash[:methods] = self.default_fxml_methods_array if self.respond_to?(:default_fxml_methods_array)
- if self.respond_to?(:default_fxml_include_params)
- default_includes = self.default_fxml_include_params
- default_hash[:include] = default_includes.inject({}) do |include_hash, included|
- next if already_included.include?(included) # We only want to include things once, to avoid infinite loops
- included_class = included.to_s.singularize.camelize.constantize
- include_hash[included] = included_class.default_fxml_hash(already_included + default_includes)
- include_hash
- end
- end
- default_hash
- end
-
- # options[:include] can be a Hash, Array, Symbol or nil.
- # We always want it as a Hash. This translates includes to a Hash like this:
- # If it's a nil, return an empty Hash ({})
- # If it's a Hash, then it is just returned
- # If it's an array, then it returns a Hash with each array element as a key, and values of empty Hashes.
- # If it's a symbol, then it returns a Hash with a single key/value pair, with the symbol as the key and an empty Hash as the value.
- def includes_as_hash(includes = nil)
- res = case
- when includes.is_a?(Hash)
- includes
- when includes.nil?
- {}
- else #Deal with arrays and symbols
- res = [includes].flatten.inject({}) {|include_hash, included| include_hash[included] = {} ; include_hash}
- end
- res
- end
-
- end
- end
-
module Serialization
def to_fxml(options = {}, &block)
options.merge!(:dasherize => false)
default_except = [:crypted_password, :salt, :remember_token, :remember_token_expires_at]
options[:except] = (options[:except] ? options[:except] + default_except : default_except)
\ No newline at end of file