From 99b126c54d134b9ad6939340aa49a1ec41ea3680 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Thu, 12 Sep 2024 08:56:59 -0700 Subject: [PATCH] Remove usage of openstruct for config object --- CHANGELOG.md | 1 + lib/prawn-rails/config.rb | 25 +++++++++++++++++++++---- lib/prawn-rails/document.rb | 2 +- test/unit/config_test.rb | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0860088..6326795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * `Unreleased` - [View Diff](https://github.com/cortiz/prawn-rails/compare/v1.4.2...master) - [#52](https://github.com/cortiz/prawn-rails/pull/52) - Add active_support as a dependency and use the active_support lazy load hooks + - [#51](https://github.com/cortiz/prawn-rails/pull/51) - Remove usage of openstruct for config object - Remove unnecessary method `get_metadata` from `PrawnRailsHelper` which was getting included into `ActionView::Base` - Remove unnecessary rescue in `PrawnRails.config` - Remove redundant method `PrawnRails::Document.extensions` diff --git a/lib/prawn-rails/config.rb b/lib/prawn-rails/config.rb index 3f8b2fa..15b7ab8 100644 --- a/lib/prawn-rails/config.rb +++ b/lib/prawn-rails/config.rb @@ -1,12 +1,29 @@ -require 'ostruct' +require "active_support/hash_with_indifferent_access" module PrawnRails - extend self + class Config < HashWithIndifferentAccess + def method_missing(method_name, *args) + # to replace behaviour of previous openstruct-based configuration object - @config = OpenStruct.new(page_layout: :portrait, page_size: "A4", skip_page_creation: false) + method_name = method_name.to_s + + 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