Module: DataMapper::Serialize
Public Visibility
Public Instance Method Summary
| #to_csv(writer = '') |
Serialize a Resource to comma-separated values (CSV). Returns: |
|---|---|
| #to_json(options = {}) |
Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627). Returns: |
| #to_xml(opts = {}) |
Serialize a Resource to XML. Returns: |
| #to_yaml(opts = {}) |
Serialize a Resource to YAML. Returns: |
Public Instance Method Details
to_csv
Serialize a Resource to comma-separated values (CSV).
64 65 66 67 68 69 70 71 72
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 64 def to_csv(writer = '') FasterCSV.generate(writer) do |csv| row = [] self.class.properties(repository.name).each do |property| row << send(property.name).to_s end csv << row end end
to_json
Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 21 def to_json(options = {}) result = '{ ' fields = [] # FIXME: this should go into bunch of protected methods shared with other serialization methods only_properties = options[:only] || [] excluded_properties = options[:exclude] || [] exclude_read_only = options[:without_read_only_attributes] || false propset = self.class.properties(repository.name) # FIXME: this ugly condition is here because PropertySet does not support reject/select yet. unless only_properties.empty? propset.each do |property| fields << "#{property.name.to_json}: #{send(property.getter).to_json}" if only_properties.include?(property.name.to_sym) end else propset.each do |property| fields << "#{property.name.to_json}: #{send(property.getter).to_json}" unless excluded_properties.include?(property.name.to_sym) end end if self.class.respond_to?(:read_only_attributes) && exclude_read_only self.class.read_only_attributes.each do |property| fields << "#{property.to_json}: #{send(property).to_json}" end end # add methods (options[:methods] || []).each do |meth| if self.respond_to?(meth) fields << "#{meth.to_json}: #{send(meth).to_json}" end end result << fields.join(', ') result << ' }' result end
to_xml
Serialize a Resource to XML
77 78 79
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 77 def to_xml(opts = {}) to_xml_document(opts).to_s end
to_yaml
Serialize a Resource to YAML
84 85 86 87 88 89 90 91 92 93 94 95 96
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 84 def to_yaml(opts = {}) YAML::quick_emit(object_id,opts) do |out| out.map(nil,to_yaml_style) do |map| self.class.properties(repository.name).each do |property| value = send(property.name.to_sym) map.add(property.name, value.is_a?(Class) ? value.to_s : value) end (instance_variable_get("@yaml_addes") || []).each do |k,v| map.add(k.to_s,v) end end end end
Protected Visibility
Protected Instance Method Summary
| #to_xml_document(opts={}) |
Return a REXML::Document representing this Resource. Returns: |
|---|---|
| #xml_element_name |
Return the name of this Resource - to be used as the root element name. Returns: |
Protected Instance Method Details
to_xml_document
Return a REXML::Document representing this Resource
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 111 def to_xml_document(opts={}) doc = REXML::Document.new root = doc.add_element(xml_element_name) keys = self.class.key(repository.name) keys.each do |key| value = send(key.name) root.attributes[key.name.to_s] = value.to_s end #TODO old code base was converting single quote to double quote on attribs self.class.properties(repository.name).each do |property| if !keys.include?(property) value = send(property.name) node = root.add_element(property.name.to_s) node << REXML::Text.new(value.to_s) unless value.nil? end end doc end
xml_element_name
Return the name of this Resource - to be used as the root element name. This can be overloaded.
104 105 106 107 108
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 104 def xml_element_name Extlib::Inflection.underscore(self.class.name) end