module Detagger

Mixin to allow paths and options to be described as ‘tags:’ that are only pocessed when called for. the tag is satisfied by calls to self

Attributes

tagex[RW]

Public Instance Methods

detag( value, targets = detag_chain, resolved = [] ) click to toggle source
# File lib/detagger.rb, line 34
def detag( value, targets = detag_chain, resolved = [] )
    @tagex ||=  /([^\/:]+:)/
    targets = [targets] unless targets.is_a? Array
    targets.each do |target|
        if ( value.respond_to? :call )
            # manipulates self so that it is called with 'this' and not he context of the proc when defined
            value = target.instance_eval &value
        end

        if value && value.is_a?(String) 
            value = value.shatter(@tagex).flatten.map do |mtch|
                if mtch =~ @tagex
                    raise "Circular Reference" if resolved.include? mtch
                    new_value = drill_down_ref(mtch,targets)
                    mtch == new_value ? new_value : detag(new_value, [target], resolved + [mtch] )
                else
                    mtch
                end
            end
            if (value.uniq == [nil]) 
                value = nil
            else
                value = value.join
            end
        end
    end
    value
end
detag_chain() click to toggle source
# File lib/detagger.rb, line 16
def detag_chain
    (@detag_chain || self)
end
drill_down_ref(txt,targets = detag_chain) click to toggle source
# File lib/detagger.rb, line 20
def drill_down_ref(txt,targets = detag_chain)
    return  unless txt 
    parm = txt.chomp(":").to_sym
    [*targets].each do |target|
        begin
            new_txt = target.send(parm) 
            raise "Self Reference" if new_txt == txt
            return new_txt
        rescue NoMethodError
        end
    end
    txt
end
if_flag( flag ) { |blk| ... } click to toggle source
# File lib/detagger.rb, line 92
def if_flag( flag, &blk )
    if self.send(:detag, self.send(flag) )
        yield blk
    else
        puts "\033[31m!! Skipping step due to #{flag.to_s.quote} being false...\033[0m"
        nil
    end
end
method_missing( method, *args, &blk ) click to toggle source
Calls superclass method
# File lib/detagger.rb, line 67
def method_missing( method, *args, &blk )
    access, orig_method = split_method( method )
    unless orig_method && target = [*detag_chain].find {|t| t.respond_to?( orig_method.to_sym )}
        super(method,*args,&blk)
    else
        rawval = target.send( orig_method, *args, &blk ) 
        (access == "detag") ? detag( rawval ) : rawval  
    end
end
respond_to?( method ) click to toggle source
Calls superclass method
# File lib/detagger.rb, line 77
def respond_to?( method )
    access, orig_method = split_method( method )
    return true if orig_method && [*detag_chain].find {|t| t.respond_to?( orig_method.to_sym )}
    super(method)
end
set_detag_chain(*args) click to toggle source

create a chain of objects that will be used to discover the value behind the tags. This will be ordered, favouring the first item in preference to later ones.

# File lib/detagger.rb, line 12
def set_detag_chain(*args)
    @detag_chain = args
end
split_method(method) click to toggle source
# File lib/detagger.rb, line 63
def split_method(method)
    access, orig_method = *method.to_s.scan(/^(detag|raw)_(.+)$/).flatten
end
unless_flag( flag ) { |blk| ... } click to toggle source
# File lib/detagger.rb, line 83
def unless_flag( flag, &blk )
    unless self.send(:detag, self.send(flag) )
        yield blk
    else
       puts "\033[31m!! Skipping step due to #{flag.to_s.quote} being true...\033[0m"
       nil
    end
end