Skip to content

Commit

Permalink
Make @__options__ to collect renamed keys with defaults and coercions
Browse files Browse the repository at this point in the history
  • Loading branch information
nepalez committed Mar 5, 2017
1 parent e8f62a9 commit 363ba5c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
12 changes: 8 additions & 4 deletions lib/dry/initializer/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def clear_method(mixin, name)
def code
<<-RUBY.gsub(/^ +\|/, "")
|def __initialize__(#{initializer_signatures})
| @__options__ = #{defined_options}
| @__options__ = {}
|#{initializer_presetters}
|#{initializer_setters}
|#{initializer_postsetters}
|end
|private :__initialize__
|private :__defaults__
Expand Down Expand Up @@ -72,9 +73,8 @@ def initializer_signatures

def initializer_presetters
dups = duplications
attributes
.map { |a| " #{a.presetter}" if dups.include? a.target }
.compact.uniq.join("\n")
attributes.map { |a| " #{a.presetter}" if dups.include? a.target }
.compact.uniq.join("\n")
end

def initializer_setters
Expand All @@ -84,6 +84,10 @@ def initializer_setters
end.compact.uniq.join("\n")
end

def initializer_postsetters
attributes.map { |a| " #{a.postsetter}" }.compact.uniq.join("\n")
end

def defined_options
if @options.any?
keys = @options.map(&:target).join(" ")
Expand Down
5 changes: 5 additions & 0 deletions lib/dry/initializer/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def fast_setter
"Dry::Initializer::UNDEFINED"
end

def postsetter
"@__options__[:#{target}] = @#{target}" \
" unless @#{target} == Dry::Initializer::UNDEFINED"
end

# part of __defaults__
def default_hash
super :option
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def fast_setter
safe_setter
end

def postsetter; end

# part of __defaults__
def default_hash
super :param
Expand Down
18 changes: 7 additions & 11 deletions spec/options_var_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@ class Test::Foo
before do
class Test::Foo
extend Dry::Initializer::Mixin
param :foo
option :bar, optional: true
option :foo
option :bar, default: proc { 1 }
option :baz, optional: true
option :qux, proc(&:to_s), as: :quxx
end
end

it "is set to empty hash if no options assigned" do
subject = Test::Foo.new(1)

expect(subject.instance_variable_get(:@__options__)).to eq({})
end

it "slices allowed options only" do
subject = Test::Foo.new(1, baz: :QUX, qux: :BAZ)
it "collects coerced and renamed options with default values" do
subject = Test::Foo.new(foo: :FOO, qux: :QUX)

expect(subject.instance_variable_get(:@__options__)).to eq({ baz: :QUX })
expect(subject.instance_variable_get(:@__options__))
.to eq({ foo: :FOO, bar: 1, quxx: "QUX" })
end
end
end

0 comments on commit 363ba5c

Please sign in to comment.