Sha256: 0954523fb8db1bd4e2bb717e95e0237ef2269650e1a1dff6db5441df7d0a4f4e
Contents?: true
Size: 1.79 KB
Versions: 1
Compression:
Stored size: 1.79 KB
Contents
require 'bundler/setup' require 'mongo_mapper' module MongoMapper module Plugins module ToViewModel extend ActiveSupport::Concern module ClassMethods def to_view_model fields = keys.inject({}) do |memo, (k, v)| memo[k.to_s] = if v.type == ObjectId nil else begin unless v.default_value.nil? v.default_value else v.type.new end rescue nil end end begin memo[k.to_s] = memo[k.to_s].class.from_mongo(memo[k.to_s]) rescue # keep the unconverted version end memo end relations = associations.inject({}) do |memo, (k, v)| memo[k.to_s] = [v.klass.to_view_model] if ['OneAssociation', 'ManyAssociation'].include?(v.class.to_s.split(/::/).last) memo end fields.merge(relations) end end module InstanceMethods def to_view_model fields = keys.inject({}) do |memo, (k, v)| memo[k.to_s] = send(k) begin memo[k.to_s] = memo[k.to_s].class.from_mongo(memo[k.to_s]) rescue # keep the unconverted version end memo end relations = associations.inject({}) do |memo, (k, v)| begin memo[k.to_s] = send(k).collect(&:to_view_model) rescue # Don't add to hash end memo end fields.merge(relations) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mm_to_view_model-0.1.2 | lib/mm_to_view_model.rb |