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.



347
348
349
350
351
# File 'lib/active_model/attribute_methods.rb', line 347

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.



343
344
345
# File 'lib/active_model/attribute_methods.rb', line 343

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



343
344
345
# File 'lib/active_model/attribute_methods.rb', line 343

def suffix
  @suffix
end

Instance Method Details

#match(method_name) ⇒ Object



353
354
355
356
357
358
359
# File 'lib/active_model/attribute_methods.rb', line 353

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

#method_missing_targetObject



365
366
367
# File 'lib/active_model/attribute_methods.rb', line 365

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

#method_name(attr_name) ⇒ Object



361
362
363
# File 'lib/active_model/attribute_methods.rb', line 361

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