Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
ruby
提交
28e60b00
R
ruby
项目概览
张重言
/
ruby
通知
3
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
ruby
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
28e60b00
编写于
9月 14, 2020
作者:
M
Marc-Andre Lafortune
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[ruby/ostruct] Revert recent changes
This reverts commit
e026e186..12a2e32d
.
上级
867f0c67
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
28 addition
and
64 deletion
+28
-64
lib/ostruct.rb
lib/ostruct.rb
+28
-48
test/ostruct/test_ostruct.rb
test/ostruct/test_ostruct.rb
+0
-16
未找到文件。
lib/ostruct.rb
浏览文件 @
28e60b00
...
...
@@ -36,10 +36,9 @@
# Hash keys with spaces or characters that could normally not be used for
# method calls (e.g. <code>()[]*</code>) will not be immediately available
# on the OpenStruct object as a method for retrieval or assignment, but can
# still be reached through the Object#send method
or using []
.
# still be reached through the Object#send method.
#
# measurements = OpenStruct.new("length (in inches)" => 24)
# measurements[:"length (in inches)"] # => 24
# measurements.send("length (in inches)") # => 24
#
# message = OpenStruct.new(:queued? => true)
...
...
@@ -62,7 +61,8 @@
# first_pet # => #<OpenStruct name="Rowdy">
# first_pet == second_pet # => true
#
# == Caveats
#
# == Implementation
#
# An OpenStruct utilizes Ruby's method lookup structure to find and define the
# necessary methods for properties. This is accomplished through the methods
...
...
@@ -71,41 +71,10 @@
# This should be a consideration if there is a concern about the performance of
# the objects that are created, as there is much more overhead in the setting
# of these properties compared to using a Hash or a Struct.
# Creating an open struct from a small Hash and accessing a few of the
# entries can be 200 times slower than accessing the hash directly.
#
# This may also be the source of incompatibilities between Ruby versions:
#
# o = OpenStruct.new
# o.then # => nil in Ruby < 2.6, enumerator for Ruby >= 2.6
#
# Builtin methods may be overwritten this way, which may be a source of bugs
# or security issues:
#
# o = OpenStruct.new
# o.methods # => [:to_h, :marshal_load, :marshal_dump, :each_pair, ...
# o.methods = [:foo, :bar]
# o.methods # => [:foo, :bar]
#
# To help remedy clashes, OpenStruct uses only protected/private methods ending with `!`
# and defines aliases for builtin public methods by adding a `!`:
#
# o = OpenStruct.new(make: 'Bentley', class: :luxury)
# o.class # => :luxury
# o.class! # => OpenStruct
#
# It is recommended (but not enforced) to not use fields ending in `!`.
#
# For all these reasons, consider not using OpenStruct at all.
#
class
OpenStruct
VERSION
=
"0.2.0"
instance_methods
.
each
do
|
method
|
new_name
=
"
#{
method
}
!"
alias_method
new_name
,
method
end
#
# Creates a new OpenStruct object. By default, the resulting OpenStruct
# object will have no attributes.
...
...
@@ -124,16 +93,18 @@ def initialize(hash=nil)
@table
=
{}
if
hash
hash
.
each_pair
do
|
k
,
v
|
self
[
k
]
=
v
k
=
k
.
to_sym
@table
[
k
]
=
v
new_ostruct_member!
(
k
)
end
end
end
# Duplicates an OpenStruct object's Hash table.
def
initialize_copy
(
orig
)
# :nodoc:
orig
.
table
.
each_key
{
|
key
|
new_ostruct_member!
(
key
)}
super
@table
=
@table
.
dup
@table
.
each_key
{
|
key
|
new_ostruct_member!
(
key
)}
end
#
...
...
@@ -190,35 +161,46 @@ def marshal_dump
# Provides marshalling support for use by the Marshal library.
#
def
marshal_load
(
x
)
x
.
each_key
{
|
key
|
new_ostruct_member!
(
key
)}
@table
=
x
@table
.
each_key
{
|
key
|
new_ostruct_member!
(
key
)}
end
#
# 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?
# :nodoc:
begin
@modifiable
=
true
rescue
raise
FrozenError
,
"can't modify frozen
#{
self
.
class
}
"
,
caller
(
3
)
end
@table
end
private
:modifiable?
#
# Used internally to defined properties on the
# 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
)
# :nodoc:
unless
@table
.
key?
(
name
)
name
=
name
.
to_sym
unless
respond_to?
(
name
)
define_singleton_method
(
name
)
{
@table
[
name
]
}
define_singleton_method
(
"
#{
name
}
="
)
{
|
x
|
@table
[
name
]
=
x
}
define_singleton_method
(
"
#{
name
}
="
)
{
|
x
|
modifiable?
[
name
]
=
x
}
end
name
end
private
:new_ostruct_member!
def
freeze
@table
.
freeze
super
end
def
method_missing
(
mid
,
*
args
)
# :nodoc:
len
=
args
.
length
if
mname
=
mid
[
/.*(?==\z)/m
]
if
len
!=
1
raise
ArgumentError
,
"wrong number of arguments (given
#{
len
}
, expected 1)"
,
caller
(
1
)
end
self
[
mname
]
=
args
[
0
]
modifiable?
[
new_ostruct_member!
(
mname
)]
=
args
[
0
]
elsif
len
==
0
elsif
@table
.
key?
(
mid
)
raise
ArgumentError
,
"wrong number of arguments (given
#{
len
}
, expected 0)"
...
...
@@ -258,9 +240,7 @@ def [](name)
# person.age # => 42
#
def
[]=
(
name
,
value
)
name
=
name
.
to_sym
new_ostruct_member!
(
name
)
@table
[
name
]
=
value
modifiable?
[
new_ostruct_member!
(
name
)]
=
value
end
# :call-seq:
...
...
test/ostruct/test_ostruct.rb
浏览文件 @
28e60b00
...
...
@@ -225,25 +225,9 @@ def foo
end
end
def
test_access_undefined
os
=
OpenStruct
.
new
assert_nil
os
.
foo
end
def
test_overriden_private_methods
os
=
OpenStruct
.
new
(
puts: :foo
,
format: :bar
)
assert_equal
(
:foo
,
os
.
puts
)
assert_equal
(
:bar
,
os
.
format
)
end
def
test_overriden_public_methods
os
=
OpenStruct
.
new
(
method: :foo
,
class: :bar
)
assert_equal
(
:foo
,
os
.
method
)
assert_equal
(
:bar
,
os
.
class
)
end
def
test_access_original_methods
os
=
OpenStruct
.
new
(
method: :foo
)
assert_equal
(
os
.
object_id
,
os
.
method!
(
:object_id
).
call
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录