class BeStrong::Code

Constants

REG_MASS_ASSIGNMENT_METHOD

Public Class Methods

new(code) click to toggle source
# File lib/be_strong/code.rb, line 7
def initialize(code)
  @code     = code
  @original = code.dup
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/be_strong/code.rb, line 69
def <=>(other)
  code <=> other
end
add_private!() click to toggle source
# File lib/be_strong/code.rb, line 44
def add_private!
  unless code.include?('private')
    code.sub!(/^end/) do
      "\n  private\nend"
    end
  end
  self
end
apply_strong_parameter!() click to toggle source
# File lib/be_strong/code.rb, line 12
def apply_strong_parameter!
  # replace params[:model] to model_params
  models = []
  code.gsub!(REG_MASS_ASSIGNMENT_METHOD) do
    if StrongParameterMethods.method_for($4)
      models << $4
      "#{$2}#{$3}#{$4}_params"
    else
      $1
    end
  end

  return self if models.size.zero?

  # add private section
  add_private!

  # add model_params method as private method
  models.each do |model|
    method = StrongParameterMethods.method_for(model)
    next unless method

    next if code.include?("def #{model}_params")

    code.sub!(/^  private$/) do
      "  private\n\n#{method.gsub(/^/, '  ').chomp}"
    end
  end

  self
end
changed?() click to toggle source
# File lib/be_strong/code.rb, line 61
def changed?
  code != @original
end
remove_attr_accessible_and_protected!() click to toggle source
# File lib/be_strong/code.rb, line 53
def remove_attr_accessible_and_protected!
  %w(accessible protected).each do |name|
    code.gsub!(/( *attr_#{name}\(.*?\)$)/m, '')
    code.gsub!(/( *attr_#{name}.+?[^,\\]$)/m, '')
  end
  self
end
to_str() click to toggle source
# File lib/be_strong/code.rb, line 65
def to_str
  code
end

Private Instance Methods

code() click to toggle source
# File lib/be_strong/code.rb, line 75
def code
  @code
end