datocms / ruby-datocms-client

Ruby client for the DatoCMS API.
https://www.datocms.com/docs/content-management-api/using-the-ruby-client
MIT License
20 stars 15 forks source link

Added method_missing to directory Dump Dsl #10

Closed andresgutgon closed 7 years ago

andresgutgon commented 7 years ago

What is this?

This issue is that I'm wrapping my Dato content inside a directory. Which is using instance_eval. I'm reading here that methods outside of instance are not accessible

Here is what I'm trying to do:

# root of my dato_config.rb
def my_helper_method
  puts "Awesome"
end

directory 'src/posts' do
   I18n.available_locales.each do |locale|
    I18n.with_locale(locale) do
      # This will fail
      my_helper_method
     # NameError: undefined local variable or method `my_helper_method' for #<Dato::Dump::Dsl::Directory:0x007fa5cf5c2b60>
   end
  end
end

In the same article recommends doing something like this:


class Directory
  def initialize(&block)
    @self_before_instance_eval = eval "self", block.binding
    instance_eval &block
  end
  def method_missing(method, *args, &block)
    @self_before_instance_eval.send method, *args, &block
  end
end
coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.03%) to 93.47% when pulling f0d5169aaafd4d6853559a05325ceb93a9cfe2a8 on andresgutgon:fix/allow-external-methods-inside-directory-blocks into a240de18d0c39dc0bcabfc81b0f688cc53f6b64d on datocms:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.008%) to 93.511% when pulling 7d5c9d714b143c1092f66348cba56dfa1861d0e9 on andresgutgon:fix/allow-external-methods-inside-directory-blocks into a240de18d0c39dc0bcabfc81b0f688cc53f6b64d on datocms:master.

andresgutgon commented 7 years ago

Hi @stefanoverna could you take a look into this? Thanks for your work :)