Class: Bridgetown::Model::Base
- Inherits:
-
Object
- Object
- Bridgetown::Model::Base
show all
- Includes:
- RodaCallable
- 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.
52
53
54
|
# File '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 '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
.find(id, site: Bridgetown::Current.site) ⇒ Object
9
10
11
12
13
14
|
# File '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 '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 '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
33
34
35
36
|
# File '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
88
89
90
|
# File 'lib/bridgetown-core/model/base.rb', line 88
def as_resource_in_collection
collection.add_resource_from_model(self)
end
|
#attributes ⇒ Object
126
127
128
|
# File 'lib/bridgetown-core/model/base.rb', line 126
def attributes
@attributes ||= HashWithDotAccess::Hash.new
end
|
#attributes=(new_attributes) ⇒ Object
130
131
132
|
# File 'lib/bridgetown-core/model/base.rb', line 130
def attributes=(new_attributes)
attributes.update new_attributes
end
|
#call ⇒ String
Converts this model to a resource and returns the full output
100
|
# File 'lib/bridgetown-core/model/base.rb', line 100
def call(*) = render_as_resource.output
|
109
110
111
|
# File 'lib/bridgetown-core/model/base.rb', line 109
def collection
attributes[:_collection_]
end
|
#collection=(new_collection) ⇒ Object
113
114
115
|
# File 'lib/bridgetown-core/model/base.rb', line 113
def collection=(new_collection)
attributes[:_collection_] = new_collection
end
|
#content ⇒ String
118
119
120
|
# File 'lib/bridgetown-core/model/base.rb', line 118
def content
attributes[:_content_]
end
|
#content=(new_content) ⇒ Object
122
123
124
|
# File 'lib/bridgetown-core/model/base.rb', line 122
def content=(new_content)
attributes[:_content_] = new_content
end
|
Strip out keys like origin, collection, etc.
136
137
138
|
# File 'lib/bridgetown-core/model/base.rb', line 136
def data_attributes
attributes.reject { |k| k.starts_with?("_") && k.ends_with?("_") }
end
|
#id ⇒ Object
56
57
58
|
# File 'lib/bridgetown-core/model/base.rb', line 56
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
|
61
62
63
|
# File 'lib/bridgetown-core/model/base.rb', line 61
def origin
attributes[:_origin_]
end
|
#origin=(new_origin) ⇒ Object
65
66
67
68
|
# File 'lib/bridgetown-core/model/base.rb', line 65
def origin=(new_origin)
attributes[:_id_] = new_origin.id
attributes[:_origin_] = new_origin
end
|
#persisted? ⇒ Boolean
70
71
72
|
# File 'lib/bridgetown-core/model/base.rb', line 70
def persisted?
(id && origin&.exists?) == true
end
|
93
94
95
|
# File '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
140
141
142
|
# File '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
|
#save ⇒ Object
74
75
76
77
78
79
80
|
# File '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
|
104
105
106
|
# File 'lib/bridgetown-core/model/base.rb', line 104
def site
origin.site
end
|