Class: Datapathy::Model::DynamicFinders::ClassMethods::DynamicFinderMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/datapathy/model/dynamic_finders.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ DynamicFinderMatch

Returns a new instance of DynamicFinderMatch.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/datapathy/model/dynamic_finders.rb', line 60

def initialize(method)
  @finder = :first
  case method.to_s
  when /^find_(all_by|last_by|by)_([_a-zA-Z]\w*)$/
    @finder = :last if $1 == 'last_by'
    @finder = :all if $1 == 'all_by'
    names = $2
  when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
    @instantiator = $1 == 'initialize' ? :new : :create
    names = $2
  else
    @finder = nil
  end
  @attribute_names = names && names.split('_and_')
end

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



53
54
55
# File 'lib/datapathy/model/dynamic_finders.rb', line 53

def attribute_names
  @attribute_names
end

#finderObject (readonly)

Returns the value of attribute finder.



53
54
55
# File 'lib/datapathy/model/dynamic_finders.rb', line 53

def finder
  @finder
end

#instantiatorObject (readonly)

Returns the value of attribute instantiator.



53
54
55
# File 'lib/datapathy/model/dynamic_finders.rb', line 53

def instantiator
  @instantiator
end

Class Method Details

.match(method) ⇒ Object



55
56
57
58
# File 'lib/datapathy/model/dynamic_finders.rb', line 55

def self.match(method)
  df_match = self.new(method)
  df_match.finder ? df_match : nil
end

Instance Method Details

#finder?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/datapathy/model/dynamic_finders.rb', line 76

def finder?
  !@finder.nil? && @instantiator.nil?
end

#instantiator?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/datapathy/model/dynamic_finders.rb', line 80

def instantiator?
  @finder == :first && !@instantiator.nil?
end