Class: TextInjector

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TextInjector

Returns a new instance of TextInjector.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/text_injector.rb', line 2

def initialize(options={})
  @options = options

  @options[:file] ||= raise "required :file option missing"
  @identifier ||= (@options[:identifier] || default_identifier)
  @content ||= @options[:content]

  if @options[:update] && !File.exists?(@options[:file])
    warn("[fail] Can't find file: #{@options[:file]}")
    exit(1)
  end

  if @options[:update] && @options[:write]
    warn("[fail] Can't update AND write. choose one.")
    exit(1)
  end
end

Instance Method Details

#content(text = nil) ⇒ Object

both setter and getter for the dsl



21
22
23
# File 'lib/text_injector.rb', line 21

def content(text = nil)
  text.nil? ? @content : @content = text
end

#identifier(id = nil) ⇒ Object



24
25
26
# File 'lib/text_injector.rb', line 24

def identifier(id = nil)
  id.nil? ? @identifier : @identifier = id
end

#runObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/text_injector.rb', line 28

def run
  if @options[:update]
    write_file(updated_file)
  elsif @options[:write]
    write_file(marked_content)
  else
    puts marked_content
    exit
  end
end