Class: ReactToRails::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/react_to_rails/convert.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(react_content, name:, client: nil) ⇒ Convert

Returns a new instance of Convert.



48
49
50
51
52
# File 'lib/react_to_rails/convert.rb', line 48

def initialize(react_content, name:, client: nil)
  @react_content = react_content
  @name = name
  @client = client || Client.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def client
  @client
end

#demo_erb_codeObject (readonly)

Returns the value of attribute demo_erb_code.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def demo_erb_code
  @demo_erb_code
end

#erb_templateObject (readonly)

Returns the value of attribute erb_template.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def erb_template
  @erb_template
end

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def name
  @name
end

#react_contentObject (readonly)

Returns the value of attribute react_content.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def react_content
  @react_content
end

#summaryObject (readonly)

Returns the value of attribute summary.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def summary
  @summary
end

#view_component_ruby_codeObject (readonly)

Returns the value of attribute view_component_ruby_code.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def view_component_ruby_code
  @view_component_ruby_code
end

#view_component_test_ruby_codeObject (readonly)

Returns the value of attribute view_component_test_ruby_code.



39
40
41
# File 'lib/react_to_rails/convert.rb', line 39

def view_component_test_ruby_code
  @view_component_test_ruby_code
end

Class Method Details

.for_path(path, name:) ⇒ Object



54
55
56
57
# File 'lib/react_to_rails/convert.rb', line 54

def self.for_path(path, name:)
  react_content = File.read(path)
  new(react_content, name: name)
end

Instance Method Details

#callObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/react_to_rails/convert.rb', line 59

def call
  puts "### PROMPT ###"
  puts
  puts prompt
  puts
  puts "Waiting for OpenAI to respond..."

  response = Client.new.call(prompt)

  @summary = response.fetch("summary")
  @view_component_ruby_code = response.fetch("view_component_ruby_code")
  @erb_template = response.fetch("erb_template")
  @demo_erb_code = response.fetch("demo_erb_code")
  @view_component_test_ruby_code = response.fetch("view_component_test_ruby_code")

  puts @summary

  return if @erb_template == "" || @view_component_ruby_code == ""

  puts
  puts "### EXAMPLE USAGE ###"
  puts
  puts @demo_erb_code
end

#component_file_nameObject



84
85
86
# File 'lib/react_to_rails/convert.rb', line 84

def component_file_name
  @name.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
end