eiei0
eiei0

Reputation: 54

Add a basic UI layer to existing ruby app

First, I am fairly new to Ruby/RoR and so you'll have to forgive me for any wrong terminology, but hopefully I'll get my point across.

I built an ruby app that I am needing to add an extremely simple UI layer using rails. Read up on a previous post of mine that explains the project thoroughly to give you good an idea of what it does. Specifically take a look at the tree outline that I pasted in so you see the existing file structure for the project.

What I need to know, is how to convert this existing project into a rails app? My experience in building something with rails has always started out with rails new app_name, but never anything like this. Any tips would be appreciated.

Upvotes: 0

Views: 157

Answers (3)

eiei0
eiei0

Reputation: 54

These were great answers and I'm sure they would have worked perfectly. However, they were a little bit more complex than what I was looking for.

What I ultimately ended up doing was just cd into the directory above where the ruby app was located and then just simply ran rails new app_name. Rails will ask if you'd like to overwrite any files that exist already. From there I just integrated my script into the controller actions and created the views.

Upvotes: 0

AnoE
AnoE

Reputation: 8345

Rails is just "something" on top of Ruby. Especially, you can use any plain ruby objects inside of Rails, anywhere, and this is nothing unusual (google "PORO").

In your case, I would make a simple Rails app in the way you have mentioned yourself with rails new. Then trivially refactor your existing code until you have a simple, standalone class that does what you need to be done but takes its input/output from simple ruby data structures (i.e., method arguments, return values, no global state, no file operations). Then you can use that class from inside your Rails controller (taking input from a HTML form, rendering output to HTML), and also from inside your script (reading input from a file or STDIN, rendering output to STDOUT).

Where you put that class is up to you. In the MVC paradigm, it is not "C" or "V", and one could argue about whether it's "M". So put it into app/models/ or lib/, whatever you like more.

Upvotes: 1

Aetherus
Aetherus

Reputation: 8898

I saw your parser script, and it is not a daemon (a program that keeps running indefinitely in the background), right?

If I'm right, then you have several options:

The easiest option

Just build a rails application using rails new app_name, and inside some controller action, make a system call to run your script

class SomeController
  def some_action
    succeeded = system(:ruby, '/path/to/main.rb', '/path/to/some.txt')
    # Do some rendering stuff here based on the result of the system call
  end
end

This approach is somehow nasty for me, and it's not performant because each system call reads your ruby script and compiles or interprets it then runs it.

The harder option

Refactor your script so that it's features can be wrapped into a gem. Then you install that gem, require it in your rails app, and use it. I saw your original ruby script is almost there, it shouldn't be that hard to make it become a gem.

Upvotes: 2

Related Questions