diff --git a/lib/tty/option/params.rb b/lib/tty/option/params.rb index a583892..4b120ea 100644 --- a/lib/tty/option/params.rb +++ b/lib/tty/option/params.rb @@ -8,25 +8,36 @@ class Params extend Forwardable def_delegators :@parameters, - :keys, :key?, :has_key?, :member?, :value?, :has_value?, :empty?, - :include?, :each_key, :each_value + :keys, :key?, :has_key?, :member?, :value?, :has_value?, + :empty?, :include?, :each_key, :each_value def initialize(parameters = {}) @parameters = parameters + + @parameters.default_proc = ->(hash, key) do + return hash[key] if hash.key?(key) + + case key + when Symbol + hash[key.to_s] if hash.key?(key.to_s) + when String + hash[key.to_sym] if hash.key?(key.to_sym) + end + end end # Access a given value for a key # # @api public def [](key) - @parameters[key.to_sym] + @parameters[key] end # Assign value to a key # # @api public def []=(key, value) - @parameters[key.to_sym] = value + @parameters[key] = value end # Access a given value for a key @@ -49,6 +60,7 @@ def merge!(other_params) def ==(other) return false unless other.kind_of?(TTY::Option::Params) + @parameters == other.to_h end alias eql? == diff --git a/spec/unit/params_spec.rb b/spec/unit/params_spec.rb index 707dd54..105f7e2 100644 --- a/spec/unit/params_spec.rb +++ b/spec/unit/params_spec.rb @@ -2,10 +2,12 @@ RSpec.describe TTY::Option::Params do it "allows indifferent access to keys via has like syntax" do - params = described_class.new({foo: "bar"}) + params = described_class.new({foo: "bar", "baz" => :qux}) expect(params[:foo]).to eq("bar") expect(params["foo"]).to eq("bar") + expect(params[:baz]).to eq(:qux) + expect(params["baz"]).to eq(:qux) end it "allows indifferent access to keys via fetch" do