Thor YAML.load_file invalid subclass (TypeError) - ruby

I'm creating a thor script that shows the current project I am on based on a yml file that stores Ruby structures. I'm getting an error when I tried to load this yml file.
from /Users/cpara/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/syck.rb:135:in `node_import'
from /Users/cpara/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/syck.rb:135:in `load'
from /Users/cpara/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/syck.rb:135:in `load'
from /Users/cpara/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/syck.rb:146:in `block in load_file'
from /Users/cpara/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/syck.rb:145:in `open'
from /Users/cpara/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/syck.rb:145:in `load_file'
from ./project:84:in `current'
from /Users/cpara/.rvm/gems/ruby-1.9.2-p318#rails/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
from /Users/cpara/.rvm/gems/ruby-1.9.2-p318#rails/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
from /Users/cpara/.rvm/gems/ruby-1.9.2-p318#rails/gems/thor-0.14.6/lib/thor.rb:263:in `dispatch'
from /Users/cpara/.rvm/gems/ruby-1.9.2-p318#rails/gems/thor-0.14.6/lib/thor/base.rb:389:in `start'
from ./project:213:in `<main>'
Here is the piece of the script I'm trying to run:
#!/usr/bin/env ruby
require 'yaml'
class Project < Thor
include Thor::Actions
# Task: return current project
desc 'current', 'Shows current project.'
def current
projects = YAML.load_file "#{ENV['HOME']}/.hana/data/projects.yml" #error
abort "There are no projects. Try creating one first." if not #projects.is_a? Array
projects.each do |project|
if project.current == true
say_status :current, "Current project: #{project.name} // #{project.type} // #{project.version}", :green
return project
end
end
say_status :error, "There is no current project.", :red
end
end
I've triple checked the path in irb and it does exist. I thought it was the way my YAML was storing my Ruby structure but even the console, I get the error. Here is my YAML file
---
- !ruby/struct:Proj
name: test
type: testing
version: 4.0.2
deploy_dir: deploy
source_dir: source
current: true
Any ideas? I'm running Ruby 1.9.2p318.

YAML is trying to instanciate a Struct named Proj from the file as indicated by the line:
!ruby/struct:Proj
You should require the file where you haved defined Proj before loading the yaml. Or, just to test if it works, in your code, after the require 'yaml' line define Proj:
Proj = Struct.new(:name, :type, :version, :deploy_dir, :source_dir, :current)

Related

Custom Rake task unable to be called twice in same Rakefile unike Rake::TestTask

I recently read an "older", 2009, article about how to make a Custom Rake tasks. So far it works for the first iteration, but I saw that Rake::TestTask can be called twice, so I figured I could do it, however my name attr_accessor is not picking up the symbol I'm passing to it.
require 'rake'
require 'rake/tasklib'
module Phil
class FooTask < Rake::TaskLib
attr_accessor :name
attr_accessor :data
attr_accessor :task_dependencies
def initialize(name = :task, task_dependencies)
#name = name
#data = nil
yield self if block_given?
#task_dependencies = task_dependencies
define
end
def define
desc "Run the #{#name} task"
task #name => #task_dependencies do
puts 'Some Test being Printed'
puts #data
sh 'echo blah'
end
self
end
end
end
Phil::FooTask.new :foo, [:call_me_first, :call_me_second]
task :call_me_first do
puts 'I am called first because I am a dependency'
end
task :call_me_second do
puts 'I am called second because Im also a dependency'
end
Phil::FooTask.new(:stuff) do |t|
t.data = 'I am a stuff task.'
end
The following is the results I get.
C:\Users\user01\Desktop
λ rake --tasks
rake foo # Run the foo task
rake stuff # Run the stuff task
C:\Users\user01\Desktop
λ rake foo
I am called first because I am a dependency
I am called second because Im also a dependency
Some Test being Printed
echo blah
blah
C:\Users\user01\Desktop
λ rake stuff --trace
** Invoke stuff (first_time)
rake aborted!
Don't know how to build task '{}' (see --tasks)
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task_manager.rb:58:in `[]'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:61:in `lookup_prerequisite'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:57:in `block in prerequisite_tasks'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:57:in `map'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:57:in `prerequisite_tasks'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:214:in `invoke_prerequisites'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:193:in `block in invoke_with_call_chain'
C:/tools/ruby23/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:80:in `block in run'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
C:/tools/ruby23/bin/rake:22:in `load'
C:/tools/ruby23/bin/rake:22:in `<main>'
Tasks: TOP => stuff
I'm not sure why the Rake task is failing. Even from reading the Rake's TestTask code, it seems I'm doing mostly everything correctly. I'll also say that I'm not a professional on Ruby and all the little tricks.
Keep arguments with default value at the end in method declaration!
The reason rake stuff is raising error is because of your Phil::FooTask.new(:stuff) do |t| call. Your initialize method expects two arguments. The error is in your declaration of the constructor where you've declared name = :task which has a default value of :task. However, the second parameter task_dependencies is expected. It is the second parameter that you're missing when initializing task with name :stuff.
Modify your initialize method declaration as follows:
def initialize(name = :task, task_dependencies = [])
Then you should see correct tasks when invoking rake -T:
rake foo # Run the foo task
rake stuff # Run the stuff task

How to define Ruby Test::Unit testcase with `must`

I had a test case of the following form:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
def test_should_do_action
assert true
end
end
and have re-written it using must as suggested in the book A Test::Unit Trick to Know About:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
must "do action" do
assert true
end
end
And when I run it, I get an undefined method 'must' error shown as follows:
SomeTests.rb:3:in `<class:SomeTests>': undefined method `must' for SomeTests:Class (NoMethodError) from
SomeTests.rb:2:in `<top (required)>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `each' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `block in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `select' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `<main>' rake aborted! Command failed with status (1): [ruby -w -I"lib" -I"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0" "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb" "test/**/*Tests.rb" ]
Tasks: TOP => default => test (See full trace by running task with --trace)
I thought that must might be a part of minitest, so I required 'minitest/unit' instead, but I still get an error. I also assume that must keyword isn't part of rspec, which I'm not using yet.
How do I get this to work properly?
It looks like that method is not provided out of the box, but was developed by a third party. You need to add code described here.

rspec bugs when trying to run rake

I'm trying to teach myself some ruby using the app academy tutorials and after doing the readings, installing rvm,rubygems and rspec2 when I even try to run the first most basic code (00_hello) with rake I get the whole error :
(in /home/deadpool/Documents/learn_ruby)
/home/deadpool/Documents/learn_ruby/rspec_config.rb:3:in `block in <top (required)>': undefined method `color=' for #<RSpec::Core::Configuration:0x0000000293dee0> (NoMethodError)
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core.rb:67:in `configure'
from /home/deadpool/Documents/learn_ruby/rspec_config.rb:1:in `<top (required)>'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `require'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `block in requires='
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `map'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:162:in `requires='
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration_options.rb:22:in `block in configure'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration_options.rb:21:in `each'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/configuration_options.rb:21:in `configure'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:17:in `run'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:55:in `run_in_process'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:46:in `run'
from /home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:10:in `block in autorun'
rake aborted!
ruby -S bundle exec rspec -I/home/deadpool/Documents/learn_ruby/00_hello -I/home/deadpool/Documents/learn_ruby/00_hello/solution -f documentation -r ./rspec_config "/home/deadpool/Documents/learn_ruby/00_hello/hello_spec.rb" failed
/home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/rake_task.rb:117:in `rescue in block (2 levels) in initialize'
/home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/rake_task.rb:113:in `block (2 levels) in initialize'
/home/deadpool/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.0.0/lib/rspec/core/rake_task.rb:109:in `block in initialize'
Tasks: TOP => default => spec
(See full trace by running task with --trace)
I tried to google some of the errors, but with no success. I had previously another error with the rake file using rspec v2 and the current version is 3.0.0, so I had to install the older and I think it might be another setup problem. Thanks if someone can help me or direct me.
rspec_config.rb file :
RSpec.configure do |c|
c.fail_fast = true
c.color = true
end
hello.rb file:
def hello
"Hello!"
end
def greet(who)
"Hello, #{who}!"
end
UPDATE
Getting new error as :-
While I changed c.color = true to c.color_enabled = true
(in /home/deadpool/Documents/learn_ruby)
the hello function says hello (FAILED - 1)
Failures: 1) the hello function says hello Failure/Error:
Unable to find matching line from backtrace undefined method run_all' for []:Array
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/hooks.rb:116:inrun_hook_filtered'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/example_group.rb:176:in eval_before_alls'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/example_group.rb:231:inrun'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:26:in block (2 levels) in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:26:inmap'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:26:in block in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/reporter.rb:11:inreport'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:23:in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:55:inrun_in_process'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:46:in run'
# /home/deadpool/.rvm/gems/ruby-2.1.2/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:10:inblock in autorun'
UPDATE
hello_spec.rb file :
require "hello"
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end
UPDATE
So, I updated rspec to v3.0.0 and changed Rakefile gem 'rspec', '~>3.0.0' and c.color = true back. Everything is working now(getting some deprecation warnings, but nothing critical), getting the output that is in the tutorial. Guess I just had to updae my rspec and change the version in the Rakefile. Thank you so much :)
Your error log is showing you are in rspec-core-2.0.0 version and your error is saying -
rspec_config.rb:3:in `block in <top (required)>': undefined method `color=' for
#<RSpec::Core::Configuration:0x0000000293dee0> (NoMethodError)
Now Deprecate config options confirms that below versions of 2.99.0.rc1 / 2014-05-18 or 2.99.0 methods were - #color_enabled, #color_enabled= and #color?. Which are changed since 2.99.0 to #color, #color= and #color_enabled?.
I got the information from the changelog as I linked -
Deprecate #color_enabled, #color_enabled= and #color? in favour of #color, #color= and #color_enabled? output. (Jon Rowe)
Thus you need to write as
RSpec.configure do |c|
c.fail_fast = true
c.color_enabled = true
end
Regarding your new error, I found it as a bug undefined methodrun_all' for []:Array`. Which has been fixed in this patch. Check this Rspec issue.
My suggestion use Rspec 3.0, at least you will be happy. In this case revert the color_enabled to color.
Hope this would help you.

undefined method `infer_base_class_for_anonymous_controllers=' for #<RSpec::Core::Configuration:0x007f7fb3a62b80> (NoMethodError)

I'm currently on Chapter 7 of Hartl's Tutorial, and every time I run
bundle exec rspec spec/
the following results:
/Users/siciliana/sample_app/spec/spec_helper.rb:31:in `block in <top (required)>': undefined method `infer_base_class_for_anonymous_controllers=' for #<RSpec::Core::Configuration:0x007f7fb3a62b80> (NoMethodError)
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core.rb:79:in `configure'
from /Users/siciliana/sample_app/spec/spec_helper.rb:11:in `<top (required)>'
from /Users/siciliana/sample_app/spec/models/user_spec.rb:12:in `require'
from /Users/siciliana/sample_app/spec/models/user_spec.rb:12:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `map'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:18:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'
Can someone please explain what is happening here to an absolute newbie?
spec_helper.rb :
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
#
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
I was getting the same error (although not following the tutorial).
I believe this setting requires 'rspec/rails'
My solution was to move the line
config.infer_base_class_for_anonymous_controllers = # whatever
From spec_helper.rb to rails_helper.rb.

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Resources