Class: Binbundle::JewelryBox

Inherits:
Array
  • Object
show all
Defined in:
lib/binbundle/jewelry_box.rb

Overview

String helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_version: true, sudo: false, user_install: false, contents: nil) ⇒ JewelryBox

Create a new JewelryBox object

Parameters:

  • include_version (Boolean) (defaults to: true)

    include version

  • sudo (Boolean) (defaults to: false)

    include sudo

  • user_install (Boolean) (defaults to: false)

    include –user-install

  • contents (String) (defaults to: nil)

    The contents to parse



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/binbundle/jewelry_box.rb', line 24

def initialize(include_version: true, sudo: false, user_install: false, contents: nil)
  @include_version = include_version
  @sudo = sudo
  @user_install = user_install

  super()

  return unless contents

  init_from_contents(contents)
end

Instance Attribute Details

#include_version=(value) ⇒ Object (writeonly)

Include version info in output



7
8
9
# File 'lib/binbundle/jewelry_box.rb', line 7

def include_version=(value)
  @include_version = value
end

#sudo=(value) ⇒ Object (writeonly)

Include sudo



10
11
12
# File 'lib/binbundle/jewelry_box.rb', line 10

def sudo=(value)
  @sudo = value
end

#user_install=(value) ⇒ Object (writeonly)

Include –user-install



13
14
15
# File 'lib/binbundle/jewelry_box.rb', line 13

def user_install=(value)
  @user_install = value
end

Instance Method Details

#bins_for_gem(gem) ⇒ Object

List binaries for a given gem name

Parameters:

  • gem (String)

    The gem name to search for



73
74
75
76
77
78
# File 'lib/binbundle/jewelry_box.rb', line 73

def bins_for_gem(gem)
  m = select { |g| g.gem == gem }.first
  return "Gem #{gem} not found" unless m

  m.bins ? m.bins.join(', ') : 'Missing info'
end

#gem_for_bin(bin) ⇒ String

Find a gem for a given binary name

Parameters:

  • bin (String)

    The bin to search for

Returns:

  • (String)

    Associated gem name



61
62
63
64
65
66
# File 'lib/binbundle/jewelry_box.rb', line 61

def gem_for_bin(bin)
  m = select { |gem| gem.bins&.include?(bin) }.first
  return "Gem for #{bin} not found" unless m

  m.gem
end

#init_from_contents(contents) ⇒ Object

Read and parse a Binfile for gems to install

Parameters:

  • contents (String)

    The contents



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/binbundle/jewelry_box.rb', line 41

def init_from_contents(contents)
  rx = /(?mix)(?:\#\sExecutables:\s(?<bins>[\S\s,]+?)\n)?(?:sudo\s)?gem\sinstall
        \s(?:--user-install\s)?
        (?<gem>\S+)(?:\s(?:-v|--version)\s'(?<version>[0-9.]+)')?/
  contents.to_enum(:scan, rx).map { Regexp.last_match }.each do |m|
    g = Jewel.new(m['gem'], m['bins'], m['version'])
    g.include_version = @include_version
    g.sudo = @sudo
    g.user_install = @user_install
    push(g)
  end
end

#to_sString

Output a Binfile version of the JewelryBox

Returns:

  • (String)

    String representation of the object.



85
86
87
# File 'lib/binbundle/jewelry_box.rb', line 85

def to_s
  map(&:to_s).join("\n")
end