Class: Klipp::Creator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCreator

Returns a new instance of Creator.



18
19
20
# File 'lib/klipp/creator.rb', line 18

def initialize
  @tokens = Hash.new
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/klipp/creator.rb', line 4

def identifier
  @identifier
end

#tokensObject (readonly)

Returns the value of attribute tokens.



4
5
6
# File 'lib/klipp/creator.rb', line 4

def tokens
  @tokens
end

Class Method Details

.from_file(path) ⇒ Object



6
7
8
9
10
11
# File 'lib/klipp/creator.rb', line 6

def self.from_file(path)
  raise "Klippfile not found in directory #{File.dirname path}. Run `klipp prepare`." unless File.exists? path
  string = IO.read path
  creator = Klipp::Creator.new
  creator.eval_string(string, path)
end

.from_user_input(template_identifier, highline) ⇒ Object



13
14
15
16
# File 'lib/klipp/creator.rb', line 13

def self.from_user_input(template_identifier, highline)
  creator = Klipp::Creator.new
  creator.ask_user_input(template_identifier, highline)
end

Instance Method Details

#ask_user_input(identifier, highline = HighLine.new) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/klipp/creator.rb', line 31

def ask_user_input(identifier, highline = HighLine.new)
  @identifier = identifier
  spec_path = Template::Spec.spec_path_for_identifier(identifier)
  template = Template::Spec.from_file(spec_path)
  template.each do |name, token|
    self.tokens[name] = highline.ask("#{token.comment}?") { |q|
      q.validate = token.validation if token.validation
      q.responses[:not_valid] = token.validation_hint if token.validation_hint
    } unless token.hidden
  end
  validate
end

#create(template_identifier, &config) ⇒ Object



72
73
74
75
# File 'lib/klipp/creator.rb', line 72

def create(template_identifier, &config)
  @identifier = template_identifier
  config.yield(@tokens) if block_given?
end

#eval_string(string, path) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/klipp/creator.rb', line 22

def eval_string(string, path)
  begin
    eval(string, nil, path)
  rescue Exception => e
    raise "Error evaluating klippfile: #{File.basename(path)}: #{e.message}\n  #{e.backtrace.join("\n  ")}"
  end
  validate
end

#hint(message) ⇒ Object

Raises:



68
69
70
# File 'lib/klipp/creator.rb', line 68

def hint(message)
  raise Klipp::Hint.new(message)
end

#invalidate(message) ⇒ Object



64
65
66
# File 'lib/klipp/creator.rb', line 64

def invalidate(message)
  raise message
end

#validateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/klipp/creator.rb', line 44

def validate
  msg = 'Klippfile invalid: '
  invalidate msg+'missing name' unless @identifier && @identifier.length > 0
  matching_specs = Template::Spec.specs_matching_identifier(@identifier)

  case
    when matching_specs.count == 0
      msg += "Unknown template name `#{@identifier}`. "
      msg += "Run `klipp template list` to see your options."
      invalidate msg+msg
    when matching_specs.count > 1
      msg += "Found multiple templates named `#{@identifier}`. "
      msg += "Use a full template identifier to pick one. "
      msg += "Run `klipp template list` to see your options."
      hint msg+msg
    else
      self
  end
end