Class: ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/attribute_methods.rb

Defined Under Namespace

Classes: AttributeMethodMatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AttributeMethodMatcher

Returns a new instance of AttributeMethodMatcher.



319
320
321
322
323
# File 'lib/active_model/attribute_methods.rb', line 319

def initialize(options = {})
  options.symbolize_keys!
  @prefix, @suffix = options[:prefix] || '', options[:suffix] || ''
  @regex = /^(#{Regexp.escape(@prefix)})(.+?)(#{Regexp.escape(@suffix)})$/
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



315
316
317
# File 'lib/active_model/attribute_methods.rb', line 315

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



315
316
317
# File 'lib/active_model/attribute_methods.rb', line 315

def suffix
  @suffix
end

Instance Method Details

#match(method_name) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/active_model/attribute_methods.rb', line 325

def match(method_name)
  if matchdata = @regex.match(method_name)
    AttributeMethodMatch.new(method_missing_target, matchdata[2])
  else
    nil
  end
end

#method_missing_targetObject



337
338
339
# File 'lib/active_model/attribute_methods.rb', line 337

def method_missing_target
  :"#{prefix}attribute#{suffix}"
end

#method_name(attr_name) ⇒ Object



333
334
335
# File 'lib/active_model/attribute_methods.rb', line 333

def method_name(attr_name)
  "#{prefix}#{attr_name}#{suffix}"
end