How should I generate a Rhodes extension from a Ruby gem? - ruby-on-rails

I know there is no direct support for Ruby gems in the Rhomobile framework. I have read their (sparse!) documentation to migrate in gem support through extensions, but I cannot for the life of me figure out how exactly this should be implemented.
Besides the document linked above being very disjointed, what I can find can't be easily translated to what I need. I am trying to bring devise into my app, but the gem structure is very hierarchical and the example given in the Rhomobile documentation suggests that a given library should be a singular .rb file.
The exact example given is as follows:
Assuming your application is called “mynewapp”, create a directory
under app called lib (or whatever you wish to call it):
$ cd mynewapp
$ mkdir app/lib
$ cp /path/to/my_lib.rb app/lib/my_lib.rb
Then just require lib/my_lib in a given file within my app. Eg:
require 'lib/my_lib'
To translate to the devise gem, my assumption is that I couldn't do something similar, but would instead have to flatten the directory structure out in some way before I could use it. Is that the case or am I missing something? That's a lot of re-writing code...
Also, if anyone knows of any kind of guide to adding gems to the Rhodes framework, I would love to see it! I've looked through most of the official documentation and some non-official and nothing seems to address this at all.

Wow. I don't know how I overlooked this, but it's really simple and nothing like what I was assuming.
If you are using Rhodes via the RubyGems installation, you must add external Ruby libraries to your RubyGems installation directory for the ‘rhodes-framework’ gem. Your RubyGems installation directory can be found with gem env in a terminal.
From the same page linked in question.
My paths didn't match what was listed in that document because I'm using RVM, but I just ran find / -name rhodes-* and just looked for the one followed by /lib/framework.

Related

Write new gem for using command line on terminal

Hi guys.
I wanna learn how to write a new gem by myself (I'm newbie).
So I found some document to do it but it's quite hard for me.
I want to write a gem. So when I install it, I can use it on terminal, for example:
$ search key_work
But all document just said that I have to use:
$ irb -Ilib -rMygem
And then I can use it with Mygem.search.But it's not my purpose.
My question is "How I can write a code for key-word that can run on terminal?"
Thanks for your help :)
You might not want to make a gem just yet. what you want - I think - is to make an executable. This does not require a full GEM.
I'll be making 2 assumptions here, you are on a UNIX system, and ~/bin is in your PATH.
First make a file with the name of your program (for this example call proga) in your ~/bin folder.
Start this file with (only first line matters)
#!/usr/bin/env ruby
# your code here
# p gets.strip
We just want to make it executable: chmod +x ~/bin/proga
If you really want to make a command line gem, you can find a good start here: http://robdodson.me/how-to-write-a-command-line-ruby-gem/ and http://blog.excelwithcode.com/build-commandline-apps.html

How can I get ctags to generate tags for a yeoman generated AngularJS webapp completely?

I have an AngularJS project that was scaffolded using yeoman. I want to use ctags to generate tags for the whole project so that I can navigate the code in vim. But when I use the command
ctags -R .
in the root folder, it generates tags for folders at one or two levels deeper relative to root. The folders at 5-6 levels deeper are not tagged by ctags. How can I get it to work for the whole project?
I am using exuberant-ctags for generating tags.
OS : Ubuntu 15.04
Do you get the same results with the following?
ctags -R *
What OS? Have you verified it's NOT a permissions issue?
ctags uses the file extension to correctly choose the parser that it will use to generate the tags file. It's possible that the files you are not finding have an unsupported extension.
Another possibility is that those files are using language extensions not supported by Exuberant Ctags. In which case you might want to try Universal Ctags, which was forked from Exuberant and is in active development. It is possible that the JS parser was improved in this fork of ctags.

How can I see the source code of a gem installed on my machine?

I installed Devise in my Rails app and I want to read through the source code and learn how it works. I looked through the entire folder structure of my Rails app, but couldn't find any code (except for the method calls).
I know I can see the source from the Github repository but I would like to see it in my editor and on my local machine.
I'm guessing this code must be in some main Ruby directory, but I'm having trouble locating it. Any help is appreciated. Thanks.
Besides Sergio's suggestion, there is another option.
Within your Rails path
$ bundle open devise
This will open the installed gem in editor with the version specified in Gemfile, very handy.
Try gem unpack, it will copy source of a gem to current directory. For example,
gem unpack rails
Documentation: gem unpack.
Simply run bundle show <gem-name>,
it will list the absolute path of gem source code and in next step simply open source code using text editor like this
subl <gem-code-absolute-path>
For Example
Let's assume you want to read kaminari gem code
bundle show kaminari
/home/abdullah/.rvm/gems/ruby-2.3.0#your_gem_name/gems/kaminari-0.16.3
next step (subl is command to open with Sublime Text Editor)
subl /home/abdullah/.rvm/gems/ruby-2.3.0#your_gem_name/gems/kaminari-0.16.3
Run gem environment - this will display you all the information about your gems, including their location.
Additionally I would advise you to install some IDE with go to source feature - RubyMine is just brilliant (and has 30-day-long free trial), if you want to go for absolutely free go with NetBeans together with Ruby plugin. This feature allows you to navigate quickly to source of clicked method, regardless whether it is defined inside your code or inside the gem.
Clone the github repo in your local machine and explore it using your prefered editor:
git clone https://github.com/plataformatec/devise.git

There a way to search all bundle'd gems project directories?

This comes up occasionally. Suppose I have some strange error like an image timing out pinging some external server. The file doesn't exist in my project, and most assuredly is being called from one of my gems, but I'm not sure which.
Is there a way to find all and look through all the bundled gems all at once?
small note: I use Sublime as my editor.
bundle show --paths gives you a list of gem paths in your project. You can then feed that list to grep or ack. See the instructions from the maintainer of Bundler in Hack your bundle for fun and profit.
If you use the command gem serverfrom with in a project, navigate to
http://localhost:8808/
in your browser. I think you may find what your looking for.

Copying files from a Rails plugin into the application upon plugin install

When someone installs this plugin, I would like a file to be copied into the config/initializers directory of the app. I could do this in install.rb by copying a template file that resides somewhere in the plugin. Another option would be to require the user to run a generator after install. I know rspec-rails makes you run a generator after you install it, is that the recommended behavior?
And is there anything wrong with copying files into the application in install.rb?
Thanks!
Lou
Does the user need to manually tweak the file? If so, then I would use a generator with parameters. If not, I would prefer that you do it with install.rb. My $.02

Resources