diff --git a/lib/prawn-rails/config.rb b/lib/prawn-rails/config.rb index 3f8b2fa..70e8cca 100644 --- a/lib/prawn-rails/config.rb +++ b/lib/prawn-rails/config.rb @@ -1,12 +1,27 @@ -require 'ostruct' - module PrawnRails - extend self + class Config < HashWithIndifferentAccess + def method_missing(method_name, *args) + # to replace behaviour of previous openstruct-based configuration object + + method_name = method_name.to_s - @config = OpenStruct.new(page_layout: :portrait, page_size: "A4", skip_page_creation: false) + if method_name.end_with?("=") + key = method_name.sub(/=$/, "") + self[key] = args.first + else + self[method_name] + end + end + end + + @config = Config.new.merge( + page_layout: :portrait, + page_size: "A4", + skip_page_creation: false, + ) def config(&block) block_given? ? yield(@config) : @config end - + module_function :config end diff --git a/lib/prawn-rails/document.rb b/lib/prawn-rails/document.rb index 133cee7..37bfde3 100644 --- a/lib/prawn-rails/document.rb +++ b/lib/prawn-rails/document.rb @@ -4,7 +4,7 @@ module PrawnRails class Document < Prawn::Document def initialize(options = {}) - options = PrawnRails.config.marshal_dump.merge(options) ### For Ruby 1.9.3 support, use `marshal_dump` instead of `to_h` + options = PrawnRails.config.merge(options) super(options) end diff --git a/test/unit/config_test.rb b/test/unit/config_test.rb index 87f9c30..00f01fb 100644 --- a/test/unit/config_test.rb +++ b/test/unit/config_test.rb @@ -19,7 +19,7 @@ def teardown end test "has a default config" do - assert_equal PrawnRails.config.to_h, {page_layout: :portrait, page_size: "A4", skip_page_creation: false} + assert_equal PrawnRails.config.to_h.symbolize_keys, {page_layout: :portrait, page_size: "A4", skip_page_creation: false} end test "config can be set with block syntax" do @@ -28,7 +28,7 @@ def teardown config.page_size = "A8" end - assert_equal PrawnRails.config.to_h, {page_layout: :landscape, page_size: "A8", skip_page_creation: false} + assert_equal PrawnRails.config.to_h.symbolize_keys, {page_layout: :landscape, page_size: "A8", skip_page_creation: false} end end