Created
October 3, 2011 09:51
-
-
Save ritxi/1258794 to your computer and use it in GitHub Desktop.
Replaceable module for ruby String class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Replaceable | |
def self.include_me!(_class) | |
_class.class_eval do | |
include Replaceable | |
end | |
end | |
def replace_values!(locals) | |
locals.each{ |key, val| self.gsub!("%%#{key.to_s.upcase}%%", val) } | |
self | |
end | |
def parse_and_replace_values!(locals, proc_param=nil) | |
self.replace_values!(self.parse_variables(locals, proc_param)) | |
end | |
def parse_variables(locals, proc_param=nil) | |
locals.reject!{ |key, val| !required_variable?(key) } | |
locals.select{ |key, val| val.is_a?(Proc) }. | |
each{ |key, proc| locals[key] = call_proc(proc, proc_param) } | |
locals | |
end | |
private | |
def call_proc(proc, param) | |
# when a param is required it should not be nil otherwise return an empty string | |
proc.parameters.empty? ? proc.call : call_proc_with_param(proc, param) | |
end | |
def call_proc_with_param(proc, param) | |
(!param.nil? ? proc.call(param) : "") | |
end | |
def required_variable?(var) | |
!self[%r{\%\%#{var.to_s.upcase}\%\%}].blank? | |
end | |
end | |
Replaceable.include_me!(String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment