Class: Crudboy::Template

Inherits:
Object show all
Defined in:
lib/crudboy/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, base_path, context) ⇒ Template

Returns a new instance of Template.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/crudboy/template.rb', line 7

def initialize(path, base_path, context)
  @path = path
  @base_path = base_path.blank? ? '.' : base_path
  @type = if File.directory?(@path)
            :directory
          elsif @path.end_with?('.erb')
            :erb
          else
            :plain
          end
  @context = context
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



5
6
7
# File 'lib/crudboy/template.rb', line 5

def base_path
  @base_path
end

#contextObject

Returns the value of attribute context.



5
6
7
# File 'lib/crudboy/template.rb', line 5

def context
  @context
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/crudboy/template.rb', line 5

def path
  @path
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/crudboy/template.rb', line 5

def type
  @type
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/crudboy/template.rb', line 64

def directory?
  @type == :directory
end

#erb?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/crudboy/template.rb', line 68

def erb?
  @type == :erb
end

#make_base_directory!(destination) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/crudboy/template.rb', line 29

def make_base_directory!(destination)
  @context.eval(@base_path).tap do |path|
    File.dirname(path).tap do |base_dir|
      File.join(destination, base_dir).tap do |full_path|
        FileUtils.mkdir_p(full_path)
        puts "mkdir -p: #{full_path}"
      end
    end
  end
end

#make_directory!(destination) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/crudboy/template.rb', line 20

def make_directory!(destination)
  @context.eval(@base_path).tap do |path|
    File.join(destination, path).tap do |full_path|
      FileUtils.mkdir_p(full_path)
      puts "mkdir -p: #{full_path}"
    end
  end
end

#plain?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/crudboy/template.rb', line 72

def plain?
  @type == :plain
end

#render!(destination) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/crudboy/template.rb', line 50

def render!(destination)
  if directory?
    make_directory!(destination)
  else
    make_base_directory!(destination)
    render_file.tap do |file_content|
      File.join(destination, @context.eval(@base_path.delete_suffix('.erb'))).tap do |path|
        IO.write(path, file_content)
        puts "Write file: #{path}"
      end
    end
  end
end

#render_fileObject



40
41
42
43
44
45
46
47
48
# File 'lib/crudboy/template.rb', line 40

def render_file
  if erb?
    erb = ERB.new(IO.read(path))
    erb.filename = path
    erb.result(@context.binding)
  else
    IO.read(path)
  end
end