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)


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

def content
  @content
end

#front_matter_line_countInteger

Returns:

  • (Integer)


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

def front_matter_line_count
  @front_matter_line_count
end

Class Method Details

.data_file_extensionsObject



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

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

.handle_scheme?(scheme) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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)


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

def exists?
  File.exist?(original_path)
end

#original_pathObject



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

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

#readObject



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

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



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

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

#urlObject



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

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

#write(model) ⇒ Object



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

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