Class: Bridgetown::Model::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Model
Defined in:
lib/bridgetown-core/model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(attributes = {})
  run_callbacks :load do
    super
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



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

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!
    # 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}"}"
  nil
end

Class Method Details

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

Parameters:

  • builder (Bridgetown::Builder)


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

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: site
  ).read do
    data[:_collection_] = site.collections[collection_name]
    data
  end
  new(data)
end

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



13
14
15
16
17
18
# File 'lib/bridgetown-core/model/base.rb', line 13

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: site)
  klass_for_id(id, origin: origin).new(origin.read)
end

.klass_for_id(id, origin: nil) ⇒ Object



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

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

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/bridgetown-core/model/base.rb', line 20

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

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

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/bridgetown-core/model/base.rb', line 37

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



96
97
98
# File 'lib/bridgetown-core/model/base.rb', line 96

def as_resource_in_collection
  collection.add_resource_from_model(self)
end

#attributesObject



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

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

#collectionBridgetown::Collection



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

def collection
  attributes[:_collection_]
end

#collection=(new_collection) ⇒ Object



116
117
118
# File 'lib/bridgetown-core/model/base.rb', line 116

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

#contentString

Returns:

  • (String)


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

def content
  attributes[:_content_]
end

#content=(new_content) ⇒ Object



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

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

#data_attributesHashWithDotAccess::Hash

Strip out keys like origin, collection, etc.



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

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

#idObject



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

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

#inspectObject



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

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

#originBridgetown::Model::Origin



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

def origin
  attributes[:_origin_]
end

#origin=(new_origin) ⇒ Object



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#render_as_resourceBridgetown::Resource::Base



101
102
103
# File 'lib/bridgetown-core/model/base.rb', line 101

def render_as_resource
  to_resource.read!.transform!
end

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

Returns:

  • (Boolean)


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

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

#saveObject



80
81
82
83
84
85
86
87
88
# File 'lib/bridgetown-core/model/base.rb', line 80

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

  run_callbacks :save do
    origin.write(self)
  end
end

#siteBridgetown::Site

override if need be

Returns:



107
108
109
# File 'lib/bridgetown-core/model/base.rb', line 107

def site
  origin.site
end

#to_resourceBridgetown::Resource::Base



91
92
93
# File 'lib/bridgetown-core/model/base.rb', line 91

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