Class: Chemlab::CLI::Stubber::PageLibrary

Inherits:
Object
  • Object
show all
Defined in:
lib/chemlab/cli/stubber.rb

Overview

A Class representation of a Page Library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PageLibrary

Returns a new instance of PageLibrary.



43
44
45
# File 'lib/chemlab/cli/stubber.rb', line 43

def initialize(path)
  @path = File.absolute_path(path)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



41
42
43
# File 'lib/chemlab/cli/stubber.rb', line 41

def path
  @path
end

Instance Method Details

#generate_stubObject

Generate the stub for a given Page Library



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chemlab/cli/stubber.rb', line 48

def generate_stub
  $stdout.print "- #{@path} :\t"

  begin
    load @path

    library = Stubber.libraries.last # last appended library is this Page Library

    require 'chemlab/core_ext/string/inflections'

    stub_path = @path.gsub(@path[@path.rindex('.')..], '.stub.rb')
    File.open(stub_path, 'w') do |stub|
      stub.write(ERB.new(File.read(File.expand_path("#{__dir__}/stub.erb")),
                         trim_mode: '%<>').result_with_hash({ library: library }))
      Stubber.libraries.pop
    end
  rescue StandardError => e
    # $stderr.print(e.message)
    raise e
  ensure
    $stdout.print "\tDone.\n"
  end
end