Module: Terjira::OptionSupportable

Included in:
BaseCLI
Defined in:
lib/terjira/option_supportable.rb

Overview

For support CLI options.

Constant Summary collapse

OPTION_TO_SELECTOR =
{
  project: :select_project,
  board: :select_board,
  summary: :write_summary,
  description: :write_description,
  sprint: :select_sprint,
  issuetype: :select_issuetype,
  assignee: :select_assignee,
  status: :select_issue_status,
  priority: :select_priority,
  resolution: :select_resolution,
  epiclink: :write_epiclink_key,
  comment: :write_comment,
  editable_comment: :update_comment
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
11
12
13
# File 'lib/terjira/option_supportable.rb', line 8

def self.included(klass)
  klass.class_eval do
    extend SharedOptions
    include OptionSelector
  end
end

Instance Method Details

#resource_storeObject



102
103
104
# File 'lib/terjira/option_supportable.rb', line 102

def resource_store
  ResourceStore.instance
end

#suggest_options(opts = {}) ⇒ Object

Transforming and clening options and suggest list of option values



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/terjira/option_supportable.rb', line 33

def suggest_options(opts = {})
  origin = options.dup
  self.with_editor = origin.delete(:editor)

  if opts[:required].is_a? Array
    opts[:required].inject(origin) do |memo, opt|
      memo[opt] ||= opt.to_s
      memo
    end
  end

  # Store assigned options
  origin.reject { |k, v| k.to_s.casecmp(v.to_s).zero? }.each do |k, v|
    resource_store.set(k.to_sym, v)
  end

  # Store given options from arguments
  (opts[:resources] || {}).each do |k, v|
    resource_store.set(k.to_sym, v)
  end

  # Select options that are not assigned value from user
  default_value_options = origin.select do |k, v|
    k.to_s.casecmp(v.to_s).zero?
  end

  # Sort order for suggest option values
  default_value_options = default_value_options.sort do |hash|
    OPTION_TO_SELECTOR.keys.index(hash[0].to_sym) || 999
  end
  default_value_options = Hash[default_value_options]

  # Suggest option values and save to resource store
  default_value_options.each do |k, _v|
    selector_method = OPTION_TO_SELECTOR[k.to_sym]
    send(selector_method) if selector_method
  end

  # Fetch selected values from resource store
  default_value_options.each do |k, _v|
    default_value_options[k] = resource_store.get(k)
  end

  origin.merge! default_value_options
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/terjira/option_supportable.rb', line 79

def suggest_related_value_options(opts = {})
  if opts[:issuetype]
    if opts[:issuetype].key_value.casecmp('epic').zero?
      # Suggest epic name
      epic_name_field = Client::Field.epic_name
      opts[epic_name_field.key] ||= write_epic_name
    else
      subtask_issuetypes = Client::Issuetype.subtask_issuetypes.map(&:name)
      if subtask_issuetypes.include? opts[:issuetype].key_value
        # Suggest parent issue
        opts[:parent] ||= write_parent_issue_key
      end
    end
  end

  if opts[:epiclink]
    epiclink_field = Client::Field.epic_link
    opts[epiclink_field.key] ||= opts.delete(:epiclink)
  end

  opts
end