Class: Bridgetown::Model::RepoOrigin

Inherits:
Origin
  • Object
show all
Includes:
FrontMatter::Importer
Defined in:
bridgetown-core/lib/bridgetown-core/model/repo_origin.rb

Direct Known Subclasses

PluginOrigin

Constant Summary

Constants inherited from Origin

Origin::EAGER_LOAD_DESCENDANTS

Instance Attribute Summary collapse

Attributes inherited from Origin

#id, #site

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FrontMatter::Importer

included, #read_front_matter

Methods inherited from Origin

#initialize, #verify_model?

Constructor Details

This class inherits a constructor from Bridgetown::Model::Origin

Instance Attribute Details

#contentString

Returns:

  • (String)


10
11
12
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 10

def content
  @content
end

#front_matter_line_countInteger

Returns:

  • (Integer)


13
14
15
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 13

def front_matter_line_count
  @front_matter_line_count
end

Class Method Details

.data_file_extensionsObject



20
21
22
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 20

def data_file_extensions
  %w(.yaml .yml .json .csv .tsv .rb).freeze
end

.handle_scheme?(scheme) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 16

def handle_scheme?(scheme)
  scheme == "repo"
end

.new_with_collection_path(collection, relative_path, site: Bridgetown::Current.site) ⇒ Object

Initializes a new repo object using a collection and a relative source path. You’ll need to use this when you want to create and save a model to the source.

Parameters:

  • collection (Bridgetown::Collection, String, Symbol)

    either a collection label or Collection object

  • relative_path (Pathname, String)

    the source path of the file to save



30
31
32
33
34
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 30

def new_with_collection_path(collection, relative_path, site: Bridgetown::Current.site)
  collection = collection.label if collection.is_a?(Bridgetown::Collection)

  new("repo://#{collection}.collection/#{relative_path}", site:)
end

Instance Method Details

#collectionObject



83
84
85
86
87
88
89
90
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 83

def collection
  return @collection if @collection

  collection_name = url.host.ends_with?(".collection") ?
                      url.host.chomp(".collection") :
                      "pages"
  @collection = site.collections[collection_name]
end

#exists?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 96

def exists?
  File.exist?(original_path)
end

#original_pathObject



92
93
94
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 92

def original_path
  @original_path ||= relative_path.expand_path(site.source)
end

#readObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 37

def read
  begin
    @data = (in_data_collection? ? read_file_data : read_front_matter(original_path)) || {}
  rescue SyntaxError => e
    Bridgetown.logger.error "Error:",
                            "Ruby Exception in #{e.message}"
  rescue StandardError => e
    handle_read_error(e)
  end

  @data ||= {}
  @data[:_id_] = id
  @data[:_origin_] = self
  @data[:_collection_] = collection
  @data[:_content_] = content if content

  @data
end

#relative_pathObject



77
78
79
80
81
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 77

def relative_path
  @relative_path ||= Pathname.new(
    Addressable::URI.unescape(url.path.delete_prefix("/"))
  )
end

#urlObject



73
74
75
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 73

def url
  @url ||= URI.parse(id)
end

#write(model) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'bridgetown-core/lib/bridgetown-core/model/repo_origin.rb', line 56

def write(model)
  if File.exist?(original_path) && !Bridgetown::Utils.has_yaml_header?(original_path)
    raise Bridgetown::Errors::InvalidYAMLFrontMatterError,
          "Only existing files containing YAML front matter can be overwritten by the model"
  end

  contents = "#{front_matter_to_yaml(model)}---\n\n#{model.content}"

  # Create folders if necessary
  dir = File.dirname(original_path)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  File.write(original_path, contents)

  true
end