Module: DataMapper::Serialize
- Included in:
- Resource
- Defined in:
- lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb
Instance Method Summary collapse
-
#to_csv(writer = '') ⇒ String
Serialize a Resource to comma-separated values (CSV).
-
#to_json(options = {}) ⇒ String
Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627).
-
#to_xml(opts = {}) ⇒ REXML::Document
Serialize a Resource to XML.
-
#to_yaml(opts = {}) ⇒ YAML
Serialize a Resource to YAML.
Instance Method Details
#to_csv(writer = '') ⇒ String
Serialize a Resource to comma-separated values (CSV).
75 76 77 78 79 80 81 82 83 |
# File 'lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb', line 75 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(options = {}) ⇒ String
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 69 70 |
# File 'lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb', line 21 def to_json( = {}) result = '{ ' fields = [] # FIXME: this should go into bunch of protected methods shared with other serialization methods only_properties = Array([:only]) excluded_properties = Array([:exclude]) exclude_read_only = [:without_read_only_attributes] || false propset = self.class.properties(repository.name).reject do |p| next if only_properties.include? p.name excluded_properties.include?(p.name) || !(only_properties.empty? || only_properties.include?(p.name)) end fields += propset.map do |property| "#{property.name.to_json}: #{send(property.getter).to_json}" end if self.respond_to?(:serialize_properties) self.serialize_properties.each do |k,v| fields << "#{k.to_json}: #{v.to_json}" 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 ([:methods] || []).each do |meth| if self.respond_to?(meth) fields << "#{meth.to_json}: #{send(meth).to_json}" end end # Note: if you want to include a whole other model via relation, use :methods # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}}) # add relationships ([:relationships] || {}).each do |rel,opts| if self.respond_to?(rel) fields << "#{rel.to_json}: #{send(rel).to_json(opts)}" end end result << fields.join(', ') result << ' }' result end |
#to_xml(opts = {}) ⇒ REXML::Document
Serialize a Resource to XML
88 89 90 91 |
# File 'lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb', line 88 def to_xml(opts = {}) to_xml_document(opts).to_s end |
#to_yaml(opts = {}) ⇒ YAML
Serialize a Resource to YAML
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb', line 96 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 |