Class: Perron::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Adjacency, Associations, ClassMethods, Configuration, Core, Previewable, Publishable, ReadingTime, Scopes, Searchable, Sourceable, Sweeper, TableOfContent
Defined in:
lib/perron/resource.rb,
lib/perron/resource/core.rb,
lib/perron/resource/slug.rb,
lib/perron/resource/scopes.rb,
lib/perron/resource/sweeper.rb,
lib/perron/resource/metadata.rb,
lib/perron/resource/renderer.rb,
lib/perron/resource/adjacency.rb,
lib/perron/resource/separator.rb,
lib/perron/resource/searchable.rb,
lib/perron/resource/sourceable.rb,
lib/perron/resource/previewable.rb,
lib/perron/resource/publishable.rb,
lib/perron/resource/associations.rb,
lib/perron/resource/reading_time.rb,
lib/perron/resource/class_methods.rb,
lib/perron/resource/configuration.rb,
lib/perron/resource/table_of_content.rb

Defined Under Namespace

Modules: Adjacency, Associations, ClassMethods, Configuration, Core, Previewable, Publishable, ReadingTime, Renderer, Scopes, Searchable, Sourceable, Sweeper, TableOfContent Classes: Metadata, Separator, Slug

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TableOfContent

#table_of_content

Methods included from Sweeper

#extracted_headings, #sweeped_content

Methods included from Sourceable

#source, #source_backed?, #source_template

Methods included from ReadingTime

#estimated_reading_time

Methods included from Core

#association_value, #model_name, #persisted?, #to_model, #to_partial_path

Constructor Details

#initialize(file_path) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(file_path)
  @errors = ActiveModel::Errors.new(self)
  @file_path = file_path
  @id = generate_id

  raise Errors::FileNotFoundError, "No such file: #{file_path}" unless File.exist?(file_path)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



41
42
43
# File 'lib/perron/resource.rb', line 41

def file_path
  @file_path
end

#idObject (readonly)

Returns the value of attribute id.



41
42
43
# File 'lib/perron/resource.rb', line 41

def id
  @id
end

Instance Method Details

#collectionObject



90
# File 'lib/perron/resource.rb', line 90

def collection = Collection.new(self.class.model_name.collection)

#contentObject



78
79
80
81
82
83
84
# File 'lib/perron/resource.rb', line 78

def content
  page_content = Perron::Resource::Separator.new(raw_content).content

  return Perron::Resource::Renderer.erb(page_content, resource: self) if erb_processing?

  render_inline_erb using: page_content
end

#filenameObject



61
# File 'lib/perron/resource.rb', line 61

def filename = File.basename(@file_path)

#inline(layout: "application", **options) ⇒ Object



86
87
88
# File 'lib/perron/resource.rb', line 86

def inline(layout: "application", **options)
  {html: content, layout: layout}.merge(options)
end

#metadataObject



67
68
69
70
71
72
73
# File 'lib/perron/resource.rb', line 67

def 
  Perron::Resource::Metadata.new(
    resource: self,
    frontmatter: frontmatter,
    collection: collection
  ).data
end

#pluck(*attributes) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
# File 'lib/perron/resource.rb', line 51

def pluck(*attributes)
  raise ArgumentError, "wrong number of arguments (given 0, expected 1+)" if attributes.empty?

  if attributes.size == 1
    public_send(attributes.first)
  else
    attributes.map { |attr| public_send(attr) }
  end
end

#raw_contentObject Also known as: raw



75
# File 'lib/perron/resource.rb', line 75

def raw_content = File.read(@file_path)


92
# File 'lib/perron/resource.rb', line 92

def related_resources(limit: 5) = Perron::Site::Resource::Related.new(self).find(limit:)

#root?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/perron/resource.rb', line 95

def root?
  slug == "/"
end

#slugObject Also known as: path, to_param



63
# File 'lib/perron/resource.rb', line 63

def slug = Perron::Resource::Slug.new(self, frontmatter).create