Class: Marko::Markup::Storage

Inherits:
Storage
  • Object
show all
Includes:
FileUtils
Defined in:
lib/marko/markup/storage.rb

Overview

File Storage

Constant Summary collapse

Failure =
Class.new(StandardError)

Instance Method Summary collapse

Methods included from Pluggable

#plug

Instance Method Details

#artifactObject

See Also:

  • Strorage#artifact


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/marko/markup/storage.rb', line 76

def artifact
  artf = NULLFACT
  head, body = Psych.dump(artf).lines.then{|ln|
    [ln.first, ln.drop(1).join]
  }

  unless File.exist?(ARTIFACT)
    File.write(ARTIFACT, body)
    return artf
  end

  body = File.read(ARTIFACT)
  obj = Psych.load(head + body, freeze: true,
    permitted_classes: [Symbol, Marko::Artifact])
  obj.is_a?(Artifact) ? obj : artf
end

#content(source) ⇒ Object

See Also:



58
59
60
61
# File 'lib/marko/markup/storage.rb', line 58

def content(source)
  marko_home!
  File.read(source)
end

#furnish(directory) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/marko/markup/storage.rb', line 22

def furnish(directory)
  Dir.chdir(directory) {
    marko_directories.each{|dir| Dir.mkdir dir }
    src = File.join(Marko.root, 'lib', 'assets', 'init', '.')
    cp_r src, Dir.pwd
    artifact
  }
end

#marko_home?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/marko/markup/storage.rb', line 71

def marko_home?
  marko_directories.all?{ Dir.exist? _1 }
end

#punch(storage) ⇒ Object

See Also:

  • Storage#create


16
17
18
19
20
# File 'lib/marko/markup/storage.rb', line 16

def punch(storage)
  fail Failure, "Directory already exists" if Dir.exist?(storage)
  Dir.mkdir(storage)
  furnish(storage)
end

#punch_demoObject

create demo project



32
33
34
35
36
37
38
39
# File 'lib/marko/markup/storage.rb', line 32

def punch_demo
  return if Dir.exist?(DEMO)
  mkdir_p DEMO
  furnish DEMO
  demo = File.join(Marko.demo, '.')
  cp_r demo, DEMO
  Dir.glob("#{DEMO}/**/*", File::FNM_DOTMATCH).tap(&:shift)
end

#punch_samplesObject

create samples dir



42
43
44
45
46
47
48
# File 'lib/marko/markup/storage.rb', line 42

def punch_samples
  return if Dir.exist?(SAMPLES)
  mkdir_p SAMPLES
  samples = File.join(Marko.samples, '.')
  cp_r samples, SAMPLES
  Dir.glob("#{SAMPLES}/**/*", File::FNM_DOTMATCH).tap(&:shift)
end

#sourcesObject

See Also:



51
52
53
54
55
# File 'lib/marko/markup/storage.rb', line 51

def sources
  marko_home!
  ptrn = File.join(SOURCE, '**', '*.md')
  Dir.glob ptrn
end

#write(filename, content = '') ⇒ Object



63
64
65
66
67
68
69
# File 'lib/marko/markup/storage.rb', line 63

def write(filename, content = '')
  backup(filename)
  File.open(filename, 'w') do |f|
    f.puts(content) unless content.empty?
    yield(f) if block_given?
  end
end