Skip to content

Commit

Permalink
Remove usage of openstruct for config object
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Sep 12, 2024
1 parent 1a6731d commit b3996ac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

* `Unreleased` - [View Diff](https://github.com/cortiz/prawn-rails/compare/v1.4.2...master)
- [#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`
Expand Down
25 changes: 20 additions & 5 deletions lib/prawn-rails/config.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/prawn-rails/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit b3996ac

Please sign in to comment.