Class: Zoom::Profile

Inherits:
Hash
  • Object
show all
Defined in:
lib/zoom/profile.rb

Direct Known Subclasses

Ack, Ag, Find, Grep, Pt

Defined Under Namespace

Classes: Ack, Ag, Find, Grep, Passwords, Pt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, o = "echo", f = "#", b = "", a = "") ⇒ Profile

Returns a new instance of Profile.



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/zoom/profile.rb', line 178

def initialize(n, o = "echo", f = "#", b = "", a = "")
    self["class"] = self.class.to_s
    after(a)
    before(b)
    flags(f)
    name(n)
    operator(o)

    @pattern = "" # Setting this will override user input
    @taggable = false
end

Instance Attribute Details

#pattern ⇒ Object (readonly)

Returns the value of attribute pattern.



6
7
8
# File 'lib/zoom/profile.rb', line 6

def pattern
  @pattern
end

#taggable ⇒ Object (readonly)

Returns the value of attribute taggable.



7
8
9
# File 'lib/zoom/profile.rb', line 7

def taggable
  @taggable
end

Class Method Details

.from_json(json) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/zoom/profile.rb', line 116

def self.from_json(json)
    begin
        return profile_by_name(json["class"]).new(
            json["name"],
            json["operator"].nil? ? "" : json["operator"],
            json["flags"].nil? ? "" : json["flags"],
            json["before"].nil? ? "" : json["before"],
            json["after"].nil? ? "" : json["after"]
        )
    rescue NoMethodError
        raise Zoom::Error::ProfileNotNamed.new(json)
    rescue NameError
        raise Zoom::Error::ProfileClassUnknown.new(json["class"])
    end
end

.profile_by_name(clas) ⇒ Object



205
206
207
208
209
# File 'lib/zoom/profile.rb', line 205

def self.profile_by_name(clas)
    clas.split("::").inject(Object) do |mod, class_name|
        mod.const_get(class_name)
    end
end

.subclasses ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/zoom/profile.rb', line 211

def self.subclasses
    ObjectSpace.each_object(Class).select do |clas|
        if (clas < self)
            begin
                clas.new(clas.to_s)
                true
            rescue Zoom::Error::ExecutableNotFound
                false
            end
        else
            false
        end
    end
end

Instance Method Details

#after(a = nil) ⇒ Object



9
10
11
12
# File 'lib/zoom/profile.rb', line 9

def after(a = nil)
    self["after"] = a.strip if (a)
    return self["after"]
end

#before(b = nil) ⇒ Object



14
15
16
17
# File 'lib/zoom/profile.rb', line 14

def before(b = nil)
    self["before"] = b.strip if (b)
    return self["before"]
end

#class_name ⇒ Object



19
20
21
# File 'lib/zoom/profile.rb', line 19

def class_name
    return self["class"]
end

#exe(args, pattern, paths) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/zoom/profile.rb', line 23

def exe(args, pattern, paths)
    # Use hard-coded pattern if defined
    pattern = @pattern if (@pattern && !@pattern.empty?)

    # If not pattern and no after, then return nothing
    if (pattern.nil? || pattern.empty?)
        return "" if (after.nil? || after.empty? || after == ".")
    end

    # If paths are specified then remove "." for profiles like
    # grep
    after.gsub!(/^\.\s+/, "") if (!paths.empty?)

    # Emulate grep
    case operator.split("/")[-1]
    when "ack", "ack-grep"
        cmd = [
            before,
            operator,
            "-H --nobreak --nocolor --noheading -s",
            flags,
            args,
            pattern.shellescape,
            paths,
            after
        ].join(" ").strip
    when "ag"
        cmd = [
            before,
            operator,
            "--filename --nobreak --nocolor --noheading --silent",
            flags,
            args,
            pattern.shellescape,
            paths,
            after
        ].join(" ").strip
    when "find"
        flags.gsub!(/^\.\s+/, "") if (!paths.empty?)
        cmd = [
            before,
            operator,
            paths,
            flags,
            args,
            "\"#{pattern}\"",
            after
        ].join(" ").strip
    when "grep"
        cmd = [
            before,
            operator,
            "--color=never -EHInRs",
            "--exclude-dir=.bzr",
            "--exclude-dir=.git",
            "--exclude-dir=.git-crypt",
            "--exclude-dir=.svn",
            flags,
            args,
            pattern.shellescape,
            paths,
            after
        ].join(" ").strip
    when "pt"
        cmd = [
            before,
            operator,
            "-e --nocolor --nogroup",
            flags,
            args,
            pattern.shellescape,
            paths,
            after
        ].join(" ").strip
    else
        cmd = [
            before,
            operator,
            flags,
            args,
            pattern,
            paths,
            after
        ].join(" ").strip
    end
    return %x(#{cmd})
end

#flags(f = nil) ⇒ Object



111
112
113
114
# File 'lib/zoom/profile.rb', line 111

def flags(f = nil)
    self["flags"] = f.strip if (f)
    return self["flags"]
end

#go(editor, results) ⇒ Object



132
133
134
# File 'lib/zoom/profile.rb', line 132

def go(editor, results)
    Zoom::Editor.new(editor).open(results)
end

#name(n = nil) ⇒ Object



190
191
192
193
# File 'lib/zoom/profile.rb', line 190

def name(n = nil)
    self["name"] = n.strip if (n)
    return self["name"]
end

#operator(o = nil) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/zoom/profile.rb', line 195

def operator(o = nil)
    if (o)
        o.strip!
        op = ScoobyDoo.where_are_you(o)
        raise Zoom::Error::ExecutableNotFound.new(o) if (op.nil?)
        self["operator"] = o
    end
    return self["operator"]
end

#to_s ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/zoom/profile.rb', line 226

def to_s
    ret = Array.new
    ret.push(hilight_name)
    ret.push("#{hilight_class}\n")
    ret.push(hilight_before(before)) if (!before.empty?)
    ret.push(hilight_operator(operator)) if (!operator.empty?)
    ret.push(hilight_flags(flags)) if (!flags.empty?)
    if (@pattern.nil? || @pattern.empty?)
        ret.push(hilight_pattern("PATTERN"))
    else
        ret.push(hilight_pattern("\"#{@pattern}\""))
    end
    ret.push(hilight_after(after)) if (!after.empty?)
    return ret.join(" ").strip
end