Skip to content

Commit

Permalink
Make config object method_missing more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Sep 12, 2024
1 parent 4f05404 commit cb73450
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
14 changes: 10 additions & 4 deletions lib/prawn-rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ class Config < HashWithIndifferentAccess
def method_missing(method_name, *args)
# to replace behaviour of previous openstruct-based configuration object

method_name = method_name.to_s
if method_name.to_s.end_with?("=")
if args.size >= 2
super # raise normal exception
end

if method_name.end_with?("=")
key = method_name.sub(/=$/, "")
key = method_name.to_s.sub(/=$/, "")
self[key] = args.first
else
self[method_name]
if args.any?
super # raise normal exception
else
self[method_name]
end
end
end
end
Expand Down
31 changes: 25 additions & 6 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
class ConfigTest < ActiveSupport::TestCase

def setup
@original_config = PrawnRails.config.to_h
@original_config = PrawnRails.config.clone
end

def teardown
PrawnRails.config do |config|
config.to_h.keys.each do |key|
config.delete_field(key)
config.keys.each do |key|
config.delete(key)
end

@original_config.to_h.each do |k,v|
@original_config.each do |k,v|
config[k] = v
end
end
end

test "has a default config" do
assert_equal PrawnRails.config.to_h.symbolize_keys, {page_layout: :portrait, page_size: "A4", skip_page_creation: false}
assert_equal PrawnRails.config.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,26 @@ def teardown
config.page_size = "A8"
end

assert_equal PrawnRails.config.to_h.symbolize_keys, {page_layout: :landscape, page_size: "A8", skip_page_creation: false}
assert_equal PrawnRails.config.symbolize_keys, {page_layout: :landscape, page_size: "A8", skip_page_creation: false}
end

test "allows reading any hash value via method syntax" do
assert_equal PrawnRails.config.foo, nil
end

test "allows writing any hash value via method syntax" do
PrawnRails.config.foo = "bar"
assert_equal PrawnRails.config.foo, "bar"
end

test "does not allow unknown methods with arguments" do
assert_raises NoMethodError do
PrawnRails.config.foo("bar")
end

assert_raises NoMethodError do
PrawnRails.config.send("foo=", "bar", "baz")
end
end

end
4 changes: 2 additions & 2 deletions test/unit/document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def read_pdf_str(str)

class ConfigTest < DocumentTest
def setup
@original_config = PrawnRails.config.to_h
@original_config = PrawnRails.config.clone
end

def teardown
PrawnRails.config do |config|
config.to_h.keys.each do |key|
config.delete_field(key)
config.delete(key)
end

@original_config.to_h.each do |k,v|
Expand Down

0 comments on commit cb73450

Please sign in to comment.