Class: Bridgetown::Model::BuilderOrigin

Inherits:
Origin
  • Object
show all
Defined in:
lib/bridgetown-core/model/builder_origin.rb

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 inherited from Origin

#verify_model?

Constructor Details

#initialize(id, site: Bridgetown::Current.site) ⇒ BuilderOrigin

Returns a new instance of BuilderOrigin.



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

def initialize(id, site: Bridgetown::Current.site)
  super
  @relative_path = Pathname.new(url.path.delete_prefix("/"))
end

Instance Attribute Details

#relative_pathPathname (readonly)

Returns:

  • (Pathname)


7
8
9
# File 'lib/bridgetown-core/model/builder_origin.rb', line 7

def relative_path
  @relative_path
end

Class Method Details

.handle_scheme?(scheme) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.id_for_builder_path(builder, path) ⇒ Object



14
15
16
# File 'lib/bridgetown-core/model/builder_origin.rb', line 14

def id_for_builder_path(builder, path)
  "builder://#{builder.class.name.gsub("::", ".")}/#{path}"
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


41
# File 'lib/bridgetown-core/model/builder_origin.rb', line 41

def exists? = false

#front_matter_line_countObject



37
# File 'lib/bridgetown-core/model/builder_origin.rb', line 37

def front_matter_line_count = @data[:_front_matter_line_count_]

#original_pathObject



39
# File 'lib/bridgetown-core/model/builder_origin.rb', line 39

def original_path = @data[:_original_path_] || relative_path

#readObject



28
29
30
31
32
33
34
35
# File 'lib/bridgetown-core/model/builder_origin.rb', line 28

def read
  @data = block_given? ? yield : read_data_from_builder
  @data[:_id_] = id
  @data[:_origin_] = self
  @relative_path = Pathname.new(@data[:_relative_path_]) if @data[:_relative_path_]

  @data
end

#read_data_from_builderObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bridgetown-core/model/builder_origin.rb', line 43

def read_data_from_builder
  builder = Kernel.const_get(url.host.gsub(".", "::"))
  raise NameError unless builder.instance_methods.include?(:resource_data_for_id)

  builder.new.resource_data_for_id(id) || raise(NameError)
rescue NameError
  raise(
    Bridgetown::Errors::FatalException,
    "Builder not found which can read #{id}"
  )
end

#urlObject



24
25
26
# File 'lib/bridgetown-core/model/builder_origin.rb', line 24

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