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, Pt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Profile.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/zoom/profile.rb', line 132

def initialize(n = nil, o = nil, f = nil, b = nil, a = nil)
    a ||= ""
    b ||= ""
    f ||= ""
    n ||= camel_case_to_underscore(self.class.to_s)
    o ||= "echo"

    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

#format_flagsObject (readonly)

Returns the value of attribute format_flags.



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

def format_flags
  @format_flags
end

#patternObject (readonly)

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

#taggableObject (readonly)

Returns the value of attribute taggable.



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

def taggable
  @taggable
end

Class Method Details

.from_json(json) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zoom/profile.rb', line 70

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



209
210
211
212
213
# File 'lib/zoom/profile.rb', line 209

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

.subclassesObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/zoom/profile.rb', line 215

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



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

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

#before(b = nil) ⇒ Object



16
17
18
19
20
# File 'lib/zoom/profile.rb', line 16

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

#class_nameObject



32
33
34
# File 'lib/zoom/profile.rb', line 32

def class_name
    return self["class"]
end

#exe(header) ⇒ Object



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
# File 'lib/zoom/profile.rb', line 36

def exe(header)
    # Emulate grep
    case operator.split("/")[-1]
    when "find"
        cmd = [
            before,
            operator,
            header["paths"],
            flags,
            header["args"],
            header["pattern"],
            after
        ].join(" ").strip
    else
        cmd = [
            before,
            operator,
            @format_flags,
            flags,
            header["args"],
            header["pattern"].shellescape,
            header["paths"],
            after
        ].join(" ").strip
    end
    return %x(#{cmd})
end

#flags(f = nil) ⇒ Object



64
65
66
67
68
# File 'lib/zoom/profile.rb', line 64

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

#go(editor, results) ⇒ Object



86
87
88
# File 'lib/zoom/profile.rb', line 86

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

#name(n = nil) ⇒ Object



150
151
152
153
154
# File 'lib/zoom/profile.rb', line 150

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

#operator(o = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/zoom/profile.rb', line 156

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

#preprocess(header) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/zoom/profile.rb', line 166

def preprocess(header)
    # Use hard-coded pattern if defined
    if (
        @pattern &&
        !@pattern.empty? &&
        (header["pattern"] != @pattern)
    )
        header["args"] += " #{header["pattern"]}"
        header["pattern"] = @pattern
    end

    case operator.split("/")[-1]
    when /^ack(-grep)?$/, "ag", "grep", "pt"
        paths = header["paths"].split(" ")
        if (header["pattern"].empty? && !paths.empty?)
            header["pattern"] = paths.delete_at(0)
            header["paths"] = paths.join(" ").strip
            header["paths"] = "." if (header["paths"].empty?)
        end

        # This isn't done here anymore as it'll break hilighting
        # header["pattern"] = header["pattern"].shellescape
    when "find"
        # If additional args are passed, then assume pattern is
        # actually an arg
        if (header["args"] && !header["args"].empty?)
            header["args"] += " #{header["pattern"]}"
            header["pattern"] = ""
        end

        # If pattern was provided then assume it's an iname search
        if (header["pattern"] && !header["pattern"].empty?)
            header["pattern"] = "-iname \"#{header["pattern"]}\""
        end
    end

    # Translate any needed flags
    header["args"] += " #{translate(header["translate"])}"
    header["args"].strip!

    return header
end

#to_sObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/zoom/profile.rb', line 230

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

#translate(from) ⇒ Object



246
247
248
# File 'lib/zoom/profile.rb', line 246

def translate(from)
    return ""
end