Hobo 2.1.1 - undefined method `method_defined?' for Array - [ Using table-plus with Array of OpenStructs ]

134 views
Skip to first unread message

Tim Griffin

unread,
Dec 12, 2014, 11:57:50 PM12/12/14
to hobo...@googlegroups.com
Hi all;

In Hobo 2.1.1, I'm creating an array of OpenStructs to pass back to a table-plus for display. This certainly works nicely in Hobo 2.0/Ruby 1.9.3, but I'm getting an odd error with 2.1.1/Rails 4.0.8/Ruby/2.1.4:

  undefined method `method_defined?' for #<Array:0x007fdb88622170>
  app/views/taglibs/application/company.dryml:848:in `block (8 levels) in show_page__for_company'


The Array I pass to a table-plus tag is created as follows:

 def get_documents
    documents = Array.new
    files = Dir.glob(folder + "*.{pdf,PDF}")
    ...

    # Go through each of the files retrieved, and extract their details to be usable
    r = 0                
  
    files.each do |doc|
      filing_date = "Unknown"
      filing = "Unknown"
    
      filing_date, filing, filing_year = Filings.decode_filename(doc, self.entity_type_code)
            
      if filing.present? 
         documents[r] = OpenStruct.new(:doc_id => doc, :filing_date => filing_date, :filing => filing, :filing_year => filing_year)
         r = r + 1
      end
    end 
    return documents
  end  
  
A sample Array I passed to the table-plus tag is as follows:

[
#<OpenStruct doc_id="711021-140619-IN.PDF", filing_date="140619", filing="Incorporation", filing_year="NA">, 
#<OpenStruct doc_id="711021-140619-ND.PDF", filing_date="140619", filing="Notice/Change of Directors", filing_year="NA">
]

And, the table-plus tag is nothing unusual:

    <% documents = this.get_documents %>
            <if test="&documents.present?">
              <table-plus with="&documents" fields="filing,filing_date,filing_year, doc_id">
              </table-plus>
            </if>

Does anyone know why this might be causing the undefined method `method_defined?' for Array error? 

Some new anomaly with Rails 4 / Ruby 2?

Many thanks,
Tim




Tim Griffin

unread,
Dec 13, 2014, 8:30:54 AM12/13/14
to hobo...@googlegroups.com
It seems this error is arising from this line within 

    <page-nav param if="&this.method_defined?(:page_count) || this.method_defined?(:total_pages)" merge-attrs="&ajax_attrs" />

It appears that method_defined? cannot be called on an instance of Array (although it can be called on the Array class):

[tgriffin@mbp ~/Sites/crs] $ irb
2.1.4 :004 > Array.method_defined?(:page_count)
 => false
2.1.4 :006 > a = Array.new
 => []
2.1.4 :007 > a.method_defined?(:total_pages)
NoMethodError: undefined method `method_defined?' for []:Array

I see this listed as a problem for uninstantiated ActiveRecord classes, but why is this a problem for a built-in class like Array?

For now, I will override the table-plus tag to drop the <page-nav> bit.

Any other insight welcome!
Tim

Ignacio Huerta

unread,
Dec 16, 2014, 8:08:16 AM12/16/14
to hobo...@googlegroups.com
I'm pretty sure this commit provoked your problem:
https://github.com/Hobo/hobo/commit/6fbb12e1db73f9a82ead69e93926613cab03282b

I'll open an issue to investigate this further and create all the
necessary test cases to ensure we cover all cases. Thanks for sharing!

Warm regards,
Ignacio

El 13-12-2014 a las 14:30, Tim Griffin escribió:
> It seems this error is arising from this line within
>
> <page-nav param if="&this.method_defined?(:page_count) ||
> this.method_defined?(:total_pages)" merge-attrs="&ajax_attrs" />
>
> It appears that method_defined? cannot be called on an /instance/ of
> Array (although it can be called on the Array /class/):
>
> [tgriffin@mbp ~/Sites/crs] $ irb
> 2.1.4 :004 > Array.method_defined?(:page_count)
> => false
> 2.1.4 :006 > a = Array.new
> => []
> 2.1.4 :007 > a.method_defined?(:total_pages)
> NoMethodError: undefined method `method_defined?' for []:Array
>
> I see this listed as a problem
> <http://stackoverflow.com/questions/8013773/testing-for-method-defined-on-activerecord-classes-doesnt-work-until-an-instan>
> for uninstantiated ActiveRecord classes, but why is this a problem for a
> built-in class like /Array?/
> Does anyone know why this might be causing the *undefined method
> `method_defined?' for Array* error?
>
> Some new anomaly with Rails 4 / Ruby 2?
>
> Many thanks,
> Tim
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Hobo Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hobousers+...@googlegroups.com
> <mailto:hobousers+...@googlegroups.com>.
> To post to this group, send email to hobo...@googlegroups.com
> <mailto:hobo...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hobousers.
> For more options, visit https://groups.google.com/d/optout.

Tim Griffin

unread,
Dec 16, 2014, 9:15:28 AM12/16/14
to hobo...@googlegroups.com
Ah hah! A subtle change, for sure. A clear case of "it used to work"!

In my particular case, I simply switched to a <table> tag as I didn't need the pagination, but I'm glad to know what caused the difference. 

Tim


You received this message because you are subscribed to a topic in the Google Groups "Hobo Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hobousers/iK1cWYANfDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hobousers+...@googlegroups.com.
To post to this group, send email to hobo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages