Module: Detagger

Defined in:
lib/detagger.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



67
68
69
70
71
72
73
74
75
# 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

Instance Attribute Details

#tagexObject

Returns the value of attribute tagex.



8
9
10
# File 'lib/detagger.rb', line 8

def tagex
  @tagex
end

Instance Method Details

#detag(value, targets = detag_chain, resolved = []) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_chainObject



16
17
18
# File 'lib/detagger.rb', line 16

def detag_chain
    (@detag_chain || self)
end

#drill_down_ref(txt, targets = detag_chain) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



92
93
94
95
96
97
98
99
# 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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# 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) ⇒ Object

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.



12
13
14
# File 'lib/detagger.rb', line 12

def set_detag_chain(*args)
    @detag_chain = args
end

#split_method(method) ⇒ Object



63
64
65
# 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) ⇒ Object



83
84
85
86
87
88
89
90
# 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