Class: Poke::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/poke/group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, config_path:) ⇒ Group

Returns a new instance of Group.



31
32
33
34
35
# File 'lib/poke/group.rb', line 31

def initialize(name:, config_path:)
  @name = name
  @config_path = config_path
  @config = Poke::GroupConfig.from_path(config_path)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/poke/group.rb', line 29

def config
  @config
end

#config_pathObject (readonly)

Returns the value of attribute config_path.



29
30
31
# File 'lib/poke/group.rb', line 29

def config_path
  @config_path
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/poke/group.rb', line 29

def name
  @name
end

Class Method Details

.allObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/poke/group.rb', line 13

def self.all
  @all ||= begin
    config_paths = TTY::Command.new(printer: :null).run("find #{Dir.home}/.poke -name 'config.json'").out.split("\n")

    config_paths.map do |config_path|
      name = ::Pathname.new(config_path).parent.basename.to_s
      new(name:, config_path:)
    end
  end
end

.from_request_path(path) ⇒ Object



24
25
26
27
# File 'lib/poke/group.rb', line 24

def self.from_request_path(path)
  group_name = path.to_s.gsub(%r{#{Config.root_path}/([^\/]+)/.*}, '\1')
  new(name: group_name, config_path: "#{Config.root_path}/#{group_name}/config.json")
end

Instance Method Details

#positionObject



45
46
47
# File 'lib/poke/group.rb', line 45

def position
  LastRecentlyUsed.position(namespace: 'groups', key: name.to_s)
end

#requestsObject



41
42
43
# File 'lib/poke/group.rb', line 41

def requests
  @requests ||= Request.all.filter { |request| request.group_name == name }
end

#save_config!Object



37
38
39
# File 'lib/poke/group.rb', line 37

def save_config!
  @config.save_to(@config_path)
end

#use!Object



49
50
51
# File 'lib/poke/group.rb', line 49

def use!
  LastRecentlyUsed.use!(namespace: 'groups', key: name.to_s)
end