Class: DynamicFinderMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/ooor/finders/dynamic_finder_match.rb

Overview

Active Record Dynamic Finder Match

Taken from active record source code

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(finder, instantiator, bang, attribute_names) ⇒ DynamicFinderMatch

Returns a new instance of DynamicFinderMatch.



29
30
31
32
33
34
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 29

def initialize(finder, instantiator, bang, attribute_names)
  @finder          = finder
  @instantiator    = instantiator
  @bang            = bang
  @attribute_names = attribute_names
end

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



36
37
38
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 36

def attribute_names
  @attribute_names
end

#finderObject (readonly)

Returns the value of attribute finder.



36
37
38
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 36

def finder
  @finder
end

#instantiatorObject (readonly)

Returns the value of attribute instantiator.



36
37
38
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 36

def instantiator
  @instantiator
end

Class Method Details

.match(method) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 6

def self.match(method)
  finder       = :first
  bang         = false
  instantiator = nil

  case method.to_s
  when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
    finder = :last if $1 == 'last_'
    finder = :all if $1 == 'all_'
    names = $2
  when /^find_by_([_a-zA-Z]\w*)\!$/
    bang = true
    names = $1
  when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
    instantiator = $1 == 'initialize' ? :new : :create
    names = $2
  else
    return nil
  end

  new(finder, instantiator, bang, names.split('_and_'))
end

Instance Method Details

#bang?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 50

def bang?
  @bang
end

#creator?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 46

def creator?
  @finder == :first && @instantiator == :create
end

#finder?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 38

def finder?
  @finder && !@instantiator
end

#instantiator?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ooor/finders/dynamic_finder_match.rb', line 42

def instantiator?
  @finder == :first && @instantiator
end