Skip to content

Commit

Permalink
Implemented Lotus::Utils::LoadPaths#freeze in order to prevent modifi…
Browse files Browse the repository at this point in the history
…cation after the object has been frozen
  • Loading branch information
jodosha committed Jun 9, 2014
1 parent 2b8d8d5 commit 32c15d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/lotus/utils/load_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ def each(&blk)
#
# @return [Lotus::Utils::LoadPaths] self
#
# @raise [RuntimeError] if the object was previously frozen
#
# @since 0.2.0
#
# @see http://ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
# @see Lotus::Utils::Kernel.Pathname
# @see Lotus::Utils::LoadPaths#freeze
#
# @example Basic usage
# require 'lotus/utils/load_paths'
Expand Down Expand Up @@ -111,6 +114,26 @@ def push(*paths)
end

alias_method :<<, :push

# It freezes the object by preventing further modifications.
#
# @since 0.2.0
#
# @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-freeze
#
# @example
# require 'lotus/utils/load_paths'
#
# paths = Lotus::Utils::LoadPaths.new
# paths.freeze
#
# paths.frozen? # => true
#
# paths.push '.' # => RuntimeError
def freeze
super
@paths.freeze
end
end
end
end
16 changes: 16 additions & 0 deletions test/load_paths_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,20 @@ def include?(object)
paths2.wont_include '..'
end
end

describe '#freeze' do
it 'freezes the object' do
paths = Lotus::Utils::LoadPaths.new
paths.freeze

paths.must_be :frozen?
end

it "doesn't allow to push paths" do
paths = Lotus::Utils::LoadPaths.new
paths.freeze

-> { paths.push '.' }.must_raise RuntimeError
end
end
end

0 comments on commit 32c15d6

Please sign in to comment.