Class: Copypasta::Contents

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_directory) ⇒ Contents

Returns a new instance of Contents.



11
12
13
14
15
# File 'lib/copypasta/contents.rb', line 11

def initialize(target_directory)
  @target_directory = target_directory.dup.freeze
  @force_create = false
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



9
10
11
# File 'lib/copypasta/contents.rb', line 9

def entries
  @entries
end

#force_createObject

Returns the value of attribute force_create.



8
9
10
# File 'lib/copypasta/contents.rb', line 8

def force_create
  @force_create
end

#target_directoryObject (readonly)

Returns the value of attribute target_directory.



7
8
9
# File 'lib/copypasta/contents.rb', line 7

def target_directory
  @target_directory
end

Class Method Details

.from_tree(root) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/copypasta/contents.rb', line 33

def self.from_tree(root)
  require "copypasta/contents_dsl"

  raise "#{root} doesn't exist." unless Dir.exist?(root)
  root = File.expand_path(root)

  items = []

  Dir["#{root}/**/_contents.rb"].each do |f|
    f = File.expand_path(f)
    content_dir = File.dirname(f)
    target_directory = content_dir.sub(root, "").sub(%r!^/!, "")

    contents = Copypasta::Contents.new(target_directory)

    dsl = Copypasta::ContentsDSL.new(contents, content_dir)
    dsl.instance_eval File.read(f), f

    items << contents unless contents.entries.empty?
  end

  items
end

Instance Method Details

#apply(root, parameters) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/copypasta/contents.rb', line 17

def apply(root, parameters)
  content_dir = "#{root}/#{target_directory}"

  if should_create?
    FileUtils.mkdir_p content_dir
  end

  entries.each do |entry|
    entry.apply(content_dir, parameters)
  end
end

#should_create?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/copypasta/contents.rb', line 29

def should_create?
  force_create || !entries.empty?
end