Sha256: ee00a903083aef4917e912e8e6792a3f321bf143942a93432952404130f7bad9

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require "active_support/inflector"
require "active_support/core_ext/hash"

module Collector
  module Repository

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods

      def collection
         Collector.connection[collection_name]
      end

      def collection_name
        ActiveSupport::Inflector.tableize(model)
      end

      def model
        name.to_s.gsub("Repository", "").constantize
      end

      def save(model)
        model.touch
        save_without_updating_timestamps(model)
      end

      def save_without_updating_timestamps(model)
        attributes = serialize!(model)
        collection.insert(attributes)
      end

      def serialize!(model)
        attributes = serialize(model)
        attributes["_id"] = attributes.delete("id")
        attributes.reject! { |key, val| val.nil? }
        attributes
      end

      def serialize(model)
        model.attributes.with_indifferent_access
      end

      def deserialize!(attributes)
        attributes       = attributes.with_indifferent_access
        attributes["id"] = attributes.delete("_id")
        deserialize(attributes)
      end

      def deserialize(attributes)
        model.new(attributes)
      end

      def all
        collection.find.map do |document|
          deserialize!(document)
        end
      end

      def find_by_id(id)
        collection.find(_id: id).map do |document|
          deserialize!(document)
        end
      end

      def method_missing(method_sym, *arguments, &block)
        if method_sym.to_s =~ /^find_by_(.*)$/
          collection.find($1.to_sym => arguments.first).map do |document|
            deserialize!(document)
          end
        else
          super
        end
      end

      def respond_to?(method_sym, include_private = false)
        if method_sym.to_s =~ /^find_by_(.*)$/
          true
        else
          super
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
collector-0.0.13 lib/collector/repository.rb