Class: MongoMapper::Serialization::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/serialization.rb

Overview

:nodoc:

Direct Known Subclasses

JsonSerializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, options = {}) ⇒ Serializer

Returns a new instance of Serializer.



8
9
10
# File 'lib/mongo_mapper/serialization.rb', line 8

def initialize(record, options={})
  @record, @options = record, options.dup
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/mongo_mapper/serialization.rb', line 6

def options
  @options
end

Instance Method Details

#serializable_key_namesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongo_mapper/serialization.rb', line 12

def serializable_key_names
  key_names = @record.attributes.keys

  if options[:only]
    options.delete(:except)
    key_names = key_names & Array(options[:only]).collect { |n| n.to_s }
  else
    options[:except] = Array(options[:except])
    key_names = key_names - options[:except].collect { |n| n.to_s }
  end

  key_names
end

#serializable_method_namesObject



26
27
28
29
30
31
# File 'lib/mongo_mapper/serialization.rb', line 26

def serializable_method_names
  Array(options[:methods]).inject([]) do |method_attributes, name|
    method_attributes << name if @record.respond_to?(name.to_s)
    method_attributes
  end
end

#serializable_namesObject



33
34
35
# File 'lib/mongo_mapper/serialization.rb', line 33

def serializable_names
  serializable_key_names + serializable_method_names
end

#serializable_recordObject



37
38
39
40
41
# File 'lib/mongo_mapper/serialization.rb', line 37

def serializable_record
  returning(serializable_record = {}) do
    serializable_names.each { |name| serializable_record[name] = @record.send(name) }
  end
end

#serializeObject



43
44
45
# File 'lib/mongo_mapper/serialization.rb', line 43

def serialize
  # overwrite to implement
end

#to_s(&block) ⇒ Object



47
48
49
# File 'lib/mongo_mapper/serialization.rb', line 47

def to_s(&block)
  serialize(&block)
end