Module: MongoMapper::Serialize::InstanceMethods

Defined in:
lib/question_chain/mongo_serialization.rb

Instance Method Summary collapse

Instance Method Details

#api_nameObject



53
54
55
# File 'lib/question_chain/mongo_serialization.rb', line 53

def api_name
  self.class.name.demodulize.underscore
end

#attributes_for_api_resourceObject

this is how we serialize man



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/question_chain/mongo_serialization.rb', line 15

def attributes_for_api_resource
  result = {self.api_name => {}}
  self.class.attributes_for_api.each do |key|
    attribute = self.send(key)
    if attribute.is_a?(Array) || attribute.is_a?(Set)
      attribute.map! do |attr|
        attr.respond_to?(:attributes_for_api_resources) ? attr.attributes_for_api_resources : attr
      end
      attribute = attribute.to_a
    end
    if attribute.nil? || attribute.is_a?(Array) || attribute.is_a?(Set)
      result[self.api_name][key] = attribute
    else
      result[self.api_name][key] = (attribute.is_a?(BSON::ObjectId)) ? attribute.to_s : attribute
    end
  end
  result
end

#attributes_for_api_resourcesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/question_chain/mongo_serialization.rb', line 34

def attributes_for_api_resources
  result = {}
  self.class.attributes_for_api.each do |key|
    attribute = self.send(key)
    if attribute.is_a?(Array) || attribute.is_a?(Set)
      attribute.map! do |attr|
        attr.respond_to?(:attributes_for_api_resources) ? attr.attributes_for_api_resources : attr
      end
      attribute = attribute.to_a
    end
    if attribute.nil?
      result[key] = attribute
    else
      result[key] = (attribute.is_a?(BSON::ObjectId)) ? attribute.to_s : attribute
    end
  end
  result
end

#to_jsonObject



57
58
59
# File 'lib/question_chain/mongo_serialization.rb', line 57

def to_json
  attributes_for_api_resource.to_json
end