Class: FreeAgent::Base::DynamicFinderMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/free_agent/base.rb

Overview

Active Record Dynamic Finder Match

Refer to ActiveRecord::Base documentation for Dynamic attribute-based finders for detailed info.

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.



54
55
56
57
58
59
# File 'lib/free_agent/base.rb', line 54

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.



61
62
63
# File 'lib/free_agent/base.rb', line 61

def attribute_names
  @attribute_names
end

#finderObject (readonly)

Returns the value of attribute finder.



61
62
63
# File 'lib/free_agent/base.rb', line 61

def finder
  @finder
end

#instantiatorObject (readonly)

Returns the value of attribute instantiator.



61
62
63
# File 'lib/free_agent/base.rb', line 61

def instantiator
  @instantiator
end

Class Method Details

.match(method) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/free_agent/base.rb', line 31

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)


75
76
77
# File 'lib/free_agent/base.rb', line 75

def bang?
  @bang
end

#creator?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/free_agent/base.rb', line 71

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

#finder?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/free_agent/base.rb', line 63

def finder?
  @finder && !@instantiator
end

#instantiator?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/free_agent/base.rb', line 67

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