Sha256: df8711cc8d0ae6754adfb1c0e2c45bf29a17b508a674a3d49006ea5c08e7c03d

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

class Skippy::Namespace

  def initialize(namespace)
    unless valid?(namespace)
      raise Skippy::Error, "'#{namespace}' is not a valid Ruby namespace"
    end
    @namespace = namespace
  end

  def basename
    to_a.last
  end

  def open
    @open ||= to_a.map { |part| "module #{part}" }.join("\n")
    @open
  end

  def close
    @close ||= to_a.reverse.map { |part| "end # module #{part}" }.join("\n")
    @close
  end

  def to_a
    parts(@namespace)
  end

  def to_name
    basename_words(basename).join(' ')
  end

  def to_s
    @namespace.dup
  end

  def to_underscore
    basename_words(basename).map { |word| word.downcase }.join('_')
  end

  private

  def basename_words(namespace_basename)
    result = namespace_basename.scan(/[[:upper:]]+[[:lower:][:digit:]]*/)
    result.empty? ? [namespace_basename.dup] : result
  end

  def parts(namespace)
    namespace.split('::')
  end

  def valid?(namespace)
    parts(namespace).all? { |part| /^[[:upper:]]/.match(part) }
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
skippy-0.1.1.a lib/skippy/namespace.rb
skippy-0.1.0.a lib/skippy/namespace.rb