class Zoom::Profile

Attributes

exts[RW]
files[RW]
format_flags[RW]
regex[RW]
taggable[RW]

Public Class Methods

from_json(json) click to toggle source
# File lib/zoom/profile.rb, line 68
def self.from_json(json)
    begin
        return profile_by_name(json["class"]).new(
            json["name"],
            json["tool"].nil? ? "" : json["tool"],
            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
new(n = nil, t = nil, f = nil, b = nil, a = nil) click to toggle source
# File lib/zoom/profile.rb, line 135
def initialize(n = nil, t = nil, f = nil, b = nil, a = nil)
    a ||= ""
    b ||= ""
    f ||= ""
    n ||= camel_case_to_underscore(self.class.to_s)
    t ||= "echo"

    self["class"] = self.class.to_s
    after(a)
    before(b)
    flags(f)
    name(n)
    tool(t)

    @exts = Array.new # Set this to only search specified exts
    @files = Array.new # Set this to noly search specified files
    @regex = "" # Setting this will override user input

    # In case someone overrides grep_like_format_flags
    @format_flags = ""
    @grep_like_tags = true
    @taggable = false

    grep_like_format_flags
end
profile_by_name(clas) click to toggle source
# File lib/zoom/profile.rb, line 176
def self.profile_by_name(clas)
    clas.split("::").inject(Object) do |mod, class_name|
        mod.const_get(class_name)
    end
end
subclasses() click to toggle source
# File lib/zoom/profile.rb, line 182
def self.subclasses
    ObjectSpace.each_object(Class).select do |clas|
        if (clas < self)
            begin
                clas.new
                true
            rescue Zoom::Error::ExecutableNotFound
                false
            end
        else
            false
        end
    end
end

Public Instance Methods

after(a = nil) click to toggle source
# File lib/zoom/profile.rb, line 12
def after(a = nil)
    self["after"] = a.strip if (a)
    self["after"] ||= ""
    return self["after"]
end
before(b = nil) click to toggle source
# File lib/zoom/profile.rb, line 18
def before(b = nil)
    self["before"] = b.strip if (b)
    self["before"] ||= ""
    return self["before"]
end
class_name() click to toggle source
# File lib/zoom/profile.rb, line 34
def class_name
    return self["class"]
end
exe(header) click to toggle source
# File lib/zoom/profile.rb, line 38
def exe(header)
    # Emulate grep
    cmd = [
        before,
        tool,
        @format_flags,
        flags,
        only_exts_and_files,
        header["translated"],
        header["args"],
        "--",
        header["regex"].shellescape,
        header["paths"],
        after
    ].join(" ").strip

    if (header.has_key?("debug") && header["debug"])
        puts(cmd)
        return ""
    else
        return %x(#{cmd})
    end
end
flags(f = nil) click to toggle source
# File lib/zoom/profile.rb, line 62
def flags(f = nil)
    self["flags"] = f.strip if (f)
    self["flags"] ||= ""
    return self["flags"]
end
grep_like_format_flags(all = false) click to toggle source
# File lib/zoom/profile.rb, line 84
def grep_like_format_flags(all = false)
    @format_flags = "" # Set this to mirror basic grep
    @taggable = false # Should results be tagged like grep
end
grep_like_tags?() click to toggle source
# File lib/zoom/profile.rb, line 89
def grep_like_tags?
    return @grep_like_tags
end
name(n = nil) click to toggle source
# File lib/zoom/profile.rb, line 161
def name(n = nil)
    self["name"] = n.strip if (n)
    self["name"] ||= ""
    return self["name"]
end
only_exts_and_files() click to toggle source
# File lib/zoom/profile.rb, line 167
def only_exts_and_files
    # Do nothing
    return ""
end
preprocess(header) click to toggle source
# File lib/zoom/profile.rb, line 172
def preprocess(header)
    return header
end
to_s() click to toggle source
# File lib/zoom/profile.rb, line 197
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_tool(tool)) if (!tool.empty?)
    ret.push(hilight_flags(flags)) if (!flags.empty?)
    if (@regex.nil? || @regex.empty?)
        ret.push(hilight_regex("REGEX"))
    else
        ret.push(hilight_regex("\"#{@regex}\""))
    end
    ret.push(hilight_after(after)) if (!after.empty?)
    return ret.join(" ").strip
end
tool(t = nil) click to toggle source
# File lib/zoom/profile.rb, line 213
def tool(t = nil)
    if (t)
        t.strip!
        tl = ScoobyDoo.where_are_you(t)
        raise Zoom::Error::ExecutableNotFound.new(t) if (tl.nil?)
        self["tool"] = t
    end
    return self["tool"]
end
translate(from) click to toggle source
# File lib/zoom/profile.rb, line 223
def translate(from)
    return ""
end

Private Instance Methods

camel_case_to_underscore(clas) click to toggle source
# File lib/zoom/profile.rb, line 24
def camel_case_to_underscore(clas)
    # Convert camelcase class to unscore separated string
    name = clas.to_s.split("::")[-1]
    name.gsub!(/([A-Z]+)([A-Z][a-z])/, "\\1_\\2")
    name.gsub!(/([a-z0-9])([A-Z])/, "\\1_\\2")
    name.tr!("-", "_")
    return name.downcase
end
hilight_after(str) click to toggle source
# File lib/zoom/profile.rb, line 93
def hilight_after(str)
    return str if (!Zoom.hilight?)
    return str.yellow
end
hilight_before(str) click to toggle source
# File lib/zoom/profile.rb, line 99
def hilight_before(str)
    return str if (!Zoom.hilight?)
    return str.yellow
end
hilight_class() click to toggle source
# File lib/zoom/profile.rb, line 105
def hilight_class
    return class_name if (!Zoom.hilight?)
    return class_name.cyan
end
hilight_flags(str) click to toggle source
# File lib/zoom/profile.rb, line 111
def hilight_flags(str)
    return str if (!Zoom.hilight?)
    return str.magenta
end
hilight_name() click to toggle source
# File lib/zoom/profile.rb, line 117
def hilight_name
    return name if (!Zoom.hilight?)
    return name.white
end
hilight_regex(str) click to toggle source
# File lib/zoom/profile.rb, line 123
def hilight_regex(str)
    return str if (!Zoom.hilight?)
    return str
end
hilight_tool(str) click to toggle source
# File lib/zoom/profile.rb, line 129
def hilight_tool(str)
    return str if (!Zoom.hilight?)
    return str.green
end