Class: Pullable::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/pullable/processor.rb

Constant Summary collapse

CONFIG_FILE =
".pullable.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, directory, options = {}) ⇒ Processor

Returns a new instance of Processor.



9
10
11
12
13
14
15
# File 'lib/pullable/processor.rb', line 9

def initialize(method, directory, options = {})
  @method    = method
  @directory = directory
  @options   = options

  @branch    = `git rev-parse --abbrev-ref HEAD` || 'master'
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



7
8
9
# File 'lib/pullable/processor.rb', line 7

def branch
  @branch
end

#directoryObject (readonly)

Returns the value of attribute directory.



7
8
9
# File 'lib/pullable/processor.rb', line 7

def directory
  @directory
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/pullable/processor.rb', line 7

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/pullable/processor.rb', line 7

def options
  @options
end

Class Method Details

.update!(method, directory) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/pullable/processor.rb', line 17

def self.update!(method, directory)
  options   = File.exists?(CONFIG_FILE) ? YAML.load(File.read(CONFIG_FILE)) : {}
  processor = new(method, directory, options)

  if processor.options['mirror']
    processor.mirror!(options)
  else
    processor.update!(options)
  end
end

Instance Method Details

#commandObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/pullable/processor.rb', line 42

def command
  case method
  when 'merge'
    "git merge --ff-only origin/#{branch}"
  when 'pull'
    "git pull origin #{branch}"
  else
    raise NotImplementedError
  end
end

#mirror!(options = {}) ⇒ Object



28
29
30
31
# File 'lib/pullable/processor.rb', line 28

def mirror!(options = {})
  remote = options['mirror']['remote'] rescue 'upstream'
  run "git pull #{remote} #{branch} && git push origin #{branch}"
end

#run(command) ⇒ Object



37
38
39
40
# File 'lib/pullable/processor.rb', line 37

def run(command)
  puts "Updating:\t#{directory}"
  system "#{command} > /dev/null"
end

#update!(options = {}) ⇒ Object



33
34
35
# File 'lib/pullable/processor.rb', line 33

def update!(options = {})
  run "git fetch -p && #{command}"
end