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

Remove usage of openstruct for config object #51

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
25 changes: 21 additions & 4 deletions lib/prawn-rails/config.rb
Original file line number Diff line number Diff line change
@@ -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
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
Loading