Module: DataMapper::Is::Indexed::ClassMethods

Defined in:
lib/dm-is-indexed/is/indexed.rb

Instance Method Summary collapse

Instance Method Details

#[](*keys) ⇒ DataMapper::Collection, ...

Allows querying the indexed properties of a Model.

Parameters:

  • keys (Array, Range, Integer, Object)

    The key to use when querying the Model. If the key is an Integer or a Range, it will select multiple Resources of the Model.

Returns:

  • (DataMapper::Collection, DataMapper::Resource, nil)

    The selected Resource or Resources.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dm-is-indexed/is/indexed.rb', line 22

def [](*keys)
  if keys.length > 1
    super(*keys)
  else
    key = keys[0]
    
    case key
    when Integer, Range
      super(*keys)
    else
      resource = nil

      properties.each do |field|
        if (field.index || field.unique?)
          if field.valid?(key)
            resource = first(field => key)

            break if resource
          end
        end
      end

      return resource
    end
  end
end