diff --git a/lib/telephone/service.rb b/lib/telephone/service.rb index c8bb946..295aeac 100644 --- a/lib/telephone/service.rb +++ b/lib/telephone/service.rb @@ -20,6 +20,8 @@ class Service # Primary responsibility of initialize is to instantiate the # attributes of the service object with the expected values. def initialize(attributes = {}) + attributes = attributes.transform_keys(&:to_sym) + attributes.each do |key, value| send("#{key}=", value) end diff --git a/spec/telephone_spec.rb b/spec/telephone_spec.rb index 940d3e9..8c9e356 100644 --- a/spec/telephone_spec.rb +++ b/spec/telephone_spec.rb @@ -152,6 +152,19 @@ def call expect(subject.new(foo: "baz").foo).to be "baz" end + it "accepts string keys" do + expect(subject.new("foo" => "baz").foo).to eq "baz" + end + + it "handles string keys with callable defaults" do + service = Class.new(Telephone::Service) do + argument :name, default: "Default" + argument :message, default: -> { "Hello, #{name}!" } + end + + expect(service.new("name" => "Custom").message).to eq "Hello, Custom!" + end + context "if there is a required argument" do before { subject.public_send(:argument, :required_field, required: true) }