Skip to content

Commit

Permalink
Added Array#extract_options.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Oct 2, 2015
1 parent fc7891e commit 9cb8967
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -5,6 +5,7 @@
== 1.4.0 [unreleased]

* Added File.open_file.
* Added Array#extract_options.

== 1.3.0 [2015-07-07]

Expand Down
5 changes: 5 additions & 0 deletions lib/nuggets/array/extract_options.rb
@@ -0,0 +1,5 @@
require 'nuggets/array/extract_options_mixin'

class Array
include Nuggets::Array::ExtractOptionsMixin
end
49 changes: 49 additions & 0 deletions lib/nuggets/array/extract_options_mixin.rb
@@ -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
47 changes: 47 additions & 0 deletions spec/nuggets/array/extract_options_spec.rb
@@ -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

0 comments on commit 9cb8967

Please sign in to comment.