Module DataMapper::Support::Serialization
In: lib/data_mapper/support/serialization.rb
lib/data_mapper/support/serialization.rb

Methods

Public Instance methods

[Source]

    # File lib/data_mapper/support/serialization.rb, line 90
90:       def get_value_for_column(column)
91:         send(column.type == :boolean ? column.name.to_s.ensure_ends_with('?') : column.name)
92:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 90
90:       def get_value_for_column(column)
91:         send(column.type == :boolean ? column.name.to_s.ensure_ends_with('?') : column.name)
92:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 83
83:       def to_csv(writer = "")
84:         FasterCSV.generate(writer) do |csv|
85:           csv << database_context.table(self.class).columns.map { |column| get_value_for_column(column) }
86:         end
87:         return writer
88:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 83
83:       def to_csv(writer = "")
84:         FasterCSV.generate(writer) do |csv|
85:           csv << database_context.table(self.class).columns.map { |column| get_value_for_column(column) }
86:         end
87:         return writer
88:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 70
70:       def  to_jsonto_json(*a)
71:         table = database_context.table(self.class)
72:         
73:         result = '{ '
74:         
75:         result << table.columns.map do |column|
76:           "#{column.name.to_json}: #{get_value_for_column(column).to_json(*a)}"
77:         end.join(', ')
78:         
79:         result << ' }'
80:         result
81:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 70
70:       def  to_jsonto_json(*a)
71:         table = database_context.table(self.class)
72:         
73:         result = '{ '
74:         
75:         result << table.columns.map do |column|
76:           "#{column.name.to_json}: #{get_value_for_column(column).to_json(*a)}"
77:         end.join(', ')
78:         
79:         result << ' }'
80:         result
81:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 36
36:       def to_xml
37:         to_xml_document.to_s
38:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 36
36:       def to_xml
37:         to_xml_document.to_s
38:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 44
44:       def to_xml_document
45:         doc = REXML::Document.new
46:         
47:         table = database_context.table(self.class)
48:         # root = doc.add_element(Inflector.underscore(self.class.name))
49:         root = doc.add_element(xml_element_name)
50:         
51:         key_attribute = root.attributes << REXML::Attribute.new(table.key.to_s, key)
52:         
53:         # Single-quoted attributes are ugly. :p
54:         # NOTE: I don't want to break existing REXML specs for everyone, so I'm
55:         # overwriting REXML::Attribute#to_string just for this instance.
56:         def key_attribute.to_string
57:           %Q[#@expanded_name="#{to_s().gsub(/"/, '&quot;')}"] 
58:         end
59:         
60:         table.columns.each do |column|
61:           next if column.key?
62:           value = get_value_for_column(column)
63:           node = root.add_element(column.to_s)
64:           node << REXML::Text.new(value.to_s) unless value.nil?
65:         end
66:         
67:         doc
68:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 44
44:       def to_xml_document
45:         doc = REXML::Document.new
46:         
47:         table = database_context.table(self.class)
48:         # root = doc.add_element(Inflector.underscore(self.class.name))
49:         root = doc.add_element(xml_element_name)
50:         
51:         key_attribute = root.attributes << REXML::Attribute.new(table.key.to_s, key)
52:         
53:         # Single-quoted attributes are ugly. :p
54:         # NOTE: I don't want to break existing REXML specs for everyone, so I'm
55:         # overwriting REXML::Attribute#to_string just for this instance.
56:         def key_attribute.to_string
57:           %Q[#@expanded_name="#{to_s().gsub(/"/, '&quot;')}"] 
58:         end
59:         
60:         table.columns.each do |column|
61:           next if column.key?
62:           value = get_value_for_column(column)
63:           node = root.add_element(column.to_s)
64:           node << REXML::Text.new(value.to_s) unless value.nil?
65:         end
66:         
67:         doc
68:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 19
19:       def to_yaml(opts = {})
20:         
21:         YAML::quick_emit( object_id, opts ) do |out|
22:           out.map(nil, to_yaml_style ) do |map|
23:             database_context.table(self).columns.each do |column|
24:               lazy_load!(column.name) if column.lazy?
25:               value = get_value_for_column(column)
26:               map.add(column.to_s, value.is_a?(Class) ? value.to_s : value)
27:             end
28:             (self.instance_variable_get("@yaml_added") || []).each do |k,v|
29:               map.add(k.to_s, v)
30:             end
31:           end
32:         end
33:         
34:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 19
19:       def to_yaml(opts = {})
20:         
21:         YAML::quick_emit( object_id, opts ) do |out|
22:           out.map(nil, to_yaml_style ) do |map|
23:             database_context.table(self).columns.each do |column|
24:               lazy_load!(column.name) if column.lazy?
25:               value = get_value_for_column(column)
26:               map.add(column.to_s, value.is_a?(Class) ? value.to_s : value)
27:             end
28:             (self.instance_variable_get("@yaml_added") || []).each do |k,v|
29:               map.add(k.to_s, v)
30:             end
31:           end
32:         end
33:         
34:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 40
40:       def xml_element_name ## overloadable
41:         Inflector.underscore(self.class.name)
42:       end

[Source]

    # File lib/data_mapper/support/serialization.rb, line 40
40:       def xml_element_name ## overloadable
41:         Inflector.underscore(self.class.name)
42:       end

[Validate]