Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc7891e
commit 9cb8967
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
== 1.4.0 [unreleased] | ||
|
||
* Added File.open_file. | ||
* Added Array#extract_options. | ||
|
||
== 1.3.0 [2015-07-07] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'nuggets/array/extract_options_mixin' | ||
|
||
class Array | ||
include Nuggets::Array::ExtractOptionsMixin | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#-- | ||
############################################################################### | ||
# # | ||
# nuggets -- Extending Ruby # | ||
# # | ||
# Copyright (C) 2007-2015 Jens Wille # | ||
# # | ||
# Authors: # | ||
# Jens Wille <jens.wille@gmail.com> # | ||
# # | ||
# nuggets is free software; you can redistribute it and/or modify it under # | ||
# the terms of the GNU Affero General Public License as published by the Free # | ||
# Software Foundation; either version 3 of the License, or (at your option) # | ||
# any later version. # | ||
# # | ||
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY # | ||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # | ||
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # | ||
# more details. # | ||
# # | ||
# You should have received a copy of the GNU Affero General Public License # | ||
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. # | ||
# # | ||
############################################################################### | ||
#++ | ||
|
||
module Nuggets | ||
class Array | ||
module ExtractOptionsMixin | ||
|
||
# call-seq: | ||
# array.extract_options([default]) -> aHash or +default+ | ||
# | ||
# Returns options hash from _array_ or +default+. | ||
def extract_options(default = {}) | ||
last.is_a?(::Hash) ? last : default | ||
end | ||
|
||
# call-seq: | ||
# array.extract_options!([default]) -> aHash or +default+ | ||
# | ||
# Extracts options hash from _array_ or returns +default+. | ||
def extract_options!(default = {}) | ||
last.is_a?(::Hash) ? pop : default | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'nuggets/array/extract_options' | ||
|
||
describe_extended Array, Nuggets::Array::ExtractOptionsMixin do | ||
|
||
example { expect([].extract_options).to eq({}) } | ||
|
||
example { expect([].extract_options(42)).to eq(42) } | ||
|
||
example { expect([].extract_options(nil)).to eq(nil) } | ||
|
||
example { expect([42].extract_options).to eq({}) } | ||
|
||
example { expect([foo: 42].extract_options).to eq(foo: 42) } | ||
|
||
example { expect([23, foo: 42].extract_options).to eq(foo: 42) } | ||
|
||
example { expect([{ foo: 42 }, 23].extract_options).to eq({}) } | ||
|
||
example { expect([23, 42, {}].extract_options).to eq({}) } | ||
|
||
example { expect([23, 42, {}].extract_options(nil)).to eq({}) } | ||
|
||
example { expect([23, 42].extract_options).to eq({}) } | ||
|
||
example { expect([23, 42].extract_options(nil)).to eq(nil) } | ||
|
||
example { | ||
a = [23, foo: 42] | ||
|
||
expect(a.extract_options).to eq(foo: 42) | ||
expect(a).to eq([23, foo: 42]) | ||
|
||
expect(a.extract_options!).to eq(foo: 42) | ||
expect(a).to eq([23]) | ||
} | ||
|
||
example { | ||
a = [{ foo: 42 }, 23] | ||
|
||
expect(a.extract_options).to eq({}) | ||
expect(a).to eq([{ foo: 42 }, 23]) | ||
|
||
expect(a.extract_options!).to eq({}) | ||
expect(a).to eq([{ foo: 42 }, 23]) | ||
} | ||
|
||
end |