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

Attributes included from File

#unsaved_files

Class Method Summary collapse

Instance Method Summary collapse

Methods included from File

#field_filepath

Instance Attribute Details

#dataObject (readonly)

Fields values.



59
60
61
# File 'lib/plain_record/resource.rb', line 59

def data
  @data
end

#fileObject

File, where this object is stored.



65
66
67
# File 'lib/plain_record/resource.rb', line 65

def file
  @file
end

#textsObject (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

#destroyObject

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.

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/plain_record/resource.rb', line 128

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

#pathObject

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

#saveObject

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

#to_yaml(opts = { }) ⇒ Object

Return string of YAML representation of entry data.



123
124
125
# File 'lib/plain_record/resource.rb', line 123

def to_yaml(opts = { })
  @data.to_yaml(opts)
end