yarii-content-model 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +10 -0
- data/Gemfile +14 -0
- data/LICENSE +21 -0
- data/README.md +9 -0
- data/Rakefile +38 -0
- data/app/models/yarii/content_model.rb +265 -0
- data/app/models/yarii/datafile_model.rb +255 -0
- data/lib/yarii-content-model/version.rb +1 -1
- data/yarii-content-model.gemspec +25 -0
- metadata +9 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e14fc98612d9fbb29d8fae96d8a006ffaa8fde3e963280a4febaa116bd1cd031
|
4
|
+
data.tar.gz: c49b39d476a5495c57d624c18723491ba8ba5797578ac227e98a69e8ecdc294e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 376f680ef0f239f21d50f0b3f55c743e61b591d586eedaaff2e133c58bef844f7b34802818944eb16659f796c93036ee26ade1f349eafb67581f8e14609b3628
|
7
|
+
data.tar.gz: 6db67e209966477c0f07f973bdd0c0f6a5097511696d93e213cb086430fd42411eecc0f937d82de5bc2b0a20834513d113eb7878e0ee1b17edf354276f517d6c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in yarii_platform.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Whitefusion
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Yarii::ContentModel
|
2
|
+
|
3
|
+
Provides an ActiveRecord-like method of loading and saving static content files (from Jekyll, Hugo, etc.)
|
4
|
+
|
5
|
+
Extracted and enhanced based on running production code.
|
6
|
+
|
7
|
+
Brought to you by [Whitefusion](https://whitefusion.io)
|
8
|
+
|
9
|
+
(Still WIP — stay tuned for the official release!)
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'YariiContentModel'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
# Disabling for now
|
18
|
+
#APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
19
|
+
#load 'rails/tasks/engine.rake'
|
20
|
+
|
21
|
+
|
22
|
+
#load 'rails/tasks/statistics.rake'
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task default: :test
|
@@ -0,0 +1,265 @@
|
|
1
|
+
require 'safe_yaml'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
class Yarii::ContentModel
|
6
|
+
include ActiveModel::Model
|
7
|
+
extend ActiveModel::Callbacks
|
8
|
+
define_model_callbacks :save, :destroy
|
9
|
+
|
10
|
+
include Yarii::Serializers::YAML
|
11
|
+
extend Yarii::VariableDefinitions
|
12
|
+
extend Yarii::FilePathDefinitions
|
13
|
+
|
14
|
+
# override in ApplicationContentModel
|
15
|
+
class_attribute :base_path, instance_accessor: false, default: ""
|
16
|
+
|
17
|
+
# override in subclass model
|
18
|
+
class_attribute :folder_path, instance_accessor: false, default: ""
|
19
|
+
|
20
|
+
class_attribute :variable_names, instance_accessor: false, default: []
|
21
|
+
|
22
|
+
attr_accessor :file_path, :content
|
23
|
+
|
24
|
+
# code snippet from Jekyll
|
25
|
+
YAML_FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
|
26
|
+
CHECK_BASE_64_REGEXP = /^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/
|
27
|
+
PAGINATION_SIZE = 12
|
28
|
+
|
29
|
+
def self.find(file_path)
|
30
|
+
raise "Missing base path for the #{self.name} content model" if self.base_path.blank?
|
31
|
+
|
32
|
+
# decode Base64 path if necessary
|
33
|
+
if CHECK_BASE_64_REGEXP.match file_path
|
34
|
+
file_path = Base64::decode64 file_path
|
35
|
+
end
|
36
|
+
|
37
|
+
file_path = absolute_path(file_path)
|
38
|
+
|
39
|
+
new(file_path: file_path).tap do |content_model|
|
40
|
+
content_model.load_file_from_path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.absolute_path(path)
|
45
|
+
sanitize_filepath File.join(self.base_path, self.folder_path, path)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.sanitize_filepath(path)
|
49
|
+
if path.include? "../"
|
50
|
+
# TODO: output better error message ;)
|
51
|
+
raise "Nice try, Hacker Dude. You lose."
|
52
|
+
end
|
53
|
+
|
54
|
+
path
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.all(sorted: true, order_by: :posted_datetime, order_direction: :desc, subfolder: nil)
|
58
|
+
raise "Missing base path for the #{self.name} content model" if self.base_path.blank?
|
59
|
+
|
60
|
+
if self.folder_path.present?
|
61
|
+
# find all content files in the folder and any direct subfolders
|
62
|
+
glob_pattern = File.join(self.base_path, self.folder_path, subfolder.to_s, "**/*.{md,markdown,html}")
|
63
|
+
|
64
|
+
files = Dir.glob(glob_pattern)
|
65
|
+
else
|
66
|
+
# find any content files not in special underscore folders
|
67
|
+
files = Rake::FileList.new(
|
68
|
+
File.join(self.base_path, "**/*.md"),
|
69
|
+
File.join(self.base_path, "**/*.markdown"),
|
70
|
+
File.join(self.base_path, "**/*.html")
|
71
|
+
) do |fl|
|
72
|
+
basename = Pathname.new(self.base_path).basename.sub(/^_/, '')
|
73
|
+
fl.exclude(/#{basename}\/\_/)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
models = files.map do |file_path|
|
78
|
+
unless File.directory? file_path
|
79
|
+
new(file_path: file_path).tap do |content_model|
|
80
|
+
content_model.load_file_from_path
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end.compact
|
84
|
+
|
85
|
+
if sorted
|
86
|
+
models.sort_by! do |content_model|
|
87
|
+
content_model.send(order_by)
|
88
|
+
end
|
89
|
+
order_direction == :desc ? models.reverse : models
|
90
|
+
else
|
91
|
+
models
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def save
|
96
|
+
run_callbacks :save do
|
97
|
+
if file_path.blank?
|
98
|
+
self.file_path = generate_new_file_path
|
99
|
+
end
|
100
|
+
|
101
|
+
# Create folders if necessary
|
102
|
+
dir = File.dirname(file_path)
|
103
|
+
unless File.directory?(dir)
|
104
|
+
FileUtils.mkdir_p(dir)
|
105
|
+
end
|
106
|
+
|
107
|
+
File.open(file_path, 'w') do |f|
|
108
|
+
f.write(generate_file_output)
|
109
|
+
end
|
110
|
+
|
111
|
+
true
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def destroy
|
116
|
+
run_callbacks :destroy do
|
117
|
+
if persisted?
|
118
|
+
File.delete(file_path)
|
119
|
+
self.file_path = nil
|
120
|
+
|
121
|
+
true
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def id
|
127
|
+
if persisted?
|
128
|
+
Base64.encode64(relative_path).strip
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def relative_path
|
133
|
+
relative_base = File.join(self.class.base_path, self.class.folder_path)
|
134
|
+
file_path.sub(/^#{relative_base}/, '')
|
135
|
+
end
|
136
|
+
|
137
|
+
def persisted?
|
138
|
+
file_path.present? && File.exist?(file_path)
|
139
|
+
end
|
140
|
+
def new_record?
|
141
|
+
!persisted?
|
142
|
+
end
|
143
|
+
|
144
|
+
def file_name
|
145
|
+
file_path&.split('/')&.last
|
146
|
+
end
|
147
|
+
|
148
|
+
def file_stat
|
149
|
+
if persisted?
|
150
|
+
@file_stat ||= File.stat(file_path)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def posted_datetime
|
155
|
+
if variable_names.include?(:date) && date
|
156
|
+
date.to_datetime
|
157
|
+
elsif matched = file_name.to_s.match(/^[0-9]+-[0-9]+-[0-9]+/)
|
158
|
+
matched[0].to_datetime
|
159
|
+
elsif persisted?
|
160
|
+
file_stat.mtime
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def will_be_published?
|
165
|
+
return false if respond_to?(:published) and published === false
|
166
|
+
return false if respond_to?(:draft) and draft
|
167
|
+
true
|
168
|
+
end
|
169
|
+
|
170
|
+
def generate_new_file_path
|
171
|
+
# NOTE: this can totally be subclassed by various content models…
|
172
|
+
|
173
|
+
if use_date = date && date.to_datetime
|
174
|
+
date_prefix = "#{use_date.strftime('%Y-%m-%d')}-"
|
175
|
+
else
|
176
|
+
date_prefix = "#{Date.current.strftime('%Y-%m-%d')}-"
|
177
|
+
end
|
178
|
+
|
179
|
+
slug = if title
|
180
|
+
title.gsub(/['|"]/,'').parameterize
|
181
|
+
else
|
182
|
+
"untitled_#{self.class.name.parameterize}"
|
183
|
+
end
|
184
|
+
|
185
|
+
self.class.absolute_path("#{date_prefix}#{slug}.md")
|
186
|
+
end
|
187
|
+
|
188
|
+
def generate_file_output
|
189
|
+
# NOTE: this can totally be subclassed by various content models…
|
190
|
+
|
191
|
+
as_yaml + "---\n\n" + content.to_s
|
192
|
+
end
|
193
|
+
|
194
|
+
def variable_names
|
195
|
+
@variable_names ||= self.class.variable_names&.dup || []
|
196
|
+
end
|
197
|
+
|
198
|
+
def attributes
|
199
|
+
ret = {}
|
200
|
+
variable_names.each do |var_name|
|
201
|
+
ret[var_name.to_s] = send(var_name.to_sym)
|
202
|
+
end
|
203
|
+
ret
|
204
|
+
end
|
205
|
+
|
206
|
+
def assign_attributes(new_attributes)
|
207
|
+
# Changed from Active Model
|
208
|
+
# (we implement our own method of assigning attributes)
|
209
|
+
|
210
|
+
if !new_attributes.respond_to?(:stringify_keys)
|
211
|
+
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
|
212
|
+
end
|
213
|
+
return if new_attributes.empty?
|
214
|
+
|
215
|
+
attributes = new_attributes.stringify_keys
|
216
|
+
update_variables(sanitize_for_mass_assignment(attributes))
|
217
|
+
end
|
218
|
+
|
219
|
+
def update_variables(hash)
|
220
|
+
hash.each do |key, value|
|
221
|
+
begin
|
222
|
+
send("#{key}=", value)
|
223
|
+
rescue NoMethodError
|
224
|
+
Rails.logger.warn (":#{key}: is not a defined front matter variable, will be available as read-only")
|
225
|
+
|
226
|
+
# If an unfamiliar variable is present, allow it to be set as a
|
227
|
+
# read-only value for future serialization, but it still can't be set
|
228
|
+
# via an accessor writer. Kind ugly code, but it works
|
229
|
+
instance_variable_set("@#{key}", value)
|
230
|
+
unless key.to_sym.in? variable_names
|
231
|
+
variable_names << key.to_sym
|
232
|
+
define_singleton_method key.to_sym do
|
233
|
+
instance_variable_get("@#{key}")
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def load_file_from_path
|
241
|
+
file_data = File.read(file_path)
|
242
|
+
|
243
|
+
loaded_variables = {}
|
244
|
+
|
245
|
+
begin
|
246
|
+
if file_data =~ YAML_FRONT_MATTER_REGEXP
|
247
|
+
self.content = $'
|
248
|
+
loaded_variables = ::SafeYAML.load(Regexp.last_match(1))
|
249
|
+
end
|
250
|
+
rescue SyntaxError => e
|
251
|
+
Rails.logger.error "Error: YAML Exception reading #{file_path}: #{e.message}"
|
252
|
+
end
|
253
|
+
|
254
|
+
if loaded_variables.present?
|
255
|
+
update_variables(loaded_variables)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
def as_json(options={})
|
261
|
+
options[:except] = (options[:except] || []) + ["variable_names"]
|
262
|
+
super(options)
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
@@ -0,0 +1,255 @@
|
|
1
|
+
require 'safe_yaml'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
class Yarii::DatafileModel
|
6
|
+
include ActiveModel::Model
|
7
|
+
extend ActiveModel::Callbacks
|
8
|
+
define_model_callbacks :save, :destroy
|
9
|
+
|
10
|
+
include Yarii::Serializers::YAML
|
11
|
+
extend Yarii::VariableDefinitions
|
12
|
+
extend Yarii::FilePathDefinitions
|
13
|
+
|
14
|
+
# override in ApplicationContentModel
|
15
|
+
class_attribute :base_path, instance_accessor: false, default: ""
|
16
|
+
|
17
|
+
# override in subclass model
|
18
|
+
class_attribute :folder_path, instance_accessor: false, default: ""
|
19
|
+
|
20
|
+
class_attribute :variable_names, instance_accessor: false, default: []
|
21
|
+
|
22
|
+
attr_accessor :file_path, :key_path
|
23
|
+
|
24
|
+
# code snippet from Jekyll
|
25
|
+
CHECK_BASE_64_REGEXP = /^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/
|
26
|
+
PAGINATION_SIZE = 12
|
27
|
+
|
28
|
+
def self.find(file_path, key_path)
|
29
|
+
raise "Missing base path for the #{self.name} content model" if self.base_path.blank?
|
30
|
+
|
31
|
+
# decode Base64 path if necessary
|
32
|
+
if CHECK_BASE_64_REGEXP.match file_path
|
33
|
+
file_path = Base64::decode64 file_path
|
34
|
+
end
|
35
|
+
|
36
|
+
file_path = absolute_path(file_path)
|
37
|
+
|
38
|
+
new(file_path: file_path, key_path: key_path).tap do |content_model|
|
39
|
+
content_model.load_file_from_path
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.absolute_path(path)
|
44
|
+
sanitize_filepath File.join(self.base_path, self.folder_path, path)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.sanitize_filepath(path)
|
48
|
+
if path.include? "../"
|
49
|
+
# TODO: output better error message ;)
|
50
|
+
raise "Nice try, Hacker Dude. You lose."
|
51
|
+
end
|
52
|
+
|
53
|
+
path
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.all(file_path)
|
57
|
+
raise "Missing base path for the #{self.name} content model" if self.base_path.blank?
|
58
|
+
|
59
|
+
if CHECK_BASE_64_REGEXP.match file_path
|
60
|
+
file_path = Base64::decode64 file_path
|
61
|
+
end
|
62
|
+
|
63
|
+
file_path = absolute_path(file_path)
|
64
|
+
|
65
|
+
file_data = File.read(file_path)
|
66
|
+
|
67
|
+
loaded_variables = {}
|
68
|
+
|
69
|
+
yaml_data = nil
|
70
|
+
|
71
|
+
begin
|
72
|
+
yaml_data = ::SafeYAML.load(file_data)
|
73
|
+
end
|
74
|
+
|
75
|
+
if yaml_data.nil?
|
76
|
+
raise "YAML wasn't loadable"
|
77
|
+
end
|
78
|
+
|
79
|
+
models = []
|
80
|
+
|
81
|
+
if yaml_data.is_a? Array
|
82
|
+
yaml_data.each_with_index do |item, index|
|
83
|
+
models << new(file_path: file_path, key_path: index.to_s).tap do |content_model|
|
84
|
+
content_model.load_file_from_path
|
85
|
+
end
|
86
|
+
end
|
87
|
+
else
|
88
|
+
yaml_data = ActiveSupport::HashWithIndifferentAccess.new(yaml_data)
|
89
|
+
yaml_data.keys.each do |key|
|
90
|
+
models << new(file_path: file_path, key_path: key.to_s).tap do |content_model|
|
91
|
+
content_model.load_file_from_path
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
models
|
97
|
+
end
|
98
|
+
|
99
|
+
def save
|
100
|
+
run_callbacks :save do
|
101
|
+
if file_path.blank?
|
102
|
+
raise "Must supply a file path to save to"
|
103
|
+
end
|
104
|
+
|
105
|
+
file_data = File.read(file_path)
|
106
|
+
|
107
|
+
loaded_variables = {}
|
108
|
+
|
109
|
+
yaml_data = nil
|
110
|
+
|
111
|
+
begin
|
112
|
+
yaml_data = ::SafeYAML.load(file_data)
|
113
|
+
end
|
114
|
+
|
115
|
+
if yaml_data.nil?
|
116
|
+
raise "YAML wasn't loadable"
|
117
|
+
end
|
118
|
+
|
119
|
+
final_data = nil
|
120
|
+
|
121
|
+
if yaml_data.is_a? Array
|
122
|
+
yaml_data.set_keypath(key_path, as_yaml(as_hash: true))
|
123
|
+
final_data = yaml_data.to_yaml
|
124
|
+
else
|
125
|
+
yaml_data = ActiveSupport::HashWithIndifferentAccess.new(yaml_data)
|
126
|
+
yaml_data.set_keypath(key_path, as_yaml(as_hash: true))
|
127
|
+
final_data = yaml_data.to_hash.to_yaml
|
128
|
+
end
|
129
|
+
|
130
|
+
final_data.sub!(/^---$\n/,'') # strip off the leading dashes
|
131
|
+
|
132
|
+
File.open(file_path, 'w') do |f|
|
133
|
+
f.write(final_data)
|
134
|
+
end
|
135
|
+
|
136
|
+
# TODO: figure out a better way to hook in repo updates
|
137
|
+
# Yarii::Repository.current&.add(file_path_to_use)
|
138
|
+
|
139
|
+
true
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def destroy
|
144
|
+
# TODO: what does it mean to destroy a keypath?
|
145
|
+
raise "TBD"
|
146
|
+
run_callbacks :destroy do
|
147
|
+
if persisted?
|
148
|
+
File.delete(file_path)
|
149
|
+
# TODO: figure out a better way to hook in repo updates
|
150
|
+
# Yarii::Repository.current&.remove(file_path)
|
151
|
+
self.file_path = nil
|
152
|
+
|
153
|
+
true
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def id
|
159
|
+
if persisted?
|
160
|
+
relative_base = File.join(self.class.base_path, self.class.folder_path)
|
161
|
+
relative_path = file_path.sub(/^#{relative_base}/, '')
|
162
|
+
Base64.encode64(relative_path).strip
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def persisted?
|
167
|
+
file_path.present? && File.exist?(file_path)
|
168
|
+
end
|
169
|
+
def new_record?
|
170
|
+
!persisted?
|
171
|
+
end
|
172
|
+
|
173
|
+
def file_name
|
174
|
+
file_path&.split('/')&.last
|
175
|
+
end
|
176
|
+
|
177
|
+
def file_stat
|
178
|
+
if persisted?
|
179
|
+
@file_stat ||= File.stat(file_path)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def posted_datetime
|
184
|
+
if variable_names.include?(:date) && date
|
185
|
+
date.to_datetime
|
186
|
+
elsif persisted?
|
187
|
+
file_stat.mtime
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def variable_names
|
192
|
+
@variable_names ||= self.class.variable_names&.dup || []
|
193
|
+
end
|
194
|
+
|
195
|
+
def attributes
|
196
|
+
ret = {}
|
197
|
+
variable_names.each do |var_name|
|
198
|
+
ret[var_name.to_s] = nil
|
199
|
+
end
|
200
|
+
ret
|
201
|
+
end
|
202
|
+
|
203
|
+
def assign_attributes(new_attributes)
|
204
|
+
# Changed from Active Model
|
205
|
+
# (we implement our own method of assigning attributes)
|
206
|
+
|
207
|
+
if !new_attributes.respond_to?(:stringify_keys)
|
208
|
+
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
|
209
|
+
end
|
210
|
+
return if new_attributes.empty?
|
211
|
+
|
212
|
+
attributes = new_attributes.stringify_keys
|
213
|
+
update_variables(sanitize_for_mass_assignment(attributes))
|
214
|
+
end
|
215
|
+
|
216
|
+
def update_variables(hash)
|
217
|
+
hash.each do |key, value|
|
218
|
+
begin
|
219
|
+
send("#{key}=", value)
|
220
|
+
rescue NoMethodError
|
221
|
+
Rails.logger.warn (":#{key}: is not a defined front matter variable, will be available as read-only")
|
222
|
+
|
223
|
+
# If an unfamiliar variable is present, allow it to be set as a
|
224
|
+
# read-only value for future serialization, but it still can't be set
|
225
|
+
# via an accessor writer. Kind ugly code, but it works
|
226
|
+
instance_variable_set("@#{key}", value)
|
227
|
+
unless key.to_sym.in? variable_names
|
228
|
+
variable_names << key.to_sym
|
229
|
+
define_singleton_method key.to_sym do
|
230
|
+
instance_variable_get("@#{key}")
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def load_file_from_path
|
238
|
+
raise "Missing keypath" if key_path.nil?
|
239
|
+
file_data = File.read(file_path)
|
240
|
+
|
241
|
+
loaded_variables = {}
|
242
|
+
|
243
|
+
begin
|
244
|
+
yaml_data = ::SafeYAML.load(file_data)
|
245
|
+
loaded_variables = yaml_data.value_at_keypath(key_path)
|
246
|
+
rescue SyntaxError => e
|
247
|
+
Rails.logger.error "Error: YAML Exception reading #{file_path}: #{e.message}"
|
248
|
+
end
|
249
|
+
|
250
|
+
if loaded_variables.present?
|
251
|
+
update_variables(loaded_variables)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "yarii-content-model/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "yarii-content-model"
|
9
|
+
s.version = YariiContentModel::VERSION
|
10
|
+
s.authors = ["Jared White"]
|
11
|
+
s.email = ["jared@jaredwhite.com"]
|
12
|
+
s.homepage = "https://whitefusion.io"
|
13
|
+
s.summary = "Provides an ActiveRecord-like method of loading and saving static content files (from Bridgetown, Jekyll, etc.)"
|
14
|
+
s.description = s.summary
|
15
|
+
s.license = "MIT"
|
16
|
+
|
17
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) }
|
18
|
+
|
19
|
+
s.add_dependency "rails", ">= 5.0", "< 7.0"
|
20
|
+
s.add_dependency 'safe_yaml', '~> 1.0'
|
21
|
+
s.add_dependency 'key_path', '~> 1.2'
|
22
|
+
s.add_dependency 'git', '~> 1.3'
|
23
|
+
|
24
|
+
s.add_development_dependency "rspec-rails", "3.8.2"
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yarii-content-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared White
|
@@ -94,11 +94,19 @@ executables: []
|
|
94
94
|
extensions: []
|
95
95
|
extra_rdoc_files: []
|
96
96
|
files:
|
97
|
+
- ".gitignore"
|
98
|
+
- Gemfile
|
99
|
+
- LICENSE
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- app/models/yarii/content_model.rb
|
103
|
+
- app/models/yarii/datafile_model.rb
|
97
104
|
- lib/content_model_mixins.rb
|
98
105
|
- lib/tasks/yarii_content_model_tasks.rake
|
99
106
|
- lib/yarii-content-model.rb
|
100
107
|
- lib/yarii-content-model/engine.rb
|
101
108
|
- lib/yarii-content-model/version.rb
|
109
|
+
- yarii-content-model.gemspec
|
102
110
|
homepage: https://whitefusion.io
|
103
111
|
licenses:
|
104
112
|
- MIT
|