Module: PlainRecord::Resource
- 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 properties from entry file path, by in_filepath definer. 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)
property :title
text :summary
text :content
end
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Properties values.
-
#file ⇒ Object
File, where this object is stored.
-
#texts ⇒ Object
readonly
Texts values.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#destroy ⇒ Object
Delete current entry and it file if there isn’t has any other entries.
-
#eql?(other) ⇒ Boolean
(also: #==)
Compare if its properties and texts are equal.
-
#initialize(file = nil, data = { }, texts = []) ⇒ Object
Create new model instance with YAML
dataandtextsfromfile. -
#path ⇒ Object
Return relative path to
filefromPlainRecord.root. -
#save ⇒ Object
Save entry to file.
-
#to_yaml(opts = { }) ⇒ Object
Return string of YAML representation of entry
data.
Instance Attribute Details
#data ⇒ Object (readonly)
Properties values.
57 58 59 |
# File 'lib/plain_record/resource.rb', line 57 def data @data end |
#file ⇒ Object
File, where this object is stored.
63 64 65 |
# File 'lib/plain_record/resource.rb', line 63 def file @file end |
#texts ⇒ Object (readonly)
Texts values.
60 61 62 |
# File 'lib/plain_record/resource.rb', line 60 def texts @texts end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
51 52 53 |
# File 'lib/plain_record/resource.rb', line 51 def included(base) #:nodoc: base.send :extend, Model end |
Instance Method Details
#destroy ⇒ Object
Delete current entry and it file if there isn’t has any other entries.
114 115 116 117 118 |
# File 'lib/plain_record/resource.rb', line 114 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 properties and texts are equal.
126 127 128 129 |
# File 'lib/plain_record/resource.rb', line 126 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.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/plain_record/resource.rb', line 66 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.
91 92 93 94 |
# File 'lib/plain_record/resource.rb', line 91 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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/plain_record/resource.rb', line 98 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 properties or file." end end self.class.save_file(@file) end end |
#to_yaml(opts = { }) ⇒ Object
Return string of YAML representation of entry data.
121 122 123 |
# File 'lib/plain_record/resource.rb', line 121 def to_yaml(opts = { }) @data.to_yaml(opts) end |