Module: DataMapper::Support::Serialization
- Defined in:
- lib/data_mapper/support/serialization.rb
Instance Method Summary collapse
- #get_value_for_column(column) ⇒ Object
- #to_json(*a) ⇒ Object
- #to_xml ⇒ Object
- #to_xml_document ⇒ Object
- #to_yaml(opts = {}) ⇒ Object
Instance Method Details
#get_value_for_column(column) ⇒ Object
72 73 74 |
# File 'lib/data_mapper/support/serialization.rb', line 72 def get_value_for_column(column) send(column.type == :boolean ? column.name.to_s.ensure_ends_with('?') : column.name) end |
#to_json(*a) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/data_mapper/support/serialization.rb', line 59 def to_json(*a) table = database_context.table(self.class) result = '{ ' result << table.columns.map do |column| "#{column.name.to_json}: #{get_value_for_column(column).to_json(*a)}" end.join(', ') result << ' }' result end |
#to_xml ⇒ Object
30 31 32 |
# File 'lib/data_mapper/support/serialization.rb', line 30 def to_xml to_xml_document.to_s end |
#to_xml_document ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/data_mapper/support/serialization.rb', line 34 def to_xml_document doc = REXML::Document.new table = database_context.table(self.class) root = doc.add_element(Inflector.underscore(self.class.name)) key_attribute = root.attributes << REXML::Attribute.new(table.key.to_s, key) # Single-quoted attributes are ugly. :p # NOTE: I don't want to break existing REXML specs for everyone, so I'm # overwriting REXML::Attribute#to_string just for this instance. def key_attribute.to_string %Q[#@expanded_name="#{to_s().gsub(/"/, '"')}"] end table.columns.each do |column| next if column.key? value = get_value_for_column(column) node = root.add_element(column.to_s) node << REXML::Text.new(value.to_s) unless value.nil? end doc end |
#to_yaml(opts = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/data_mapper/support/serialization.rb', line 13 def to_yaml(opts = {}) YAML::quick_emit( object_id, opts ) do |out| out.map(nil, to_yaml_style ) do |map| database_context.table(self).columns.each do |column| lazy_load!(column.name) if column.lazy? value = get_value_for_column(column) map.add(column.to_s, value.is_a?(Class) ? value.to_s : value) end (self.instance_variable_get("@yaml_added") || []).each do |k,v| map.add(k.to_s, v) end end end end |