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

#completed_draftsObject



36
37
38
39
40
41
# File 'lib/contraption/repository.rb', line 36

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



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

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

#draftsObject



24
25
26
# File 'lib/contraption/repository.rb', line 24

def drafts
  @drafts_source.list
end

#finalize_completed_drafts(link_handlers = []) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/contraption/repository.rb', line 43

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



28
29
30
# File 'lib/contraption/repository.rb', line 28

def posts
  Catalog.new( @posts_source.list.map{|p| Post.build(@posts_source.read p)} )
end

#static_filesObject



32
33
34
# File 'lib/contraption/repository.rb', line 32

def static_files
  @static_source.path
end

#valid_layout?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/contraption/repository.rb', line 20

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

#validateObject



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

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