Sha256: 0799d2cba1bc0465371388bffaaad5a39015dc7446b44400ffee43a4aad40420

Contents?: true

Size: 1002 Bytes

Versions: 32

Compression:

Stored size: 1002 Bytes

Contents

module MongoModel
  module Serialization
    extend ActiveSupport::Concern
    
    include ActiveModel::Serializers::JSON
    
    def serializable_hash(given_options = nil)
      options = given_options ? given_options.dup : {}

      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

32 entries across 32 versions & 1 rubygems

Version Path
mongomodel-0.4.9 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.8 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.7 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.6 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.5 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.4 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.3 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.2 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.1 lib/mongomodel/concerns/serialization.rb
mongomodel-0.4.0 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.6 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.5 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.4 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.3 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.2 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.1 lib/mongomodel/concerns/serialization.rb
mongomodel-0.3.0 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.20 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.19 lib/mongomodel/concerns/serialization.rb
mongomodel-0.2.18 lib/mongomodel/concerns/serialization.rb