class SocialSnippet::Context

Attributes

flag_absolute[R]
path[R]
pathname[R]
ref[R]
repo[R]

Public Class Methods

new(new_path, new_repo = nil, new_ref = nil) click to toggle source

Constructor

@param new_path [String] The path of context

# File lib/social_snippet/context.rb, line 12
def initialize(new_path, new_repo = nil, new_ref = nil)
  set_path new_path
  @repo = new_repo
  @ref  = new_ref
end

Public Instance Methods

basename() click to toggle source
# File lib/social_snippet/context.rb, line 53
def basename
  pathname.basename.to_s unless path.nil?
end
dirname() click to toggle source
# File lib/social_snippet/context.rb, line 57
def dirname
  pathname.dirname.to_s unless path.nil?
end
is_in_repository?() click to toggle source

Check context in repo

@return [Boolean]

# File lib/social_snippet/context.rb, line 28
def is_in_repository?
  repo.nil? === false
end
move(new_path, new_repo = nil, new_ref = nil) click to toggle source

Move to new path from current path

@param new_path [String] The next path @param new_repo [String] The next repository @param new_ref [String] The next reference

# File lib/social_snippet/context.rb, line 37
def move(new_path, new_repo = nil, new_ref = nil)
  if new_repo.nil?
    if is_absolute_path(new_path)
      @flag_absolute = true
      set_path new_path
    else
      set_path move_path(new_path)
    end
  else
    @flag_absolute = false
    set_path new_path
    @repo = new_repo
    @ref  = new_ref
  end
end
root_text?() click to toggle source
# File lib/social_snippet/context.rb, line 61
def root_text?
  path.nil?
end
set_path(new_path) click to toggle source
# File lib/social_snippet/context.rb, line 18
def set_path(new_path)
  return if new_path.nil?
  @path = new_path
  @pathname = ::Pathname.new(path)
  @flag_absolute = is_absolute_path(path)
end

Private Instance Methods

is_absolute_path(s) click to toggle source

Check given text is absolute path

# File lib/social_snippet/context.rb, line 94
def is_absolute_path(s)
  s[0] === "/"
end
is_dot(s) click to toggle source

Check given text is ‘.`

# File lib/social_snippet/context.rb, line 99
def is_dot(s)
  s === "."
end
is_dotdot(s) click to toggle source

Check given text is ‘..`

# File lib/social_snippet/context.rb, line 104
def is_dotdot(s)
  s === ".."
end
move_path(new_path) click to toggle source
# File lib/social_snippet/context.rb, line 67
def move_path(new_path)
  source = dirname.split("/")
  dest = new_path.split("/")
  destname = dest.pop

  if is_absolute_path(path)
    source.shift
  end

  dest.each do |x|
    if is_dotdot(x)
      source.pop
    elsif ! is_dot(x)
      source.push x
    end
  end

  if flag_absolute
    "/" + source.join("/") + "/" + destname
  else
    source.join("/") + "/" + destname
  end
end