Class: Zoom::Profile

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

Direct Known Subclasses

Ack, Ag, Find, Grep, Passwords, Pt

Defined Under Namespace

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

Constant Summary collapse

@@cache_file =
"~/.zoom_cache"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator = "echo", flags = "#", envprepend = "", append = "") ⇒ Profile

Returns a new instance of Profile.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zoom/profile.rb', line 60

def initialize(
    operator = "echo",
    flags = "#",
    envprepend = "",
    append = ""
)
    self["class"] = self.class.to_s
    self.flags(flags)
    self.prepend(envprepend)
    self.append(append)
    self.operator(operator)

    @immutable = false
    @pager = "z --pager --cache-file #{@@cache_file}"
    @taggable = false
end

Instance Attribute Details

#immutable ⇒ Object

Returns the value of attribute immutable.



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

def immutable
  @immutable
end

#taggable ⇒ Object

Returns the value of attribute taggable.



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

def taggable
  @taggable
end

Class Method Details

.cache_file(cache_file) ⇒ Object



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

def self.cache_file(cache_file)
    @@cache_file = cache_file
    @@cache_file ||= "~/.zoom_cache"
end

.from_json(json) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zoom/profile.rb', line 35

def self.from_json(json)
    begin
        return profile_by_name(json["class"]).new(
            json["operator"].nil? ? "" : json["operator"],
            json["flags"].nil? ? "" : json["flags"],
            json["prepend"].nil? ? "" : json["prepend"],
            json["append"].nil? ? "" : json["append"]
        )
    rescue NameError => e
        raise Zoom::Error::ProfileClassUnknownError.new(
            json["class"]
        )
    end
end

.profile_by_name(clas) ⇒ Object



97
98
99
100
101
# File 'lib/zoom/profile.rb', line 97

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

Instance Method Details

#append(append = nil) ⇒ Object



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

def append(append = nil)
    self["append"] = append if (append)
    return self["append"]
end

#colors ⇒ Object



21
22
23
24
# File 'lib/zoom/profile.rb', line 21

def colors
    # TODO color support
    ""
end

#exe(args, pattern) ⇒ Object



26
27
28
# File 'lib/zoom/profile.rb', line 26

def exe(args, pattern)
    system("#{self.to_s} #{args} #{pattern} #{self.append}")
end

#flags(flags = nil) ⇒ Object



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

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

#info ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/zoom/profile.rb', line 50

def info
    [
        "Class   : #{self.class.to_s}",
        "Prepend : #{self["prepend"]}",
        "Operator: #{self["operator"]}",
        "Flags   : #{self["flags"]}",
        "Append  : #{self["append"]}"
    ].join("\n").strip
end

#operator(operator = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/zoom/profile.rb', line 77

def operator(operator = nil)
    if (operator)
        op = ScoobyDoo.where_are_you(operator)
        if (op)
            self["operator"] = op
        else
            self["operator"] = ScoobyDoo.where_are_you("echo")
            flags("")
            prepend("")
            append("#{operator} is not installed!")
        end
    end
    return self["operator"]
end

#prepend(envprepend = nil) ⇒ Object



92
93
94
95
# File 'lib/zoom/profile.rb', line 92

def prepend(envprepend = nil)
    self["prepend"] = envprepend if (envprepend)
    return self["prepend"]
end

#to_s ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/zoom/profile.rb', line 103

def to_s
    [
        self.colors,
        self["prepend"],
        self["operator"],
        self["flags"]
    ].join(" ").strip
end