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

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

Overview

:nodoc:

Defined Under Namespace

Classes: AttributeMethodMatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AttributeMethodMatcher

Returns a new instance of AttributeMethodMatcher.


391
392
393
394
395
396
# File 'activemodel/lib/active_model/attribute_methods.rb', line 391

def initialize(options = {})
  @prefix, @suffix = options.fetch(:prefix, ""), options.fetch(:suffix, "")
  @regex = /^(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})$/
  @target = "#{@prefix}attribute#{@suffix}"
  @method_name = "#{prefix}%s#{suffix}"
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix


387
388
389
# File 'activemodel/lib/active_model/attribute_methods.rb', line 387

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix


387
388
389
# File 'activemodel/lib/active_model/attribute_methods.rb', line 387

def suffix
  @suffix
end

#targetObject (readonly)

Returns the value of attribute target


387
388
389
# File 'activemodel/lib/active_model/attribute_methods.rb', line 387

def target
  @target
end

Instance Method Details

#match(method_name) ⇒ Object


398
399
400
401
402
# File 'activemodel/lib/active_model/attribute_methods.rb', line 398

def match(method_name)
  if @regex =~ method_name
    AttributeMethodMatch.new(target, $1)
  end
end

#method_name(attr_name) ⇒ Object


404
405
406
# File 'activemodel/lib/active_model/attribute_methods.rb', line 404

def method_name(attr_name)
  @method_name % attr_name
end