class Kamaze::Project::Tools::Gemspec::Writer

Class intended to generate “gemspec“ file from a template

Default template filename is “gemspec.tpl“. Consider to use “Dir.chdir“ in order read and generate contents from the right directory (especially during tests).

@see templated @see Kamaze::Project

Attributes

fs[R]

@return [FileUtils]

project[RW]

@see Kamaze.project @type [Object|Kamaze::Project]

@return [Kamaze::Project]

templated[W]

Set path (almost filename) to templated gemspec

@type [String|Pathname]

Public Instance Methods

content() click to toggle source

Get generated/templated content

@return [String]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 99
def content
  template.render(templated.to_s, context)
end
context() click to toggle source

Get template's context (variables)

@return [Hahsh]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 84
def context
  # @formatter:off
  {
    name: project.name,
    version: project.version,
    dependencies: dependency,
  }.yield_self do |variables|
    project.version.to_h.merge(variables)
  end
  # @formatter:on
end
dependency() click to toggle source

Get dependency

@return [Dependency]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 77
def dependency
  DepGen.new(spec_id).dependency
end
generated() click to toggle source

Get path (almost filename) to generated gemspec

@return [Pathname]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 50
def generated
  Pathname.new("#{project.name}.gemspec")
end
mutable_attributes() click to toggle source
# File lib/kamaze/project/tools/gemspec/writer.rb, line 36
def mutable_attributes
  [:templated, :project]
end
spec_id() click to toggle source

Get variable used in template for “Gem::Specification“ instantiation

@return [String]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 65
def spec_id
  # @formatter:off
  templated
    .read
    .scan(/Gem::Specification\.new\s+do\s+\|([a-z]+)\|/)
    .flatten.fetch(0)
  # @formatter:on
end
status() click to toggle source

Get status for current gemspec file.

@return [Hash{Symbol => Object}]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 106
def status
  Pathname.new(self.to_s).yield_self do |file|
    # @formatter:off
    {
      mtime: -> { return File.mtime(file) if file.file? }.call,
      content: -> { return file.read if file.file? }.call
    }
    # @formatter:on
  end
end
templated() click to toggle source

Get path (almost filename) to templated gemspec

@return [Pathname]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 43
def templated
  Pathname.new(@templated)
end
to_s() click to toggle source

Get string representation

@see generated @return [String]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 58
def to_s
  generated.to_s
end
write(preserve_mtime: false) click to toggle source

Write gemspec file

@return [self]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 120
def write(preserve_mtime: false)
  self.tap do
    (preserve_mtime ? status : {}).tap do |meta|
      generated.write(content)

      if preserve_mtime
        if content == meta.fetch(:content, nil)
          fs.touch(self.to_s, mtime: meta.fetch(:mtime), nocreate: true)
        end
      end
    end
  end
end

Protected Instance Methods

setup() click to toggle source
# File lib/kamaze/project/tools/gemspec/writer.rb, line 139
def setup
  @templated ||= 'gemspec.tpl'
  @project ||= Kamaze::Project.instance
  @fs ||= FileUtils
end
template() click to toggle source

Get template engine

@return [Tenjin::Engine]

# File lib/kamaze/project/tools/gemspec/writer.rb, line 148
def template
  Tenjin::Engine.new(cache: false)
end