Class: Namaste::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/namaste/tag.rb

Defined Under Namespace

Modules: Dirtype

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory_or_file, tag = nil, value = nil) ⇒ Tag

Returns a new instance of Tag.

Parameters:

  • directory_or_file (Directory, File)
  • tag (String) (defaults to: nil)
  • value (String) (defaults to: nil)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/namaste/tag.rb', line 14

def initialize(directory_or_file, tag = nil, value = nil)
  directory_or_file = File.expand_path(directory_or_file)

  tag = Namaste::DUBLIN_KERNEL[tag] if tag.is_a? Symbol

  if File.directory?(directory_or_file)
    @dir = directory_or_file
    @tag = tag.to_s
    @value = value.to_s
    write!
  elsif File.file?(directory_or_file)
    @file = File.new(directory_or_file, "a+")
    @dir = File.dirname(@file.path)
  else
    raise "Unknown directory or file (#{directory_or_file.to_s})"
  end

  load_tag_extensions
end

Class Method Details

.filename(tag, value) ⇒ String

Create a filename for a namaste key/value pair

Returns:

  • (String)


5
6
7
8
9
# File 'lib/namaste/tag.rb', line 5

def self.filename tag, value
  n = "%s=%s" % [tag, self.elide(value)]
  n = n.slice(0...252) + "..." if n.length > 255
  n
end

Instance Method Details

#deleteObject

Delete this tag



60
61
62
63
64
# File 'lib/namaste/tag.rb', line 60

def delete
  @value = value # make sure to preserve the value after the tag is deleted
  File.delete(file.path)
  @file = nil
end

#tagString

Returns namaste tag.

Returns:

  • (String)

    namaste tag



48
49
50
# File 'lib/namaste/tag.rb', line 48

def tag
  @tag ||= File.basename(file.path).split("=").first
end

#tag=(tag) ⇒ Object

Parameters:

  • namaste (String)

    tag



53
54
55
56
57
# File 'lib/namaste/tag.rb', line 53

def tag=(tag)
  delete
  @tag = tag.to_s
  write!
end

#valueString

Returns namaste value.

Returns:

  • (String)

    namaste value



35
36
37
38
# File 'lib/namaste/tag.rb', line 35

def value
  file.rewind
  @value ||= file.read
end

#value=(value) ⇒ Object

Parameters:

  • value (String)


41
42
43
44
45
# File 'lib/namaste/tag.rb', line 41

def value=(value)
  delete
  @value = value.to_s
  write!
end