Class: ActiveModel::AttributeMethods::ClassMethods::AttributeMethodPattern

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

Overview

:nodoc:

Defined Under Namespace

Classes: AttributeMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix: "", suffix: "", parameters: nil) ⇒ AttributeMethodPattern

Returns a new instance of AttributeMethodPattern.



471
472
473
474
475
476
477
478
# File 'lib/active_model/attribute_methods.rb', line 471

def initialize(prefix: "", suffix: "", parameters: nil)
  @prefix = prefix
  @suffix = suffix
  @parameters = parameters.nil? ? "..." : parameters
  @regex = /\A(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})\z/
  @proxy_target = "#{@prefix}attribute#{@suffix}"
  @method_name = "#{prefix}%s#{suffix}"
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



467
468
469
# File 'lib/active_model/attribute_methods.rb', line 467

def parameters
  @parameters
end

#prefixObject (readonly)

Returns the value of attribute prefix.



467
468
469
# File 'lib/active_model/attribute_methods.rb', line 467

def prefix
  @prefix
end

#proxy_targetObject (readonly)

Returns the value of attribute proxy_target.



467
468
469
# File 'lib/active_model/attribute_methods.rb', line 467

def proxy_target
  @proxy_target
end

#suffixObject (readonly)

Returns the value of attribute suffix.



467
468
469
# File 'lib/active_model/attribute_methods.rb', line 467

def suffix
  @suffix
end

Instance Method Details

#match(method_name) ⇒ Object



480
481
482
483
484
# File 'lib/active_model/attribute_methods.rb', line 480

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

#method_name(attr_name) ⇒ Object



486
487
488
# File 'lib/active_model/attribute_methods.rb', line 486

def method_name(attr_name)
  @method_name % attr_name
end