Sha256: c5f643e87c2ff21f0ce000290853729fe552886c690a624c60362c79929d06f3

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require "fileutils"
require "ggem/template"

module GGem; end
class GGem::Gem
  NoNameError = Class.new(ArgumentError)

  attr_reader :root_path, :name

  def initialize(path, name)
    raise NoNameError if name.to_s.empty?
    @root_path, self.name = path, name
  end

  def save!
    GGem::Template.new(self).save
    self
  end

  def path
    File.join(@root_path, @name)
  end

  def name=(name)
    @name = normalize_name(name)
  end

  def module_name
    transforms = {
      "_" => "",
      "-" => ""
    }
    @module_name ||= transform_name(transforms){ |part| part.capitalize }
  end

  def ruby_name
    @ruby_name ||= transform_name{ |part| part.downcase }
  end

  private

  def normalize_name(name)
    und_camelcs = [/([A-Z])([a-z])/, '_\1\2']
    rm_dup_und  = [/_+/, "_"]
    rm_lead_und = [/^_/, "" ]
    name.gsub(*und_camelcs).gsub(*rm_dup_und).sub(*rm_lead_und).downcase
  end

  def transform_name(conditions = {}, &block)
    n = (block ? block.call(self.name) : self.name)
    conditions.each do |on, glue|
      if (a = n.split(on)).size > 1
        n = a.map{ |part| block.call(part) if block }.join(glue)
      end
    end
    n
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ggem-1.9.3 lib/ggem/gem.rb
ggem-1.9.2 lib/ggem/gem.rb