Class: Textrepo::Repository

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/textrepo/repository.rb

Direct Known Subclasses

FileSystemRepository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Repository

Create a new repository. The argument must be an object which can be accessed like a Hash object.



24
25
26
27
# File 'lib/textrepo/repository.rb', line 24

def initialize(conf)
  @type = conf[:repository_type]
  @name = conf[:repository_name]
end

Instance Attribute Details

#nameObject (readonly)

Repository name. The usage of the value of ‘name` depends on a concrete repository class. For example, `FileSystemRepository` uses it as a part of the repository path.



18
19
20
# File 'lib/textrepo/repository.rb', line 18

def name
  @name
end

#typeObject (readonly)

Repository type. It specifies which concrete repository class will instantiated. For example, the type ‘:file_system` specifies `FileSystemRepository`.



11
12
13
# File 'lib/textrepo/repository.rb', line 11

def type
  @type
end

Instance Method Details

#create(timestamp, text) ⇒ Object

Stores text data into the repository with the specified timestamp. Returns the timestamp.

:call-seq:

create(Timestamp, Array) -> Timestamp


36
# File 'lib/textrepo/repository.rb', line 36

def create(timestamp, text); timestamp; end

#delete(timestamp) ⇒ Object

Deletes the content in the repository, which is associated to the timestamp. Returns an array which contains the deleted text.

:call-seq:

delete(Timestamp) -> Array


77
# File 'lib/textrepo/repository.rb', line 77

def delete(timestamp); []; end

#each(&block) ⇒ Object Also known as: each_pair

Calls the given block once for each pair of timestamp and text in self, passing those pair as parameter. Returns the repository itself.

If no block is given, an Enumerator is returned.



137
138
139
140
141
142
143
144
# File 'lib/textrepo/repository.rb', line 137

def each(&block)
  if block.nil?
    entries.lazy.map { |timestamp| pair(timestamp) }.to_enum(:each)
  else
    entries.each { |timestamp| yield pair(timestamp) }
    self
  end
end

#each_key(&block) ⇒ Object Also known as: each_timestamp

Calls the given block once for each timestamp in self, passing the timestamp as a parameter. Returns the repository itself.

If no block is given, an Enumerator is returned.



154
155
156
157
158
159
160
# File 'lib/textrepo/repository.rb', line 154

def each_key(&block)
  if block.nil?
    entries.to_enum(:each)
  else
    entries.each(&block)
  end
end

#each_value(&block) ⇒ Object Also known as: each_text

Calls the given block once for each timestamp in self, passing the text as a parameter. Returns the repository itself.

If no block is given, an Enumerator is returned.



170
171
172
173
174
175
176
# File 'lib/textrepo/repository.rb', line 170

def each_value(&block)
  if block.nil?
    entries.lazy.map { |timestamp| read(timestamp) }.to_enum(:each)
  else
    entries.each { |timestamp| yield read(timestamp) }
  end
end

#entries(stamp_pattern = nil) ⇒ Object

Finds all entries of text those have timestamps which mathes the specified pattern of timestamp. Returns an array which contains instances of Timestamp. If none of text was found, an empty array would be returned.

A pattern must be one of the following:

   - yyyymoddhhmiss_lll : whole stamp
   - yyyymoddhhmiss     : omit millisecond part
   - yyyymodd           : date part only
   - yyyymo             : month and year
   - yyyy               : year only
   - modd               : month and day

If ‘stamp_pattern` is omitted, the recent entries will be listed. Then, how many entries are listed depends on the implementaiton of the concrete repository class.

:call-seq:

entries(String) -> Array of Timestamp instances


101
# File 'lib/textrepo/repository.rb', line 101

def entries(stamp_pattern = nil); []; end

#exist?(timestamp) ⇒ Boolean

Check the existence of text which is associated with the given timestamp.

:call-seq:

exist?(Timestamp) -> true or false

Returns:

  • (Boolean)


110
# File 'lib/textrepo/repository.rb', line 110

def exist?(timestamp); false; end

#read(timestamp) ⇒ Object

Reads text data from the repository, which is associated to the timestamp. Returns an array which contains the text.

:call-seq:

read(Timestamp) -> Array


45
# File 'lib/textrepo/repository.rb', line 45

def read(timestamp); []; end

#search(pattern, stamp_pattern = nil) ⇒ Object

Searches a pattern (word or regular expression) in text those matches to a given timestamp pattern. Returns an Array of search results. If no match, returns an empty Array.

See the document for Repository#entries about a timestamp pattern. When nil is passed as a timestamp pattern, searching applies to all text in the repository.

Each entry of the result Array is constructed from 3 items, (1) timestamp (Timestamp), (2) line number (Integer), (3) matched line (String).

:call-seq:

search(String for pattern, String for Timestamp pattern) -> Array


128
# File 'lib/textrepo/repository.rb', line 128

def search(pattern, stamp_pattern = nil); []; end

#update(timestamp, text, keep_stamp = false) ⇒ Object

Updates the content with given text in the repository, which is associated to the given Timestamp object. Returns the Timestamp newly generated during the execution.

When true is passed as the third argument, keeps the Timestamp unchanged, though updates the content. Then, returns the given Timestamp object.

If the given Timestamp object is not existed as a Timestamp attached to text in the repository, raises MissingTimestampError.

If the given text is empty, raises EmptyTextError.

If the given text is identical to the text in the repository, does nothing. Returns the given timestamp itself.

:call-seq:

update(Timestamp, Array, true or false) -> Timestamp


68
# File 'lib/textrepo/repository.rb', line 68

def update(timestamp, text, keep_stamp = false); timestamp; end