Module: FleetVentures::Serialization

Defined in:
lib/rdf_for_sqlite.rb

Overview

Rather than going through a Rails commit process, I’ve just hijacked this code and run it as my own module. For Spira models, the attributes hash uses symbols rather than strings, and so I have to map them to strings on line 15 below.

Instance Method Summary collapse

Instance Method Details

#serializable_hash(options = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rdf_for_sqlite.rb', line 19

def serializable_hash(options = nil)
  options ||= {}

  options[:only]   = Array.wrap(options[:only]).map { |n| n.to_s }
  options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }

  attribute_names = attributes.keys.map {|k| k.to_s}.sort
  if options[:only].any?
    attribute_names &= options[:only]
  elsif options[:except].any?
    attribute_names -= options[:except]
  end

  method_names = Array.wrap(options[:methods]).inject([]) do |methods, name|
    methods << name if respond_to?(name.to_s)
    methods
  end

  (attribute_names + method_names).inject({}) { |hash, name|
    hash[name] = send(name)
    hash
  }
end