Include a callback to register the YAML output
@param [DataMapper::Model] descendant
@return [undefined]
@api private
# File lib/dm-serializer/to_yaml.rb, line 14 14: def self.included(descendant) 15: YAML.add_domain_type(TAG_NAME, descendant.name) do |_tag, values| 16: values 17: end 18: end
Converts the resource into a hash of properties.
@param [Hash] options
Additional options.
@return [Hash{String => String}]
The hash of resources properties.
@since 1.0.1
# File lib/dm-serializer/to_json.rb, line 18 18: def as_json(options = {}) 19: options = {} if options.nil? 20: result = {} 21: 22: properties_to_serialize(options).each do |property| 23: property_name = property.name 24: value = __send__(property_name) 25: result[property_name] = value.kind_of?(DataMapper::Model) ? value.name : value 26: end 27: 28: # add methods 29: Array(options[:methods]).each do |method| 30: next unless respond_to?(method) 31: result[method] = __send__(method) 32: end 33: 34: # Note: if you want to include a whole other model via relation, use 35: # :methods: 36: # 37: # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}}) 38: # 39: # TODO: This needs tests and also needs to be ported to #to_xml and 40: # #to_yaml 41: if options[:relationships] 42: options[:relationships].each do |relationship_name, opts| 43: if respond_to?(relationship_name) 44: result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false)) 45: end 46: end 47: end 48: 49: result 50: end
A callback to encode the resource in the YAML stream
@param [#] coder
handles adding the values to the output
@param [Hash] options
optional Hash configuring the output
@return [undefined]
@api public
# File lib/dm-serializer/to_yaml.rb, line 49 49: def encode_with(coder, options = {}) 50: coder.tag = to_yaml_type if coder.respond_to?(:tag=) 51: coder.style = to_yaml_style if coder.respond_to?(:style=) 52: 53: methods = [] 54: 55: methods.concat properties_to_serialize(options).map { |property| property.name } 56: methods.concat Array(options[:methods]) 57: 58: methods.each do |method| 59: coder.add(method.to_s, __send__(method)) 60: end 61: end
Returns propreties to serialize based on :only or :exclude arrays, if provided :only takes precendence over :exclude
@return [Array]
Properties that need to be serialized.
# File lib/dm-serializer/common.rb, line 11 11: def properties_to_serialize(options) 12: only_properties = Array(options[:only]) 13: excluded_properties = Array(options[:exclude]) 14: 15: model.properties(repository.name).reject do |p| 16: if only_properties.include? p.name 17: false 18: else 19: excluded_properties.include?(p.name) || 20: !(only_properties.empty? || 21: only_properties.include?(p.name)) 22: end 23: end 24: end
Serialize a Resource to comma-separated values (CSV).
@return
# File lib/dm-serializer/to_csv.rb, line 19 19: def to_csv(*args) 20: options = args.first || {} 21: options = options.to_h if options.respond_to?(:to_h) 22: options[:writer] = '' unless options.has_key? :writer 23: 24: CSV.generate(options[:writer]) do |csv| 25: row = properties_to_serialize(options).map do |property| 26: __send__(property.name).to_s 27: end 28: csv << row 29: end 30: end
Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627)
@return
# File lib/dm-serializer/to_json.rb, line 55 55: def to_json(*args) 56: options = args.first 57: options = {} unless options.kind_of?(Hash) 58: 59: result = as_json(options) 60: 61: # default to making JSON 62: if options.fetch(:to_json, true) 63: MultiJson.encode(result) 64: else 65: result 66: end 67: end
Serialize a Resource to XML.
@return [LibXML::Document, Nokogiri::Document, REXML::Document]
An XML representation of this Resource.
# File lib/dm-serializer/to_xml.rb, line 11 11: def to_xml(opts = {}) 12: xml = XML.serializer 13: xml.output(to_xml_document(opts)).to_s 14: end
This method requires certain methods to be implemented in the individual serializer library subclasses:
new_document
root_node
add_property_node
add_node
# File lib/dm-serializer/to_xml.rb, line 23 23: def to_xml_document(opts={}, doc = nil) 24: xml = XML.serializer 25: doc ||= xml.new_document 26: 27: default_xml_element_name = lambda { 28: DataMapper::Inflector.underscore(model.name).tr("/", "-") 29: } 30: 31: root = xml.root_node( 32: doc, 33: (opts[:element_name] || default_xml_element_name[]) 34: ) 35: 36: properties_to_serialize(opts).each do |property| 37: value = __send__(property.name) 38: attrs = {} 39: 40: unless property.primitive == String 41: attrs['type'] = property.primitive.to_s.downcase 42: end 43: 44: xml.add_node(root, property.name.to_s, value, attrs) 45: end 46: 47: Array(opts[:methods]).each do |meth| 48: if self.respond_to?(meth) 49: xml_name = meth.to_s.gsub(/[^a-z0-9_]/, '') 50: value = __send__(meth) 51: 52: unless value.nil? 53: if value.respond_to?(:to_xml_document) 54: xml.add_xml(root, value.to_xml_document) 55: else 56: xml.add_node(root, xml_name, value.to_s) 57: end 58: end 59: end 60: end 61: 62: doc 63: end
Serialize a Resource to YAML
@example
yaml = resource.to_yaml # => a valid YAML string
@param [Hash] options
@return [String]
@api public
# File lib/dm-serializer/to_yaml.rb, line 30 30: def to_yaml(options = {}) 31: YAML.quick_emit(object_id, options) do |out| 32: out.map(to_yaml_type, to_yaml_style) do |map| 33: encode_with(map, options.kind_of?(Hash) ? options : {}) 34: end 35: end 36: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.