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.



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

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



29
30
31
32
33
# File 'lib/ninny/commands/create_dated_branch.rb', line 29

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

#create_branchObject

Public: Create the desired branch



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

def create_branch
  Ninny.git.new_branch(branch_name, Ninny.project_config.deploy_branch)
end

#date_suffixObject

Public: The date suffix to append to the branch name



24
25
26
# File 'lib/ninny/commands/create_dated_branch.rb', line 24

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



36
37
38
39
40
41
42
43
44
45
# File 'lib/ninny/commands/create_dated_branch.rb', line 36

def delete_old_branches
  return unless extra_branches.any?
  should_delete = should_delete_old_branches || prompt.yes?("Do you want to delete the old #{branch_type} branch(es)? (#{extra_branches.join(", ")})")

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

#execute(input: $stdin, output: $stdout) ⇒ Object



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

def execute(input: $stdin, output: $stdout)
  create_branch
  delete_old_branches
  output.puts "#{branch_name} 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



50
51
52
53
54
# File 'lib/ninny/commands/create_dated_branch.rb', line 50

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

#with_branch_type(&block) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ninny/commands/create_dated_branch.rb', line 56

def with_branch_type(&block)
  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