Sha256: 96fdef5df517c8ed832a33b9b1ea7185d79b011ce6529e754cb3b17341512a1c

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

module Elasticsearch
  module Persistence
    module Model
      # This module contains the base interface for models
      #
      module Base
        module InstanceMethods

          # Model initializer sets the `@id` variable if passed
          #
          def initialize(attributes={})
            @_id = attributes[:id] || attributes['id']
            super
          end

          # Return model attributes as a Hash, merging in the `id`
          #
          def attributes
            super.merge id: id
          end

          # Return the document `_id`
          #
          def id
            @_id
          end; alias :_id :id

          # Set the document `_id`
          #
          def id=(value)
            @_id = value
          end; alias :_id= :id=

          # Return the document `_index`
          #
          def _index
            @_index
          end

          # Return the document `_type`
          #
          def _type
            @_type
          end

          # Return the document `_version`
          #
          def _version
            @_version
          end

          # Return the raw document `_source`
          #
          def _source
            @_source
          end

          def to_s
            "#<#{self.class} #{attributes.to_hash.inspect.gsub(/:(\w+)=>/, '\1: ')}>"
          end; alias :inspect :to_s
        end
      end

      # Utility methods for {Elasticsearch::Persistence::Model}
      #
      module Utils

        # Return Elasticsearch type based on passed Ruby class (used in the `attribute` method)
        #
        def lookup_type(type)
          case
            when type == String
              'string'
            when type == Integer
              'integer'
            when type == Float
              'float'
            when type == Date || type == Time || type == DateTime
              'date'
            when type == Virtus::Attribute::Boolean
              'boolean'
          end
        end; module_function :lookup_type
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
elasticsearch-persistence-2.0.1 lib/elasticsearch/persistence/model/base.rb
elasticsearch-persistence-2.0.0 lib/elasticsearch/persistence/model/base.rb
elasticsearch-persistence-0.1.9 lib/elasticsearch/persistence/model/base.rb