Class: Contraption::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/contraption/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



9
10
11
12
13
14
# File 'lib/contraption/repository.rb', line 9

def initialize path
  @path = path
  validate
  construct_locations
  @formatter = Formatter.new @formats_source
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



7
8
9
# File 'lib/contraption/repository.rb', line 7

def formatter
  @formatter
end

Instance Method Details

#cloneObject



16
17
18
19
20
# File 'lib/contraption/repository.rb', line 16

def clone
  new_path = @path.cd "../.tmp_#{Time.now.to_i}"
  `cp -r #{@path.path}/* #{new_path.path}`
  Repository.new new_path
end

#completed_draftsObject



57
58
59
60
61
62
# File 'lib/contraption/repository.rb', line 57

def completed_drafts
  drafts.map{|draft| [draft, @drafts_source.read(draft)]}
        .each_with_object([]) do |draft, result|
          result << draft.first if Post.build(draft.last).new?
        end
end

#construct_locationsObject



72
73
74
75
76
77
# File 'lib/contraption/repository.rb', line 72

def construct_locations
  @drafts_source  = @path.cd("drafts")
  @posts_source   = @path.cd("posts")
  @formats_source = @path.cd("formats")
  @static_source  = @path.cd("static")
end

#destroyObject



22
23
24
# File 'lib/contraption/repository.rb', line 22

def destroy
  @path.destroy
end

#draftsObject



34
35
36
# File 'lib/contraption/repository.rb', line 34

def drafts
  @drafts_source.list
end

#finalize_completed_drafts(link_handlers = []) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/contraption/repository.rb', line 64

def finalize_completed_drafts link_handlers=[]
  completed_drafts.map{|draft| [draft, Post.build(@drafts_source.read(draft)).publish(link_handlers)]}
                  .each do |filename, post|
                    @posts_source.write post.publication_date.strftime("%Y%m%d-")+post.filename, @formatter.format(post, :raw)
                    @drafts_source.remove filename
                  end
end

#postsObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/contraption/repository.rb', line 38

def posts
  Catalog.new( @posts_source.list.map do |p|
    begin
      Post.build(@posts_source.read p)
    rescue ArgumentError => e
      STDERR.puts "Invalid post #{p}"
    end
  end.select{|p| p.title != ''}
  )
end

#static_file_pathObject



49
50
51
# File 'lib/contraption/repository.rb', line 49

def static_file_path
  @static_source.path
end

#static_filesObject



53
54
55
# File 'lib/contraption/repository.rb', line 53

def static_files
  @static_source.entries
end

#valid_layout?Boolean

Returns:



30
31
32
# File 'lib/contraption/repository.rb', line 30

def valid_layout?
  @path.exist? and @path.list.include? "drafts" and @path.list.include? "posts"
end

#validateObject



26
27
28
# File 'lib/contraption/repository.rb', line 26

def validate
  raise "Invalid source directory structure in #{@path}" unless valid_layout?
end