Class: Eastrails::Components::Base

Inherits:
Generator
  • Object
show all
Defined in:
lib/eastrails/components/base.rb

Instance Method Summary collapse

Methods inherited from Generator

#initialize

Constructor Details

This class inherits a constructor from Eastrails::Generator

Instance Method Details

#addObject



8
9
10
# File 'lib/eastrails/components/base.rb', line 8

def add
  raise "Unimplemented #{self.class.name}#add"
end

#component_installed?(component) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/eastrails/components/base.rb', line 64

def component_installed?(component)
  case component
  when "rspec"
    File.directory?("spec")
  when "cucumber"
    File.directory?("features")
  when "haml"
    gems.include? "haml"
  end
end

#dir_html_to_haml(source_dir) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/eastrails/components/base.rb', line 50

def dir_html_to_haml(source_dir)
  Dir[File.expand_path("#{source_dir}/**/*.html.erb")].each do |file_path|
    destination_path = file_path.gsub(/.erb/, '.haml')
    File.write(destination_path, file_html_to_haml(file_path))
    remove_file file_path
  end
end

#do_if_installed(*components) ⇒ Object



58
59
60
61
62
# File 'lib/eastrails/components/base.rb', line 58

def do_if_installed(*components)
  if components.any? { |component| component_installed?(component) }
    yield
  end
end

#file_html_to_haml(source) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eastrails/components/base.rb', line 32

def file_html_to_haml(source)
  begin
    html = open(source) {|input| input.binmode.read }
    html_to_haml(html)
  rescue RubyParser::SyntaxError
    say_wizard "Ignoring RubyParser::SyntaxError"
    # special case to accommodate https://github.com/RailsApps/rails-composer/issues/55
    html = open(source) {|input| input.binmode.read }
    say_wizard "applying patch" if html.include? "card_month"
    say_wizard "applying patch" if html.include? "card_year"
    html = html.gsub(/, {add_month_numbers: true}, {name: nil, id: "card_month"}/, "")
    html = html.gsub(/, {start_year: Date\.today\.year, end_year: Date\.today\.year\+10}, {name: nil, id: "card_year"}/, "")
    result = html_to_haml(html)
    result = result.gsub(/select_month nil/, "select_month nil, {add_month_numbers: true}, {name: nil, id: \"card_month\"}")
    result = result.gsub(/select_year nil/, "select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: nil, id: \"card_year\"}")
  end
end

#gemsObject



25
26
27
# File 'lib/eastrails/components/base.rb', line 25

def gems
  @@gems ||= parse_gemfile
end

#html_to_haml(html) ⇒ Object



29
30
31
# File 'lib/eastrails/components/base.rb', line 29

def html_to_haml(html)
    ::Haml::HTML.new(html, :erb => true, :xhtml => true).render
end

#parse_gemfileObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/eastrails/components/base.rb', line 12

def parse_gemfile
  list_gems = Set.new
  gemfile_content = File.read("Gemfile")
  gemfile_content.gsub!(/\r\n?/, "\n")
  gemfile_content.each_line do |line|
    if (line = line.strip) =~ /^gem/
      data = line.match(/gem\s+["']([^"']+)["']/)
      list_gems.add data[1] if data.length > 0
    end
  end
  list_gems.to_a
end