class Bridgetown::Model::Base

Public Class Methods

build(collection_name, path, data) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 49
def build(collection_name, path, data)
  data = Bridgetown::Model::BuilderOrigin.new("builder://#{path}").read do
    data[:_collection_] = Bridgetown::Current.site.collections[collection_name]
    data
  end
  new(data)
end
find(id) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 18
def self.find(id)
  unless Bridgetown::Current.site
    raise "A Bridgetown site must be initialized and added to Current"
  end

  model_klass = klass_for_id(id)
  model_klass.new(read_data_for_id(id))
end
klass_for_id(id) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 27
def self.klass_for_id(id)
  descendants.find do |klass|
    klass.loads_id?(id)
  end || self
end
loads_id?(id) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 12
def self.loads_id?(id)
  name == ActiveSupport::Inflector.classify(
    URI.parse(id).host.chomp(".collection")
  )
end
new(attributes = {}) click to toggle source
Calls superclass method
# File lib/bridgetown-core/model/base.rb, line 58
def initialize(attributes = {})
  run_callbacks :load do
    super
  end
end
origin_for_id(id) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 37
def self.origin_for_id(id)
  scheme = URI.parse(id).scheme
  origin_klass = Origin.descendants.find do |klass|
    klass.handle_scheme?(scheme)
  end

  raise "No origin could be found for #{id}" unless origin_klass

  origin_klass.new(id)
end
read_data_for_id(id) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 33
def self.read_data_for_id(id)
  origin_for_id(id).read
end

Public Instance Methods

as_resource_in_collection() click to toggle source
# File lib/bridgetown-core/model/base.rb, line 82
def as_resource_in_collection
  collection.resources << to_resource.read!
  collection.resources.last
end
attributes() click to toggle source
# File lib/bridgetown-core/model/base.rb, line 103
def attributes
  @attributes ||= HashWithDotAccess::Hash.new
end
collection() click to toggle source

@return [Bridgetown::Collection]

# File lib/bridgetown-core/model/base.rb, line 94
def collection
  attributes[:_collection_]
end
content() click to toggle source

@return [String]

# File lib/bridgetown-core/model/base.rb, line 99
def content
  attributes[:_content_]
end
data_attributes() click to toggle source

Strip out keys like origin, collection, etc. @return [HashWithDotAccess::Hash]

# File lib/bridgetown-core/model/base.rb, line 109
def data_attributes
  attributes.reject { |k| k.starts_with?("_") && k.ends_with?("_") }
end
id() click to toggle source
# File lib/bridgetown-core/model/base.rb, line 64
def id
  attributes[:id] || attributes[:_id_]
end
inspect() click to toggle source
# File lib/bridgetown-core/model/base.rb, line 133
def inspect
  "#<#{self.class} #{data_attributes.inspect.delete_prefix("{").delete_suffix("}")}>"
end
method_missing(method_name, *args) click to toggle source
# File lib/bridgetown-core/model/base.rb, line 117
def method_missing(method_name, *args) # rubocop:disable Style/MethodMissingSuper
  return attributes[method_name] if attributes.key?(method_name)

  key = method_name.to_s
  if key.end_with?("=")
    key.chop!
    # attribute_will_change!(key)
    attributes[key] = args.first
    return attributes[key]
  end

  Bridgetown.logger.warn "key `#{method_name}' not found in attributes for" \
                         " #{attributes[:id].presence || ("new " + self.class.to_s)}"
  nil
end
origin() click to toggle source

@return [Bridgetown::Model::Origin]

# File lib/bridgetown-core/model/base.rb, line 69
def origin
  attributes[:_origin_]
end
persisted?() click to toggle source
# File lib/bridgetown-core/model/base.rb, line 73
def persisted?
  id && origin.exists?
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/bridgetown-core/model/base.rb, line 113
def respond_to_missing?(method_name, include_private = false)
  attributes.key?(method_name) || method_name.to_s.end_with?("=") || super
end
site() click to toggle source

override if need be @return [Bridgetown::Site]

# File lib/bridgetown-core/model/base.rb, line 89
def site
  Bridgetown::Current.site
end
to_resource() click to toggle source

@return [Bridgetown::Resource::Base]

# File lib/bridgetown-core/model/base.rb, line 78
def to_resource
  Bridgetown::Resource::Base.new(model: self)
end