Skip to content

Commit

Permalink
Merge pull request #7 from Kaligo/feature/options-declaration-default…
Browse files Browse the repository at this point in the history
…-nil

[Feature] Allow default: nil option without required: false for OptionsDeclaration.option
  • Loading branch information
Drenmi authored Mar 11, 2021
2 parents 53f7df3 + 2b4231b commit 9558067
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.5.3

### New features

- Allow `default: nil` to be used without needing `required: false` for the
`OptionsDeclaration.option` method.

## 0.5.2

### New features
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
stimpack (0.5.2)
stimpack (0.5.3)
activesupport (~> 6.1)

GEM
Expand Down
22 changes: 19 additions & 3 deletions lib/stimpack/options_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def self.extended(klass)
# option :user
# end
#
def option(*identifiers, required: true, default: nil, private_reader: true) # rubocop:disable Metrics/MethodLength
def option(*identifiers, required: true, default: Option::MISSING_VALUE, private_reader: true) # rubocop:disable Metrics/MethodLength
self.options_configuration = options_configuration.merge(
identifiers.map do |identifier|
[
Expand Down Expand Up @@ -87,6 +87,10 @@ def required_options
def optional_options
options_configuration.select { |_, option| option.optional? }.keys
end

def default_options
options_configuration.select { |_, option| option.default? }.keys
end
end

# Injects an initializer that assigns options and proxies the call to any
Expand Down Expand Up @@ -146,25 +150,37 @@ def self.included(klass)
end

class Option
MISSING_VALUE = "__missing__"

def initialize(name, required:, default:)
@name = name
@default = default
@required = required
end

attr_reader :name, :default, :required
attr_reader :name

def default_value
return nil unless default?

default.respond_to?(:call) ? default.() : default
end

def required?
required && default.nil?
required && !default?
end

def optional?
!required?
end

def default?
default != MISSING_VALUE
end

private

attr_reader :default, :required
end
end
end
2 changes: 1 addition & 1 deletion lib/stimpack/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Stimpack
VERSION = "0.5.2"
VERSION = "0.5.3"
end
14 changes: 10 additions & 4 deletions spec/stimpack/options_declaration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,38 @@
option :bar, required: false
option :baz, private_reader: false
option :qux, default: "Foo"
option :quuz, default: nil
option :quux, default: -> { "Bar" }
end
end

describe ".option" do
it { expect(service.options_configuration.size).to eq(5) }
it { expect(service.options_configuration.size).to eq(6) }
it { expect(service.options_configuration.values).to all(be_a(described_class::Option)) }

describe "private_reader (option)" do
let(:public_instance_methods) { service.public_instance_methods(false) }
let(:private_instance_methods) { service.private_instance_methods(false) }

it { expect(public_instance_methods).to contain_exactly(:baz) }
it { expect(private_instance_methods).to contain_exactly(:foo, :bar, :qux, :quux) }
it { expect(private_instance_methods).to contain_exactly(:foo, :bar, :qux, :quux, :quuz) }
end
end

describe ".options" do
it { expect(service.options).to contain_exactly(:foo, :bar, :baz, :qux, :quux) }
it { expect(service.options).to contain_exactly(:foo, :bar, :baz, :qux, :quux, :quuz) }
end

describe ".required_options" do
it { expect(service.required_options).to contain_exactly(:foo, :baz) }
end

describe ".optional_options" do
it { expect(service.optional_options).to contain_exactly(:bar, :qux, :quux) }
it { expect(service.optional_options).to contain_exactly(:bar, :qux, :quux, :quuz) }
end

describe ".default_options" do
it { expect(service.default_options).to contain_exactly(:qux, :quux, :quuz) }
end

describe "#initialize" do
Expand Down Expand Up @@ -86,6 +91,7 @@
context "when default option is assigned by omission" do
it { expect(instance.send(:qux)).to eq("Foo") }
it { expect(instance.send(:quux)).to eq("Bar") }
it { expect(instance.send(:quuz)).to eq(nil) }
end
end
end
Expand Down

0 comments on commit 9558067

Please sign in to comment.