class Namaste::Tag

Public Class Methods

filename(tag, value) click to toggle source

Create a filename for a namaste key/value pair @return [String]

# 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
new(directory_or_file, tag = nil, value = nil) click to toggle source

@param [Directory, File] directory_or_file @param [String] tag @param [String] value

# 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

Protected Class Methods

elide(value) click to toggle source

transliterate and truncate the value @param [String] value @return [String] ASCII string

# File lib/namaste/tag.rb, line 93
def self.elide value
  value = I18n.transliterate value
  value.gsub!(/[^A-Za-z0-9\-\._]+/, '_')
  value.gsub!(/_{2,}/, '_')
  value.gsub!(/^_|_$/, '_')
  encoded_value = value.downcase
end

Public Instance Methods

delete() click to toggle source

Delete this tag

# 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
tag() click to toggle source

@return [String] namaste tag

# File lib/namaste/tag.rb, line 48
def tag
  @tag ||= File.basename(file.path).split("=").first
end
tag=(tag) click to toggle source

@param [String] namaste tag

# File lib/namaste/tag.rb, line 53
def tag=(tag)
  delete
  @tag = tag.to_s
  write!
end
value() click to toggle source

@return [String] namaste value

# File lib/namaste/tag.rb, line 35
def value
  file.rewind
  @value ||= file.read
end
value=(value) click to toggle source

@param [String] value

# File lib/namaste/tag.rb, line 41
def value=(value)
  delete
  @value = value.to_s
  write!
end

Private Instance Methods

elide() click to toggle source

transliterate and truncate the namaste value

# File lib/namaste/tag.rb, line 85
def elide
  self.class.elide(@value)
end
file() click to toggle source
# File lib/namaste/tag.rb, line 67
def file
  @file ||= File.new(File.join(@dir, filename), "a+")
end
filename() click to toggle source
# File lib/namaste/tag.rb, line 71
def filename
  self.class.filename(@tag, @value)
end
load_tag_extensions() click to toggle source
# File lib/namaste/tag.rb, line 102
def load_tag_extensions
  case tag
    when "0"
      self.extend(Dirtype)
      
  end
end
write!() click to toggle source

write the namaste tag to the filesystem

# File lib/namaste/tag.rb, line 76
def write!
  file.rewind
  file.write(value)
  file.flush
  file.truncate(file.pos)
  file.rewind
end