ruby-rdf / spira

Spira is a framework for viewing RDF data as model objects
http://rubygems.org/gems/spira
The Unlicense
58 stars 36 forks source link

reset_changes in base.rb throwing NilClass error #48

Closed mohideen closed 6 years ago

mohideen commented 6 years ago

The reset_changes method is the base.rb is throwing nil:NilClass when trying to clear the @changed_attributes

https://github.com/ruby-rdf/spira/blob/5cfaa9c5d226ee170020d767122cf86e8a62b723/lib/spira/base.rb#L288

 /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/spira/base.rb:288:in `reset_changes': undefined method `clear' for nil:NilClass (NoMethodError)
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/spira/base.rb:162:in `reload'
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/spira/base.rb:124:in `initialize'
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/spira/persistence.rb:195:in `new'
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/spira/persistence.rb:195:in `project'
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/spira/persistence.rb:176:in `for'
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/spira-3.0.0/lib/rdf/ext/uri.rb:16:in `as'
        from /Users/mohideen/.rvm/gems/ruby-2.4.0/gems/worldcat-discovery-1.2.0/lib/worldcat/discovery/bib.rb:188:in `search'

Context: The problem occurs when RDF URI is requested to be modeled as a class which is an extension of the Spira::Base class.

  1. Bib (tries to model RDF URI (subject) as BibSearchResults): https://github.com/OCLC-Developer-Network/worldcat-discovery-ruby/blob/master/lib/worldcat/discovery/bib.rb#L179
  2. BibSearResults (extends SearchResults): https://github.com/OCLC-Developer-Network/worldcat-discovery-ruby/blob/master/lib/worldcat/discovery/bib_search_results.rb
  3. SearchResults (extends Spira::Base): https://github.com/OCLC-Developer-Network/worldcat-discovery-ruby/blob/master/lib/worldcat/discovery/search_results.rb

The error does not happen if change the following code

@changed_attributes.clear

to

@changed_attributes.clear if changed?

Is this a bug in the code or am I not using this correctly?

mohideen commented 6 years ago

Associated PR #49

gkellogg commented 6 years ago

Instead of changed? you might just be sure @changed_attributes is not nil, or responds to #clear?

gkellogg commented 6 years ago

Also, practice is to add a test case in /spec and coverage should not drop significantly.

mohideen commented 6 years ago

@gkellogg Thanks for your feedback! I have updated the PR accordingly.

As a side note, I don't fully understand the coverage stats. When I run the tests locally (ruby 2.4), the develop branch has 78.86% and the patch-1 branch has 89.55%. Also, the spec fails if it is run with jruby.

gkellogg commented 6 years ago

Travis runs specs with CI=true. Note tests still failing on Travis, and should for you locally too.

Jruby has been a problem for all RDF gems, and results are allowed to fail.

abrisse commented 6 years ago

Related to #47 which should solve this issue once the patch is provided. So I close this issue.