From e4e3ead316a3790f7ec4fd41d824d5b405dc19df Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 3 Mar 2016 05:09:02 +0000 Subject: ostruct.rb: make internal methods private * lib/ostruct.rb (modifiable?, new_ostruct_member!, table!): rename methods for internal use with suffixes and make private, [ruby-core:71069] [Bug #11587] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/ostruct.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'lib/ostruct.rb') diff --git a/lib/ostruct.rb b/lib/ostruct.rb index a95a0f1963..11eee24537 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -151,7 +151,7 @@ class OpenStruct # Used internally to check if the OpenStruct is able to be # modified before granting access to the internal Hash table to be modified. # - def modifiable + def modifiable? # :nodoc: begin @modifiable = true rescue @@ -159,6 +159,10 @@ class OpenStruct end @table end + private :modifiable? + + # ::Kernel.warn("#{caller(1, 1)[0]}: do not use OpenStruct#modifiable") + alias modifiable modifiable? # :nodoc: protected :modifiable # @@ -166,18 +170,22 @@ class OpenStruct # OpenStruct. It does this by using the metaprogramming function # define_singleton_method for both the getter method and the setter method. # - def new_ostruct_member(name) + def new_ostruct_member!(name) # :nodoc: name = name.to_sym unless singleton_class.method_defined?(name) define_singleton_method(name) { @table[name] } - define_singleton_method("#{name}=") { |x| modifiable[name] = x } + define_singleton_method("#{name}=") {|x| modifiable?[name] = x} end name end + private :new_ostruct_member! + + # ::Kernel.warn("#{caller(1, 1)[0]}: do not use OpenStruct#new_ostruct_member") + alias new_ostruct_member new_ostruct_member! # :nodoc: protected :new_ostruct_member def freeze - @table.each_key {|key| new_ostruct_member(key)} + @table.each_key {|key| new_ostruct_member!(key)} super end @@ -192,10 +200,10 @@ class OpenStruct if len != 1 raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) end - modifiable[new_ostruct_member(mname)] = args[0] + modifiable?[new_ostruct_member!(mname)] = args[0] elsif len == 0 if @table.key?(mid) - new_ostruct_member(mid) unless frozen? + new_ostruct_member!(mid) unless frozen? @table[mid] end else @@ -222,7 +230,7 @@ class OpenStruct # person.age # => 42 # def []=(name, value) - modifiable[new_ostruct_member(name)] = value + modifiable?[new_ostruct_member!(name)] = value end # @@ -294,6 +302,7 @@ class OpenStruct attr_reader :table # :nodoc: protected :table + alias table! table # # Compares this object and +other+ for equality. An OpenStruct is equal to @@ -302,7 +311,7 @@ class OpenStruct # def ==(other) return false unless other.kind_of?(OpenStruct) - @table == other.table + @table == other.table! end # @@ -312,7 +321,7 @@ class OpenStruct # def eql?(other) return false unless other.kind_of?(OpenStruct) - @table.eql?(other.table) + @table.eql?(other.table!) end # Compute a hash-code for this OpenStruct. -- cgit v1.2.3