Method: Macrow.rule

Defined in:
lib/macrow.rb

.rule(keyword, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/macrow.rb', line 26

def rule(keyword, &block)
  rule_words << keyword

  define_method "apply_rule_for_#{keyword}!" do |str, object|
    if str.include? replace_string(keyword)
      begin
        str.gsub!(replace_string(keyword), block.call(object).to_s)
        str
      rescue NoMethodError => e
        fail Macrow::ReplaceError, "NoMethodError is raised on applying rule: '#{keyword}'. Please check your rule. Error message is '#{e.message}'"
      end
    end
  end
end