Module: Fixjour::RedundancyChecker

Defined in:
lib/fixjour/redundant_check.rb

Overview

Uses method_added hook to make sure no redundant builder methods get defined after a builder has already created them. For example, if you have a Comment builder, this hook will ensure that any attempt to define a #new_comment method will raise an exception.

Constant Summary collapse

BUILDER_METHOD_PATTERN =
/^(new|create|valid)_(\w+)(_attributes)?$/

Instance Method Summary collapse

Instance Method Details

#get_klass_name(name) ⇒ Object



23
24
25
26
27
# File 'lib/fixjour/redundant_check.rb', line 23

def get_klass_name(name)
  if match = name.match(BUILDER_METHOD_PATTERN)
    match[2].classify.gsub(/Attribute$/, '')
  end
end

#method_added(sym) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/fixjour/redundant_check.rb', line 13

def method_added(sym)
  name = sym.to_s
  
  if klass_name = get_klass_name(name)
    if Fixjour.builder_defined?(klass_name)
      raise RedundantBuilder.new("You already defined a builder for #{inspect}")
    end
  end
end