Class: Ninny::Commands::CreateDatedBranch

Inherits:
Ninny::Command show all
Defined in:
lib/ninny/commands/create_dated_branch.rb

Direct Known Subclasses

NewStaging

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ninny::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ CreateDatedBranch

Returns a new instance of CreateDatedBranch.



8
9
10
11
# File 'lib/ninny/commands/create_dated_branch.rb', line 8

def initialize(options)
  @branch_type = options[:branch_type] || Git::STAGING_PREFIX
  @should_delete_old_branches = options[:delete_old_branches]
end

Instance Attribute Details

#branch_typeObject (readonly)

Returns the value of attribute branch_type.



6
7
8
# File 'lib/ninny/commands/create_dated_branch.rb', line 6

def branch_type
  @branch_type
end

#should_delete_old_branchesObject (readonly)

Returns the value of attribute should_delete_old_branches.



6
7
8
# File 'lib/ninny/commands/create_dated_branch.rb', line 6

def should_delete_old_branches
  @should_delete_old_branches
end

Instance Method Details

#branch_nameObject

Public: The name of the branch to create



31
32
33
34
35
# File 'lib/ninny/commands/create_dated_branch.rb', line 31

def branch_name
  with_branch_type do
    "#{branch_type}.#{date_suffix}"
  end
end

#create_branchObject

Public: Create the desired branch



20
21
22
23
# File 'lib/ninny/commands/create_dated_branch.rb', line 20

def create_branch
  prompt.say "Attempting to create branch #{branch_name}."
  Ninny.git.new_branch(branch_name, Ninny.project_config.deploy_branch)
end

#date_suffixObject

Public: The date suffix to append to the branch name



26
27
28
# File 'lib/ninny/commands/create_dated_branch.rb', line 26

def date_suffix
  Date.today.strftime('%Y.%m.%d')
end

#delete_old_branchesObject

Public: If necessary, and if user opts to, delete old branches of its type



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ninny/commands/create_dated_branch.rb', line 38

def delete_old_branches
  return unless extra_branches.any?

  should_delete = should_delete_old_branches || prompt.no?(
    "Do you want to delete the old #{branch_type} branch(es)? (#{extra_branches.join(', ')})"
  )

  return unless should_delete

  extra_branches.each do |extra|
    Ninny.git.delete_branch(extra)
  end
end

#execute(output: $stdout) ⇒ Object



13
14
15
16
17
# File 'lib/ninny/commands/create_dated_branch.rb', line 13

def execute(output: $stdout)
  create_branch
  delete_old_branches
  output.puts "Branch #{branch_name} successfully created."
end

#extra_branchesObject

Public: The list of extra branches that exist after creating the new branch

Returns an Array of Strings of the branch names



55
56
57
58
59
# File 'lib/ninny/commands/create_dated_branch.rb', line 55

def extra_branches
  with_branch_type do
    Ninny.git.branches_for(branch_type).reject { |branch| branch.name == branch_name }
  end
end

#with_branch_typeObject



61
62
63
64
65
66
67
68
# File 'lib/ninny/commands/create_dated_branch.rb', line 61

def with_branch_type
  case branch_type
  when Git::DEPLOYABLE_PREFIX, Git::STAGING_PREFIX, Git::QAREADY_PREFIX
    yield
  else
    raise InvalidBranchType, "'#{branch_type}' is not a valid branch type"
  end
end