Class: Kojo::Form

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

Overview

The Form class handles interactive ERBX templates

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Form

Returns a new instance of Form.



9
10
11
# File 'lib/kojo/form.rb', line 9

def initialize(file)
  @file = file
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/kojo/form.rb', line 36

def method_missing(method_name, *args, &block)
  if respond_to? method_name
    prompt.send method_name, *args, *block
  else
    super
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/kojo/form.rb', line 7

def file
  @file
end

Instance Method Details

#contentObject



13
14
15
# File 'lib/kojo/form.rb', line 13

def content
  @content ||= content!
end

#content!Object



17
18
19
20
21
22
# File 'lib/kojo/form.rb', line 17

def content!
  content = File.read file
  ruby_code = File.exist?("#{file}.rb") ? File.read("#{file}.rb") : nil
  content = "<%-\n#{ruby_code}\n-%>\n#{content}" if ruby_code
  content
end

#promptObject



32
33
34
# File 'lib/kojo/form.rb', line 32

def prompt
  @prompt ||= TTY::Prompt.new
end

#renderObject



24
25
26
27
28
29
30
# File 'lib/kojo/form.rb', line 24

def render
  ERBX.new(content).result(binding)
rescue TTY::Reader::InputInterrupt
  # :nocov:
  raise Kojo::Interrupt
  # :nocov:
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/kojo/form.rb', line 44

def respond_to?(method_name, include_private = false)
  prompt.respond_to?(method_name) || super
end

#select(*args, &block) ⇒ Object

Below are TTY::Prompt functions that are not captured by the ‘mthod_missing`, so we specify them explicitly



51
52
53
# File 'lib/kojo/form.rb', line 51

def select(*args, &block)
  prompt.select *args, &block
end

#warn(*args, &block) ⇒ Object



55
56
57
# File 'lib/kojo/form.rb', line 55

def warn(*args, &block)
  prompt.warn *args, &block
end