Module: PlainRecord::Model
- Includes:
- Associations, Callbacks, Filepath
- Defined in:
- lib/plain_record/model.rb,
lib/plain_record/model/list.rb,
lib/plain_record/model/entry.rb
Overview
Static methods for model. Model class extend this class when Resource is included into it. See Resource for instance methods of model.
See also Model::Entry and Model::List for storage specific methods.
Defined Under Namespace
Instance Attribute Summary collapse
-
#loaded ⇒ Object
Content of already loaded files.
-
#properties ⇒ Object
YAML properties names.
-
#storage ⇒ Object
readonly
Storage type:
:entry
or:list
. -
#texts ⇒ Object
Name of special properties with big text.
-
#virtuals ⇒ Object
Properties names with dynamic value.
Attributes included from Associations
#association_cache, #association_maps
Attributes included from Filepath
#filepath_properties, #filepath_regexp
Attributes included from Callbacks
Class Method Summary collapse
-
.extended(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#all(matchers = { }, &block) ⇒ Object
Return all entries, which is match for
matchers
and return true onblock
. -
#delete_entry(file, entry = nil) ⇒ Object
Delete
entry
fromfile
. -
#each_entry(matchers = { }) ⇒ Object
Call block on all entry, which is may be match for
matchers
. -
#files(matchers = { }) ⇒ Object
Return all file list for models, which match for
matchers
. -
#first(matchers = { }, &block) ⇒ Object
Return first entry, which is match for
matchers
and return true onblock
. -
#load_file(file) ⇒ Object
Load and return all entries in
file
. -
#move_entry(entry, from, to) ⇒ Object
Move
entry
from one file to another. -
#path(matchers = { }) ⇒ Object
Return glob pattern to for files with entris, which is may be match for
matchers
. -
#save_file(file) ⇒ Object
Write all loaded entries to
file
.
Methods included from Associations
define_link_many, define_link_one, define_real_many, define_real_one, map
Methods included from Filepath
Methods included from Callbacks
#after, #before, #call_after_callbacks, #call_before_callbacks, #use_callbacks
Instance Attribute Details
#loaded ⇒ Object
Content of already loaded files.
47 48 49 |
# File 'lib/plain_record/model.rb', line 47 def loaded @loaded end |
#properties ⇒ Object
YAML properties names.
35 36 37 |
# File 'lib/plain_record/model.rb', line 35 def properties @properties end |
#storage ⇒ Object (readonly)
Storage type: :entry
or :list
.
44 45 46 |
# File 'lib/plain_record/model.rb', line 44 def storage @storage end |
#texts ⇒ Object
Name of special properties with big text.
38 39 40 |
# File 'lib/plain_record/model.rb', line 38 def texts @texts end |
#virtuals ⇒ Object
Properties names with dynamic value.
41 42 43 |
# File 'lib/plain_record/model.rb', line 41 def virtuals @virtuals end |
Class Method Details
.extended(base) ⇒ Object
:nodoc:
49 50 51 52 53 54 |
# File 'lib/plain_record/model.rb', line 49 def self.extended(base) #:nodoc: base.properties = [] base.virtuals = [] base.texts = [] base.loaded = { } end |
Instance Method Details
#all(matchers = { }, &block) ⇒ Object
Return all entries, which is match for matchers
and return true on block
.
Matchers is a Hash with property name in key and String or Regexp for match in value.
Post.all(title: 'Post title')
Post.all(title: /^Post/, summary: /cool/)
Post.all { |post| 20 < Post.content.length }
96 97 98 99 100 101 |
# File 'lib/plain_record/model.rb', line 96 def all(matchers = { }, &block) entries = all_entries(matchers) entries.delete_if { |i| not match(i, matchers) } if matchers entries.delete_if { |i| not block.call(i) } if block_given? entries end |
#delete_entry(file, entry = nil) ⇒ Object
Delete entry
from file
.
See method code in Model::Entry
or Model::List
.
71 |
# File 'lib/plain_record/model.rb', line 71 def delete_entry(file, entry = nil); end |
#each_entry(matchers = { }) ⇒ Object
Call block on all entry, which is may be match for matchers
. Unlike all.each
it use lazy file loading, so it is useful if you planing to break this loop somewhere in the middle (for example, like first
).
See method code in Model::Entry
or Model::List
.
66 |
# File 'lib/plain_record/model.rb', line 66 def each_entry(matchers = { }); end |
#files(matchers = { }) ⇒ Object
Return all file list for models, which match for matchers
.
128 129 130 |
# File 'lib/plain_record/model.rb', line 128 def files(matchers = { }) Dir.glob(PlainRecord.root(path(matchers))) end |
#first(matchers = { }, &block) ⇒ Object
Return first entry, which is match for matchers
and return true on block
.
Matchers is a Hash with property name in key and String or Regexp for match in value.
Post.first(title: 'Post title')
Post.first(title: /^Post/, summary: /cool/)
Post.first { |post| 2 < Post.title.length }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/plain_record/model.rb', line 112 def first(matchers = { }, &block) if matchers and block_given? each_entry(matchers) do |i| return i if match(i, matchers) and block.call(i) end elsif matchers each_entry(matchers) { |i| return i if match(i, matchers) } elsif block_given? each_entry { |i| return i if block.call(i) } else each_entry { |i| return i } end nil end |
#load_file(file) ⇒ Object
Load and return all entries in file
.
See method code in Model::Entry
or Model::List
.
59 |
# File 'lib/plain_record/model.rb', line 59 def load_file(file); end |
#move_entry(entry, from, to) ⇒ Object
Move entry
from one file to another.
See method code in Model::Entry
or Model::List
.
76 |
# File 'lib/plain_record/model.rb', line 76 def move_entry(entry, from, to); end |
#path(matchers = { }) ⇒ Object
Return glob pattern to for files with entris, which is may be match for matchers
.
134 135 136 137 138 |
# File 'lib/plain_record/model.rb', line 134 def path(matchers = { }) use_callbacks(:path, matchers) do @path end end |
#save_file(file) ⇒ Object
Write all loaded entries to file
.
79 80 81 82 83 84 85 |
# File 'lib/plain_record/model.rb', line 79 def save_file(file) if @loaded.has_key? file File.open(file, 'w') do |io| io.write entries_string(@loaded[file]).slice(5..-1) end end end |