Test for data within config.rb?

So I am using middleman_contentful to populate my data object in middleman 4.1.8. This works fine and good so long as the data is already there. If it hasn’t yet been created (because a developer has just checked out a copy or wants to start from scratch), you can run middleman contentful to download the data into proper yaml files. However… I am using this data to generate proxy pages, so I get the following error:

undefined method `site' for #<Middleman::CoreExtensions::Data::DataStore:0x0000000393f668> (NoMethodError)

Fair enough - that key doesn’t exist, so let’s wrap an if statement around it:

if data && data[:site] != nil && data[:site][:pages]
  data.site.pages.each do |page|
    page = page[1]
    proxy "#{page.slug}.html", '/default.html', locals: { page: page }, ignore: true
  end
end

Except I then get another error:

/usr/local/rvm/gems/ruby-2.3.0/gems/middleman-core-4.1.8/lib/middleman-core/core_extensions/collections/lazy_step.rb:35:in `value': undefined method `[]' for nil:NilClass (NoMethodError)

Inspecting data[:site] however shows that it is #<Middleman::CoreExtensions::Collections::LazyCollectorStep:0x000000037e2310> - although this throws an error upon access.

My question is, how can I test if the data object has a key of :site - as LazyCollectorStep appears to try and fill the gap? Is this even possible?

Thanks in advance. :slight_smile:

Found a “fix” for my issue - check the datastore directly: @app.data && @app.data[:site]

Sadly I don’t know if that is a good idea or not! (But hey it works for me.)