Module: Lunr::Model::Klass

Defined in:
lib/lunr/model/klass.rb

Instance Method Summary collapse

Instance Method Details

#create(hit) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lunr/model/klass.rb', line 11

def create hit
  hash = { :id => hit.primary_key }

  properties.each do |name, type|
    value = hit.stored name

    if Array === value && value.length == 1 && type == :text
      # For text fields, which always appear to be multiple.
      value = value.first
    end

    hash[name] = value
  end

  new hash
end

#first(&block) ⇒ Object



28
29
30
# File 'lib/lunr/model/klass.rb', line 28

def first &block
  search(&block).first
end

#propertiesObject



32
33
34
# File 'lib/lunr/model/klass.rb', line 32

def properties
  @properties ||= {}
end

#scope(name = :all, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lunr/model/klass.rb', line 40

def scope name = :all, &block
  scopes[name] = block

  unless name == :all
    class_eval <<-END, __FILE__, __LINE__ + 1
      def self.#{name}
        search.#{name}
      end
    END
  end
end

#scopesObject



36
37
38
# File 'lib/lunr/model/klass.rb', line 36

def scopes
  @scopes ||= {}
end

#search(&block) ⇒ Object Also known as: all



52
53
54
# File 'lib/lunr/model/klass.rb', line 52

def search &block
  Lunr::Search.new self, &block
end

#searches(classname = nil, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lunr/model/klass.rb', line 58

def searches classname = nil, &block
  Sunspot::TypeField.alias self, classname if classname
  Sunspot.setup self, &block

  properties.each do |name, type|
    class_eval <<-END, __FILE__, __LINE__ + 1
      def #{name}
        @hash[#{name.inspect}]
      end
    END

    alias_method "#{name}?", name if type == :boolean
  end
end