Module: PlainRecord::Resource
- Includes:
- File
- Defined in:
- lib/plain_record/resource.rb
Overview
Module to be included into model class. Contain instance methods. See Model for class methods.
You can set your callbacks before and after some methods/events:
-
path(matchers)
– return file names for model which is match for matchers; -
load(enrty)
– load or create new entry; -
destroy(entry)
– delete entry; -
save(entry)
– write entry to file.
See PlainRecord::Callbacks for details.
You can define fields from entry file path, by in_filepath
filter. See PlainRecord::Filepath for details.
class Post
include PlainRecord::Resource
entry_in 'content/*/post.md'
before :save do |enrty|
entry.title = Time.now.to.s unless entry.title
end
virtual :name, in_filepath(1)
field :title
text :summary
text :content
end
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Fields values.
-
#file ⇒ Object
File, where this object is stored.
-
#texts ⇒ Object
readonly
Texts values.
Attributes included from File
Class Method Summary collapse
Instance Method Summary collapse
-
#data_recursive ⇒ Object
Return model data with plain hash of all child data.
-
#destroy ⇒ Object
Delete current entry and it file if there isn’t has any other entries.
-
#eql?(other) ⇒ Boolean
(also: #==)
Compare if its fields and texts are equal.
-
#initialize(file = nil, data = { }, texts = []) ⇒ Object
Create new model instance with YAML
data
andtexts
fromfile
. -
#path ⇒ Object
Return relative path to
file
fromPlainRecord.root
. -
#save ⇒ Object
Save entry to file.
Methods included from File
Instance Attribute Details
#data ⇒ Object (readonly)
Fields values.
59 60 61 |
# File 'lib/plain_record/resource.rb', line 59 def data @data end |
#file ⇒ Object
File, where this object is stored.
65 66 67 |
# File 'lib/plain_record/resource.rb', line 65 def file @file end |
#texts ⇒ Object (readonly)
Texts values.
62 63 64 |
# File 'lib/plain_record/resource.rb', line 62 def texts @texts end |
Class Method Details
.included(base) ⇒ Object
51 52 53 54 |
# File 'lib/plain_record/resource.rb', line 51 def self.included(base) base.send :extend, Model base.send :extend, PlainRecord::File::Model end |
Instance Method Details
#data_recursive ⇒ Object
Return model data with plain hash of all child data
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/plain_record/resource.rb', line 123 def data_recursive hash = { } @data.each_pair do |key, value| hash[key] = if value.respond_to? :data_recursive value.data_recursive else value end end hash end |
#destroy ⇒ Object
Delete current entry and it file if there isn’t has any other entries.
116 117 118 119 120 |
# File 'lib/plain_record/resource.rb', line 116 def destroy self.class.use_callbacks(:destroy, self) do self.class.delete_entry(@file, self) end end |
#eql?(other) ⇒ Boolean Also known as: ==
Compare if its fields and texts are equal.
136 137 138 139 |
# File 'lib/plain_record/resource.rb', line 136 def eql?(other) return false unless other.kind_of?(self.class) @file == other.file and @data == other.data and @texts == @texts end |
#initialize(file = nil, data = { }, texts = []) ⇒ Object
Create new model instance with YAML data
and texts
from file
.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/plain_record/resource.rb', line 68 def initialize(file = nil, data = { }, texts = []) self.class.use_callbacks(:load, self) do texts, data = data, nil if data.is_a? Array data, file = file, nil if file.is_a? Hash @file = file @data = data @texts = texts end end |
#path ⇒ Object
Return relative path to file
from PlainRecord.root
.
93 94 95 96 |
# File 'lib/plain_record/resource.rb', line 93 def path return nil unless @file @file.slice(PlainRecord.root.length..-1) end |
#save ⇒ Object
Save entry to file. Note, that for in_list models it also save all other entries in file.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/plain_record/resource.rb', line 100 def save self.class.use_callbacks(:save, self) do unless @file unless self.class.path =~ /[\*\[\?\{]/ self.file = self.class.path else raise ArgumentError, "There isn't file to save entry. Set filepath fields or file." end end self.class.save_file(@file) end end |