Class: Macrow
- Inherits:
-
Object
- Object
- Macrow
- Defined in:
- lib/macrow.rb,
lib/macrow/error.rb,
lib/macrow/version.rb more...
Defined Under Namespace
Classes: ReplaceError
Constant Summary collapse
- VERSION =
"0.1.1"
Class Method Summary collapse
- .inherited(child) ⇒ Object
- .macro_prefix(prefix) ⇒ Object
- .macro_suffix(suffix) ⇒ Object
- .replace_string(keyword) ⇒ Object
- .rule(keyword, &block) ⇒ Object
- .rule_words ⇒ Object
- .rules ⇒ Object
Instance Method Summary collapse
- #apply_all_rules(str, object = nil) ⇒ Object (also: #apply)
- #apply_rule!(rule, str, object) ⇒ Object
Class Method Details
permalink .inherited(child) ⇒ Object
[View source]
6 7 8 |
# File 'lib/macrow.rb', line 6 def inherited(child) child.instance_variable_set(:@rule_words, rule_words.dup) end |
permalink .macro_prefix(prefix) ⇒ Object
[View source]
18 19 20 |
# File 'lib/macrow.rb', line 18 def macro_prefix(prefix) @macro_prefix = prefix end |
permalink .macro_suffix(suffix) ⇒ Object
[View source]
22 23 24 |
# File 'lib/macrow.rb', line 22 def macro_suffix(suffix) @macro_suffix = suffix end |
permalink .replace_string(keyword) ⇒ Object
[View source]
41 42 43 |
# File 'lib/macrow.rb', line 41 def replace_string(keyword) "#{_macro_prefix}#{keyword}#{_macro_suffix}" end |
permalink .rule(keyword, &block) ⇒ Object
[View source]
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.}'" end end end end |
permalink .rule_words ⇒ Object
[View source]
10 11 12 |
# File 'lib/macrow.rb', line 10 def rule_words @rule_words ||= [] end |
permalink .rules ⇒ Object
[View source]
14 15 16 |
# File 'lib/macrow.rb', line 14 def rules yield end |
Instance Method Details
permalink #apply_all_rules(str, object = nil) ⇒ Object Also known as: apply
[View source]
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/macrow.rb', line 56 def apply_all_rules(str, object = nil) return if str.nil? || !str.is_a?(String) dup_str = str.dup self.class.rule_words.each do |rule_word| apply_rule!(rule_word, dup_str, object) end dup_str end |
permalink #apply_rule!(rule, str, object) ⇒ Object
[View source]
69 70 71 |
# File 'lib/macrow.rb', line 69 def apply_rule!(rule, str, object) __send__("apply_rule_for_#{rule}!", str, object) end |