Class: Bridgetown::Model::RepoOrigin

Inherits:
Origin
  • Object
show all
Includes:
FrontMatterImporter, Utils::RubyFrontMatterDSL
Defined in:
lib/bridgetown-core/model/repo_origin.rb

Direct Known Subclasses

PluginOrigin

Constant Summary collapse

YAML_FRONT_MATTER_REGEXP =
%r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
RUBY_FRONT_MATTER_HEADER =
%r!\A[~`#-]{3,}(?:ruby|<%|{%)\s*\n!.freeze
RUBY_FRONT_MATTER_REGEXP =
%r!#{RUBY_FRONT_MATTER_HEADER.source}(.*?\n?)^((?:%>|%})?[~`#-]{3,}\s*$\n?)!m.freeze

Constants included from FrontMatterImporter

FrontMatterImporter::RUBY_BLOCK, FrontMatterImporter::RUBY_HEADER, FrontMatterImporter::YAML_BLOCK, FrontMatterImporter::YAML_HEADER

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 Utils::RubyFrontMatterDSL

#front_matter

Methods included from FrontMatterImporter

included, #process_ruby_data, #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)


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

def content
  @content
end

#front_matter_line_countInteger

Returns:

  • (Integer)


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

def front_matter_line_count
  @front_matter_line_count
end

Class Method Details

.data_file_extensionsObject



25
26
27
# File 'lib/bridgetown-core/model/repo_origin.rb', line 25

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

.handle_scheme?(scheme) ⇒ Boolean

Returns:

  • (Boolean)


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

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



35
36
37
38
39
# File 'lib/bridgetown-core/model/repo_origin.rb', line 35

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: site)
end

Instance Method Details

#collectionObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/bridgetown-core/model/repo_origin.rb', line 88

def collection
  return @collection if @collection

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

#exists?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/bridgetown-core/model/repo_origin.rb', line 103

def exists?
  File.exist?(original_path)
end

#original_pathObject



99
100
101
# File 'lib/bridgetown-core/model/repo_origin.rb', line 99

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

#readObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bridgetown-core/model/repo_origin.rb', line 42

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



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

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

#urlObject



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

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

#write(model) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bridgetown-core/model/repo_origin.rb', line 61

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