Class: RablToJbuilder::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rabl_to_jbuilder/cli.rb', line 5

def initialize(argv)
  @files = []
  argv.each do |filename|
    if File.file?(filename)
      @files << filename
    elsif File.directory?(filename)
      @files.concat Dir[File.join(filename, "**/*.rabl")]
    else
      puts "Invalid filename #{filename}"
    end
  end
end

Instance Method Details

#convert_file(filename) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rabl_to_jbuilder/cli.rb', line 29

def convert_file(filename)
  raise "Filenames must end in .rabl" unless filename.end_with?(".rabl")

  # Guess at an object name
  action_name = File.basename(filename)[/\A([^.]+)/, 1]
  object_name_plural = File.basename(File.dirname(filename))
  object_name_singular = object_name_plural.singularize

  target = filename.gsub(/\.rabl$/, '.jbuilder')
  puts "#{filename} => #{target}"

  rabl = File.read(filename)
  jbuilder = RablToJbuilder.convert(rabl, object: Sexp.s(:lvar, object_name_singular.to_sym))
  #jbuilder = RablToJbuilder.convert(rabl)

  File.write(target, jbuilder)
end

#runObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/rabl_to_jbuilder/cli.rb', line 18

def run
  @files.each do |filename|
    begin
      convert_file(filename)
    rescue StandardError => e
      puts e
      puts e.backtrace
    end
  end
end