Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use a different configuration #1

Open
diegoponciano opened this issue Jan 9, 2018 · 1 comment
Open

Can't use a different configuration #1

diegoponciano opened this issue Jan 9, 2018 · 1 comment

Comments

@diegoponciano
Copy link

I'm trying to use this lib in a project (ruby 2.3.1p112 and 'rails', '~> 5.0.1'), but wasn't able to point to a different YAML configuration. I've put the following code into config/initializers/cnab150.rb:

Cnab150.configure do |config|
  config.layout_file_path = "#{Rails.root}/config/cnab150.yml"
end

But I get this: project/config/initializers/cnab150.rb:2:in `block in <top (required)>': undefined method `layout_file_path=' for nil:NilClass (NoMethodError)

@ghost
Copy link

ghost commented Jan 11, 2018

To follow up, I monkeypatched the configuration file and it worked. I'm very new to ruby/rails, so I'm not really sure what's going on, but I guess it's got to do with the Rails version I'm using.
Code patched:

module Cnab150
  class << self
    attr_accessor :config
  end

  def self.configure
    self.config ||= Configuration.new
    yield(config)
  end

  class Configuration
    attr_accessor :layout_file_path

    def initialize
      @layout_file_path = "#{File.dirname(__dir__)}/../config/layout.yml"
    end

    # The layouts method load the layouts from the yml file.
    def layouts
      @_ ||= YAML.load_file(@layout_file_path)['cnab150']
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant