Module: DataMapper::Serialize

Included in:
Resource
Defined in:
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/common.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_csv.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_xml.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_json.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_yaml.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/xml_serializers.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/xml_serializers/rexml.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/xml_serializers/libxml.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/xml_serializers/nokogiri.rb

Defined Under Namespace

Modules: XMLSerializers

Instance Method Summary collapse

Instance Method Details

#properties_to_serialize(options) ⇒ Array

Returns propreties to serialize based on :only or :exclude arrays, if provided :only takes precendence over :exclude

Returns:

  • (Array)

    properties that need to be serialized



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gems/dm-serializer-0.9.9/lib/dm-serializer/common.rb', line 7

def properties_to_serialize(options)
  only_properties     = Array(options[:only])
  excluded_properties = Array(options[:exclude])

  self.class.properties(repository.name).reject do |p|
    if only_properties.include? p.name
      false
    else
      excluded_properties.include?(p.name) || !(only_properties.empty? || only_properties.include?(p.name))
    end
  end
end

#to_csv(writer = '') ⇒ String

Serialize a Resource to comma-separated values (CSV).

Returns:

  • (String)

    a CSV representation of the Resource



15
16
17
18
19
20
21
22
23
# File 'lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_csv.rb', line 15

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(*args) ⇒ String

Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627)

Returns:

  • (String)

    a JSON representation of the Resource



16
17
18
19
20
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
# File 'lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_json.rb', line 16

def to_json(*args)
  options = args.first || {}
  result = '{ '
  fields = []

  propset = properties_to_serialize(options)

  fields += propset.map do |property|
    "#{property.name.to_json}: #{send(property.getter).to_json}"
  end

  # add methods
  (options[: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
  # TODO: This needs tests and also needs to be ported to #to_xml and #to_yaml
  (options[: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

Returns:

  • (REXML::Document)

    an XML representation of this Resource



10
11
12
# File 'lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_xml.rb', line 10

def to_xml(opts = {})
  to_xml_document(opts).to_s
end

#to_yaml(opts_or_emitter = {}) ⇒ YAML

Serialize a Resource to YAML

Returns:

  • (YAML)

    a YAML representation of this Resource



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_yaml.rb', line 8

def to_yaml(opts_or_emitter = {})
  if opts_or_emitter.is_a?(YAML::Syck::Emitter)
    emitter = opts_or_emitter
    opts = {}
  else
    emitter = {}
    opts = opts_or_emitter
  end

  YAML::quick_emit(object_id,emitter) do |out|
    out.map(nil,to_yaml_style) do |map|
      propset = properties_to_serialize(opts)
      propset.each do |property|
        value = send(property.name.to_sym)
        map.add(property.name, value.is_a?(Class) ? value.to_s : value)
      end
      # add methods
      (opts[:methods] || []).each do |meth|
        if self.respond_to?(meth)
          map.add(meth.to_sym, send(meth))
        end
      end
      (instance_variable_get("@yaml_addes") || []).each do |k,v|
        map.add(k.to_s,v)
      end
    end
  end
end