Module: PlainRecord::Model
- Includes:
- Associations, Callbacks, Default, Filepath, Type
- 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
-
#accessors_modules ⇒ Object
Named modules, created by
add_accessors
. -
#fields ⇒ Object
YAML fields names.
-
#loaded ⇒ Object
Content of already loaded files.
-
#storage ⇒ Object
readonly
Storage type:
:entry
or:list
. -
#texts ⇒ Object
Name of special fields with big text.
-
#virtuals ⇒ Object
Fields names with dynamic value.
Attributes included from Associations
Attributes included from Filepath
#filepath_fields, #filepath_regexp
Attributes included from Default
Attributes included from Callbacks
Class Method Summary collapse
Instance Method Summary collapse
-
#add_accessors(name = nil, file = nil, line = nil, code = nil) ⇒ Object
Create new anonymous module and include in model.
-
#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, init_association_cache, map
Methods included from Callbacks
#after, #before, #call_after_callbacks, #call_before_callbacks, #use_callbacks
Instance Attribute Details
#accessors_modules ⇒ Object
Named modules, created by add_accessors
.
53 54 55 |
# File 'lib/plain_record/model.rb', line 53 def accessors_modules @accessors_modules end |
#fields ⇒ Object
YAML fields names.
38 39 40 |
# File 'lib/plain_record/model.rb', line 38 def fields @fields end |
#loaded ⇒ Object
Content of already loaded files.
50 51 52 |
# File 'lib/plain_record/model.rb', line 50 def loaded @loaded end |
#storage ⇒ Object (readonly)
Storage type: :entry
or :list
.
47 48 49 |
# File 'lib/plain_record/model.rb', line 47 def storage @storage end |
#texts ⇒ Object
Name of special fields with big text.
41 42 43 |
# File 'lib/plain_record/model.rb', line 41 def texts @texts end |
#virtuals ⇒ Object
Fields names with dynamic value.
44 45 46 |
# File 'lib/plain_record/model.rb', line 44 def virtuals @virtuals end |
Class Method Details
.extended(base) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/plain_record/model.rb', line 55 def self.extended(base) base.fields = [] base.virtuals = [] base.texts = [] base.loaded = { } base.accessors_modules = { } end |
Instance Method Details
#add_accessors(name = nil, file = nil, line = nil, code = nil) ⇒ Object
Create new anonymous module and include in model.
You can set name
and it will old module, if it was created with same name.
It is helper to create model fields accessors and filters for it with super
support.
add_accessors <<-EOS, __FILE__, __LINE__
def #{name}
@data['#{name}']
end
EOS
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/plain_record/model.rb', line 160 def add_accessors(name = nil, file = nil, line = nil, code = nil) if name.is_a? String code = line line = file file = name name = nil end if file and code.nil? code = file file = line = nil end if name and @accessors_modules.has_key? name mod = @accessors_modules[name] else mod = Module.new if name @accessors_modules[name] = mod end include mod end if file and code mod.module_eval(file, line, code) elsif code mod.module_eval(code) end mod end |
#all(matchers = { }, &block) ⇒ Object
Return all entries, which is match for matchers
and return true on block
.
Matchers is a Hash with field 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 }
103 104 105 106 107 108 |
# File 'lib/plain_record/model.rb', line 103 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
.
78 |
# File 'lib/plain_record/model.rb', line 78 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
.
73 |
# File 'lib/plain_record/model.rb', line 73 def each_entry(matchers = { }); end |
#files(matchers = { }) ⇒ Object
Return all file list for models, which match for matchers
.
135 136 137 |
# File 'lib/plain_record/model.rb', line 135 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 field 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 }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/plain_record/model.rb', line 119 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
.
66 |
# File 'lib/plain_record/model.rb', line 66 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
.
83 |
# File 'lib/plain_record/model.rb', line 83 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
.
141 142 143 144 145 |
# File 'lib/plain_record/model.rb', line 141 def path(matchers = { }) use_callbacks(:path, matchers) do @path end end |
#save_file(file) ⇒ Object
Write all loaded entries to file
.
86 87 88 89 90 91 92 |
# File 'lib/plain_record/model.rb', line 86 def save_file(file) if @loaded.has_key? file ::File.open(file, 'w') do |io| io << entries_string(@loaded[file]).sub(/^---\s+/, '') end end end |