mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
09fefc2e20
commit
7c89ca5431
2 changed files with 24 additions and 9 deletions
|
@ -1,3 +1,9 @@
|
|||
Thu Mar 3 14:09:00 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/ostruct.rb (modifiable?, new_ostruct_member!, table!):
|
||||
rename methods for internal use with suffixes and make private,
|
||||
[ruby-core:71069] [Bug #11587]
|
||||
|
||||
Wed Mar 2 16:28:48 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* vm_eval.c (method_missing): call by found method entry and get
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue