Class: Bridgetown::Model::Base

Inherits:
Object
  • Object
show all
Includes:
RodaCallable
Defined in:
bridgetown-core/lib/bridgetown-core/model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RodaCallable

===

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



52
53
54
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 52

def initialize(attributes = {})
  self.attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 144

def method_missing(method_name, *args)
  return attributes[method_name] if attributes.key?(method_name)

  key = method_name.to_s
  if key.end_with?("=")
    key.chop!
    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}"}"
  nil
end

Class Method Details

.build(builder, collection_name, path, data) ⇒ Object

Parameters:



39
40
41
42
43
44
45
46
47
48
49
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 39

def build(builder, collection_name, path, data)
  site = builder.site
  data = Bridgetown::Model::BuilderOrigin.new(
    Bridgetown::Model::BuilderOrigin.id_for_builder_path(builder, path),
    site:
  ).read do
    data[:_collection_] = site.collections[collection_name]
    data
  end
  new(data)
end

.find(id, site: Bridgetown::Current.site) ⇒ Object



9
10
11
12
13
14
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 9

def find(id, site: Bridgetown::Current.site)
  raise "A Bridgetown site must be initialized and added to Current" unless site

  origin = origin_for_id(id, site:)
  klass_for_id(id, origin:).new(origin.read)
end

.klass_for_id(id, origin: nil) ⇒ Object



27
28
29
30
31
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 27

def klass_for_id(id, origin: nil)
  Bridgetown::Model::Base.descendants.find do |klass|
    klass.will_load_id?(id, origin:)
  end || Bridgetown::Model::Base
end

.origin_for_id(id, site: Bridgetown::Current.site) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 16

def origin_for_id(id, site: Bridgetown::Current.site)
  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, site:)
end

.will_load_id?(id, origin: nil) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 33

def will_load_id?(id, origin: nil)
  origin ||= origin_for_id(id)
  origin.verify_model?(self)
end

Instance Method Details

#as_resource_in_collectionBridgetown::Resource::Base



88
89
90
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 88

def as_resource_in_collection
  collection.add_resource_from_model(self)
end

#attributesObject



126
127
128
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 126

def attributes
  @attributes ||= HashWithDotAccess::Hash.new
end

#attributes=(new_attributes) ⇒ Object



130
131
132
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 130

def attributes=(new_attributes)
  attributes.update new_attributes
end

#callString

Converts this model to a resource and returns the full output

Returns:

  • (String)


100
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 100

def call(*) = render_as_resource.output

#collectionBridgetown::Collection



109
110
111
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 109

def collection
  attributes[:_collection_]
end

#collection=(new_collection) ⇒ Object



113
114
115
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 113

def collection=(new_collection)
  attributes[:_collection_] = new_collection
end

#contentString

Returns:

  • (String)


118
119
120
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 118

def content
  attributes[:_content_]
end

#content=(new_content) ⇒ Object



122
123
124
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 122

def content=(new_content)
  attributes[:_content_] = new_content
end

#data_attributesHashWithDotAccess::Hash

Strip out keys like origin, collection, etc.



136
137
138
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 136

def data_attributes
  attributes.reject { |k| k.starts_with?("_") && k.ends_with?("_") }
end

#idObject



56
57
58
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 56

def id
  attributes[:id] || attributes[:_id_]
end

#inspectObject



159
160
161
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 159

def inspect
  "#<#{self.class} #{data_attributes.inspect.delete_prefix("{").delete_suffix("}")}>"
end

#originBridgetown::Model::Origin



61
62
63
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 61

def origin
  attributes[:_origin_]
end

#origin=(new_origin) ⇒ Object



65
66
67
68
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 65

def origin=(new_origin)
  attributes[:_id_] = new_origin.id
  attributes[:_origin_] = new_origin
end

#persisted?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 70

def persisted?
  (id && origin&.exists?) == true
end

#render_as_resourceBridgetown::Resource::Base



93
94
95
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 93

def render_as_resource
  to_resource.read!.transform!
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 140

def respond_to_missing?(method_name, include_private = false)
  attributes.key?(method_name) || method_name.to_s.end_with?("=") || super
end

#saveObject



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

def save
  unless origin.respond_to?(:write)
    raise "`#{origin.class}' doesn't allow writing of model objects"
  end

  origin.write(self)
end

#siteBridgetown::Site

override if need be

Returns:



104
105
106
# File 'bridgetown-core/lib/bridgetown-core/model/base.rb', line 104

def site
  origin.site
end

#to_resourceBridgetown::Resource::Base



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

def to_resource
  Bridgetown::Resource::Base.new(model: self)
end