Class: Bridgetown::Model::Base
- Inherits:
-
Object
- Object
- Bridgetown::Model::Base
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!
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
.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
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
96
97
98
|
# File 'lib/bridgetown-core/model/base.rb', line 96
def as_resource_in_collection
collection.add_resource_from_model(self)
end
|
#attributes ⇒ Object
129
130
131
|
# File 'lib/bridgetown-core/model/base.rb', line 129
def attributes
@attributes ||= HashWithDotAccess::Hash.new
end
|
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
|
#content ⇒ 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
|
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
|
#id ⇒ Object
62
63
64
|
# File 'lib/bridgetown-core/model/base.rb', line 62
def id
attributes[:id] || attributes[:_id_]
end
|
#inspect ⇒ Object
159
160
161
|
# File 'lib/bridgetown-core/model/base.rb', line 159
def inspect
"#<#{self.class} #{data_attributes.inspect.delete_prefix("{").delete_suffix("}")}>"
end
|
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
76
77
78
|
# File 'lib/bridgetown-core/model/base.rb', line 76
def persisted?
(id && origin&.exists?) == true
end
|
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
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
|
#save ⇒ Object
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
|
107
108
109
|
# File 'lib/bridgetown-core/model/base.rb', line 107
def site
origin.site
end
|