Sha256: 7b52dbacac1d99d79e17611768294efdb3de8db90587974419220e8afa354e33

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require "fileutils"
require "ggem/template"

module GGem

  class Gem

    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!
      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

    NoNameError = Class.new(ArgumentError)

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ggem-1.9.1 lib/ggem/gem.rb
ggem-1.9.0 lib/ggem/gem.rb