Sha256: 579232fab27621c57f646f3c9888029bfe7d122ee8e172a8acda12da8245a84e

Contents?: true

Size: 962 Bytes

Versions: 9

Compression:

Stored size: 962 Bytes

Contents

module MongoModel
  module Serialization
    extend ActiveSupport::Concern
    
    include ActiveModel::Serializers::JSON
    
    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
      attribute_names -= self.class.internal_properties.map { |p| p.name.to_s }
      
      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) if respond_to?(name)
        hash
      }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongomodel-0.2.5 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.4 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.3 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.2 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.1 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.0 lib/mongomodel/concerns/serialization.rb
mongomodel-0.1.6 lib/mongomodel/concerns/serialization.rb
mongomodel-0.1.5 lib/mongomodel/concerns/serialization.rb
mongomodel-0.1.4 lib/mongomodel/concerns/serialization.rb