Class: BeStrong::Code
- Inherits:
-
Object
- Object
- BeStrong::Code
- Includes:
- Comparable
- Defined in:
- lib/be_strong/code.rb
Constant Summary collapse
- REG_MASS_ASSIGNMENT_METHOD =
/((new|build|build_.*|update|update!|assign_attributes|update_attributes|update_attributes!)([\( )])params\[:(\w*)\])/
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add_private! ⇒ Object
- #apply_strong_parameter! ⇒ Object
- #changed? ⇒ Boolean
-
#initialize(code) ⇒ Code
constructor
A new instance of Code.
- #remove_attr_accessible_and_protected! ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(code) ⇒ Code
Returns a new instance of Code.
7 8 9 10 |
# File 'lib/be_strong/code.rb', line 7 def initialize(code) @code = code @original = code.dup end |
Instance Method Details
#<=>(other) ⇒ Object
69 70 71 |
# File 'lib/be_strong/code.rb', line 69 def <=>(other) code <=> other end |
#add_private! ⇒ Object
44 45 46 47 48 49 50 51 |
# 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! ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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? ⇒ Boolean
61 62 63 |
# File 'lib/be_strong/code.rb', line 61 def changed? code != @original end |
#remove_attr_accessible_and_protected! ⇒ Object
53 54 55 56 57 58 59 |
# 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 ⇒ Object
65 66 67 |
# File 'lib/be_strong/code.rb', line 65 def to_str code end |