Class: Gitw::GitOpts

Inherits:
Object
  • Object
show all
Defined in:
lib/gitw/git_opts.rb

Overview

git options allow to define available options and to create standard args for execution

Instance Method Summary collapse

Constructor Details

#initializeGitOpts

Returns a new instance of GitOpts.



8
9
10
11
12
13
# File 'lib/gitw/git_opts.rb', line 8

def initialize
  @allowed = []
  @allowed_index = {}

  @opts = []
end

Instance Method Details

#add(label, arg = nil) ⇒ Object



40
41
42
43
44
# File 'lib/gitw/git_opts.rb', line 40

def add(label, arg = nil)
  @opts << [label, arg]

  self
end

#allow(label, **kwargs) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitw/git_opts.rb', line 15

def allow(label, **kwargs)
  allowed_opt = GitAllowedOpt.new(label, **kwargs)
  return unless allowed_opt

  @allowed << allowed_opt
  allowed_opt.each_label do |allowed_opt_label|
    @allowed_index[allowed_opt_label] = allowed_opt
  end

  self
end

#from(options) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/gitw/git_opts.rb', line 64

def from(options)
  case options
  when Hash then from_h(options)
  when Array then from_a(options)
  else
    self
  end
end

#from_a(options) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/gitw/git_opts.rb', line 46

def from_a(options)
  options.each do |option|
    add(option)
  end

  self
end

#from_h(options) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/gitw/git_opts.rb', line 54

def from_h(options)
  options.each do |k, v|
    next if v == false

    add(k, v)
  end

  self
end

#optsObject



27
28
29
30
31
32
33
34
# File 'lib/gitw/git_opts.rb', line 27

def opts
  @opts.map do |label, arg|
    linked_allowed_opt = @allowed_index[label]
    next unless linked_allowed_opt

    linked_allowed_opt.build_opt(arg)
  end.compact
end

#to_aObject



36
37
38
# File 'lib/gitw/git_opts.rb', line 36

def to_a
  opts.flatten
end