Module: Cupper::Entity

Included in:
CookbookFile, Recipe, Structure
Defined in:
lib/cupper/entity.rb

Overview

Entity represents a Entity

Constant Summary collapse

FILE =
'file'
DIR =
'dir'

Instance Method Summary collapse

Instance Method Details

#content(erb_file) ⇒ Object

Returns the content of the file



27
28
29
30
# File 'lib/cupper/entity.rb', line 27

def content(erb_file)
  return false if self.dir?
  @template = File.read("#{TEMPLATE_PATH}/#{erb_file}.erb")
end

#createObject

Create the actual file or dir in the correct place



21
22
23
24
# File 'lib/cupper/entity.rb', line 21

def create
  content(@erb_file)
  save
end

#dir?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/cupper/entity.rb', line 45

def dir?
  @type == DIR
end

#exist?Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/cupper/entity.rb', line 49

def exist?
  return File.exist?(@full_path) if self.file?
  return Dir.exist?(@full_path) if self.dir?
end

#file?Boolean

Treats entity as a file or as a dir

Returns:

  • (Boolean)


42
43
44
# File 'lib/cupper/entity.rb', line 42

def file?
  @type == FILE
end

#full_pathObject



54
55
56
# File 'lib/cupper/entity.rb', line 54

def full_path
  @full_path
end

#initialize(name, dest_path, erb_file = nil, type = nil, extension = '') ⇒ Object

As default the Entity is treated as a file



10
11
12
13
14
15
16
17
18
# File 'lib/cupper/entity.rb', line 10

def initialize(name, dest_path, erb_file = nil, type = nil, extension = '')
  @name = name
  @dest_path = dest_path
  @erb_file = erb_file
  @type = type.nil? ? FILE : type
  @ext = extension

  @full_path = "#{@dest_path}/#{@name}#{@ext}"
end

#render_templateObject



37
38
39
# File 'lib/cupper/entity.rb', line 37

def render_template
  ERB.new(@template, 0, '-').result(binding)
end

#saveObject



32
33
34
35
# File 'lib/cupper/entity.rb', line 32

def save
  File.open(@full_path,"w+") { |f| f.write(self.render_template) } if self.file?
  Dir.mkdir(@full_path) if self.dir? && !(self.exist?)
end