ostruct2 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.ruby +63 -0
- data/Config.rb +73 -0
- data/DEMO.rdoc +110 -0
- data/HISTORY.rdoc +8 -0
- data/README.rdoc +24 -0
- data/lib/ostruct2.rb +150 -0
- metadata +92 -0
data/.ruby
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
---
|
2
|
+
source:
|
3
|
+
- meta
|
4
|
+
authors:
|
5
|
+
- name: Trans
|
6
|
+
email: transfire@gmail.com
|
7
|
+
copyrights:
|
8
|
+
- holder: Rubyworks
|
9
|
+
year: '2010'
|
10
|
+
license: BSD-2-Clause
|
11
|
+
requirements:
|
12
|
+
- name: detroit
|
13
|
+
groups:
|
14
|
+
- build
|
15
|
+
development: true
|
16
|
+
- name: qed
|
17
|
+
groups:
|
18
|
+
- test
|
19
|
+
development: true
|
20
|
+
- name: ae
|
21
|
+
groups:
|
22
|
+
- test
|
23
|
+
development: true
|
24
|
+
dependencies: []
|
25
|
+
alternatives: []
|
26
|
+
conflicts: []
|
27
|
+
repositories:
|
28
|
+
- uri: git://github.com/rubyworks/ostruct2.git
|
29
|
+
scm: git
|
30
|
+
name: upstream
|
31
|
+
resources:
|
32
|
+
- uri: http://rubyworks.github.com/ostruct2
|
33
|
+
label: Website
|
34
|
+
type: home
|
35
|
+
- uri: http://rubydoc.info/gems/ostruct2/frames
|
36
|
+
label: Documentation
|
37
|
+
type: docs
|
38
|
+
- uri: http://github.com/rubyworks/ostruct2
|
39
|
+
label: Source Code
|
40
|
+
type: code
|
41
|
+
- uri: http://groups.google.com/group/rubyworks-mailinglist
|
42
|
+
label: Mailing List
|
43
|
+
type: mail
|
44
|
+
- uri: http://chat.us.freenode.net/rubyworks
|
45
|
+
label: IRC Channel
|
46
|
+
type: chat
|
47
|
+
categories: []
|
48
|
+
extra: {}
|
49
|
+
load_path:
|
50
|
+
- lib
|
51
|
+
revision: 0
|
52
|
+
created: '2010-04-21'
|
53
|
+
summary: A Better OpenStruct
|
54
|
+
title: OStruct2
|
55
|
+
version: 0.1.0
|
56
|
+
name: ostruct2
|
57
|
+
description: ! 'OStruct2 is a reimplementation of Ruby''s standard ostruct.rb library.
|
58
|
+
|
59
|
+
This new OpenStruct class addresses issues the original has with conflicting
|
60
|
+
|
61
|
+
member names and cloning.'
|
62
|
+
organization: Rubyworks
|
63
|
+
date: '2012-05-20'
|
data/Config.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# QED test coverage report using SimpleCov.
|
5
|
+
#
|
6
|
+
# Use `$properties.coverage_folder` to set directory in which to store
|
7
|
+
# coverage report this defaults to `log/coverage`.
|
8
|
+
#
|
9
|
+
# IMPORTANT! Unfortunately this will not give us a reliable report
|
10
|
+
# b/c QED uses the RC gem, so SimpleCov can't differentiate the two.
|
11
|
+
#
|
12
|
+
config 'qed', profile: 'cov' do
|
13
|
+
#puts "QED w/coverage!"
|
14
|
+
dir = $properties.coverage_folder
|
15
|
+
require 'simplecov'
|
16
|
+
SimpleCov.command_name 'QED'
|
17
|
+
SimpleCov.start do
|
18
|
+
coverage_dir(dir || 'log/coverage')
|
19
|
+
#add_group "Label", "lib/qed/directory"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Pry
|
25
|
+
#
|
26
|
+
config 'pry' do
|
27
|
+
puts "RC on Pry!"
|
28
|
+
$LOAD_PATH.unshift('lib')
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Rake tasks
|
33
|
+
#
|
34
|
+
config 'rake' do
|
35
|
+
desc 'run unit tests'
|
36
|
+
task 'test' do
|
37
|
+
sh 'qed'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
=begin
|
42
|
+
#
|
43
|
+
# Detroit assembly.
|
44
|
+
#
|
45
|
+
config 'detroit' do
|
46
|
+
service :email do |s|
|
47
|
+
s.mailto = ['ruby-talk@ruby-lang.org',
|
48
|
+
'rubyworks-mailinglist@googlegroups.com']
|
49
|
+
end
|
50
|
+
|
51
|
+
service :gem do |s|
|
52
|
+
s.gemspec = 'pkg/ostruct2.gemspec'
|
53
|
+
end
|
54
|
+
|
55
|
+
service :github do |s|
|
56
|
+
s.folder = 'web'
|
57
|
+
end
|
58
|
+
|
59
|
+
service :dnote do |s|
|
60
|
+
s.title = 'Source Notes'
|
61
|
+
s.output = 'log/notes.html'
|
62
|
+
end
|
63
|
+
|
64
|
+
service :locat do |s|
|
65
|
+
s.output = 'log/locat.html'
|
66
|
+
end
|
67
|
+
|
68
|
+
service :vclog do |s|
|
69
|
+
s.output = ['log/history.html',
|
70
|
+
'log/changes.html']
|
71
|
+
end
|
72
|
+
end
|
73
|
+
=end
|
data/DEMO.rdoc
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# OpenStruct
|
2
|
+
|
3
|
+
The constructor can take a priming hash.
|
4
|
+
|
5
|
+
o = OpenStruct.new(:a=>1,:b=>2)
|
6
|
+
o.a #=> 1
|
7
|
+
o.b #=> 2
|
8
|
+
|
9
|
+
It can also take a default procedure, just like an Hash.
|
10
|
+
|
11
|
+
o = OpenStruct.new{ |h,k| h[k] = {} }
|
12
|
+
o.a #=> {}
|
13
|
+
|
14
|
+
Common usage of an OpenStruct is via the missing method dynamic calls.
|
15
|
+
An entry can be made by making an assignment and read back via the
|
16
|
+
same method call.
|
17
|
+
|
18
|
+
o = OpenStruct.new
|
19
|
+
o.a = 1
|
20
|
+
o.a #=> 1
|
21
|
+
|
22
|
+
Key existence can also be checked by adding a question mark.
|
23
|
+
|
24
|
+
o.a? #=> true
|
25
|
+
|
26
|
+
OpenStruct "circa 2" has a CRUDified design. There are only a few primary
|
27
|
+
methods that handle access to the underlying table and all other methods
|
28
|
+
route through these. Primarily they are `#read!`, `#store!`, `#delete!`
|
29
|
+
as well as `#key?` and `#keys!`.
|
30
|
+
|
31
|
+
o = OpenStruct.new
|
32
|
+
o.store!(:a, 1)
|
33
|
+
o.read!(:a) #=> 1
|
34
|
+
o.key?(:a) #=> true
|
35
|
+
o.keys! #=> [:a]
|
36
|
+
o.delete!(:a)
|
37
|
+
o.empty? #=> true
|
38
|
+
|
39
|
+
OpenStruct offers a number of methods to access the underlying table.
|
40
|
+
Each of these ends in a exlimation mark, and include `#fetch!`, `#update!`,
|
41
|
+
and `merge!`.
|
42
|
+
|
43
|
+
o = OpenStruct.new
|
44
|
+
o.update!(:a=>1, :b=>2)
|
45
|
+
o.fetch!(:a) #=> 1
|
46
|
+
|
47
|
+
Note that `#merge!` is akin to `Hash#merge`, not `Hash#merge!` --it does not
|
48
|
+
act in-place.
|
49
|
+
|
50
|
+
o = OpenStruct.new
|
51
|
+
x = o.merge!(:a=>1, :b=>2)
|
52
|
+
o.a #=> nil
|
53
|
+
x.a #=> 1
|
54
|
+
|
55
|
+
OpenStruct also supports Hash-like read and write operators, #[] and #[]=.
|
56
|
+
|
57
|
+
o = OpenStruct.new
|
58
|
+
o[:a] = 1
|
59
|
+
o[:a] #=> 1
|
60
|
+
o.a #=> 1
|
61
|
+
|
62
|
+
The OpenStruct object can be converted to a simple Hash, via `#to_h`.
|
63
|
+
|
64
|
+
o = OpenStruct.new(:a=>1,:b=>2)
|
65
|
+
o.to_h #=> {:a=>1, :b=>2}
|
66
|
+
|
67
|
+
Iteration can be achieved with `#each!`.
|
68
|
+
|
69
|
+
o = OpenStruct.new(:a=>1,:b=>2)
|
70
|
+
a = {}
|
71
|
+
o.each! do |k,v|
|
72
|
+
a[k] = v
|
73
|
+
end
|
74
|
+
a #=> {:a=>1, :b=>2}
|
75
|
+
|
76
|
+
Currently all Enumerable methods will work if suffixed with an exclemation mark. But they operate
|
77
|
+
directly on the underlying Hash rather than at the level of the "CRUDified" OpenStruct itself.
|
78
|
+
|
79
|
+
o = OpenStruct.new(:a=>1,:b=>2)
|
80
|
+
a = o.map!{ |k,v| [k,v] }
|
81
|
+
a #=> [[:a,1], [:b,2]]
|
82
|
+
|
83
|
+
In most cases this will work fine. But it may cause some minor discrepencies in how Enumerable
|
84
|
+
methods work presently and how ultimatley they should work. A fix is a bit tricky so this is an
|
85
|
+
endeavor left a future release. In the rare cases where it does matters, proper enumeratorion
|
86
|
+
can be assured by calling `#to_enum` first.
|
87
|
+
|
88
|
+
o = OpenStruct.new(:a=>1,:b=>2)
|
89
|
+
a = {}
|
90
|
+
o.to_enum.each do |k,v|
|
91
|
+
a[k] = v
|
92
|
+
end
|
93
|
+
a #=> {:a=>1, :b=>2}
|
94
|
+
|
95
|
+
OpenStruct also has a unique method called `#key!` that is used to check for an key entry,
|
96
|
+
and raise a KeyError if it not found.
|
97
|
+
|
98
|
+
o = OpenStruct.new
|
99
|
+
expect KeyError do
|
100
|
+
o.key!(:x)
|
101
|
+
end
|
102
|
+
|
103
|
+
Lastly, OpenStruct has a convenient feature for creating cascading OpenStructs.
|
104
|
+
|
105
|
+
o = OpenStruct.cascade
|
106
|
+
o.x.a = 1
|
107
|
+
o.x.a #=> 1
|
108
|
+
OpenStruct.assert === o.a
|
109
|
+
|
110
|
+
|
data/HISTORY.rdoc
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
= OpenStruct 2
|
2
|
+
|
3
|
+
{Website}[http://rubyworks.github.com/ostruct2] /
|
4
|
+
{Report Issue}[http://github.com/rubyworks/ostruct2/issues] /
|
5
|
+
{Source Code}[http://github.com/rubyworks/ostruct2]
|
6
|
+
|
7
|
+
{<img src="https://secure.travis-ci.org/rubyworks/ostruct2.png" />}[http://travis-ci.org/rubyworks/ostruct2]
|
8
|
+
|
9
|
+
|
10
|
+
== Description
|
11
|
+
|
12
|
+
OStruct2 is a reimplementation of Ruby's standard ostruct.rb library.
|
13
|
+
This new OpenStruct class addresses issues the original has with conflicting
|
14
|
+
member names and cloning.
|
15
|
+
|
16
|
+
|
17
|
+
== Copyrights
|
18
|
+
|
19
|
+
Copyright (c) 2010 Rubyworks
|
20
|
+
|
21
|
+
OStruct2 is distributed under the *BSD-2-Clause* license.
|
22
|
+
|
23
|
+
See LICENSE.txt for detatils.
|
24
|
+
|
data/lib/ostruct2.rb
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
# A better OpenStruct class.
|
2
|
+
#
|
3
|
+
class OpenStruct < BasicObject
|
4
|
+
|
5
|
+
class << self
|
6
|
+
#
|
7
|
+
# Create cascading OpenStruct.
|
8
|
+
#
|
9
|
+
def cascade(data=nil)
|
10
|
+
leet = lambda{ |h,k| OpenStruct.new(&leet) }
|
11
|
+
new(&leet)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Is there a better name for #auto?
|
15
|
+
#alias :autorenew :cascade
|
16
|
+
|
17
|
+
# Original name for #cascade method.
|
18
|
+
alias :auto :cascade
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
def initialize(data=nil, &block)
|
23
|
+
@table = ::Hash.new(&block)
|
24
|
+
update!(data || {})
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
def method_missing(sym, *args, &blk)
|
29
|
+
str = sym.to_s
|
30
|
+
type = str[-1,1]
|
31
|
+
name = str.chomp('=').chomp('!').chomp('?')
|
32
|
+
|
33
|
+
case type
|
34
|
+
when '='
|
35
|
+
store!(name, args.first)
|
36
|
+
when '!'
|
37
|
+
@table.public_send(name, *args, &blk)
|
38
|
+
when '?'
|
39
|
+
key?(name)
|
40
|
+
else
|
41
|
+
read!(name)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# CRUD method for listing all keys.
|
46
|
+
def keys!
|
47
|
+
@table.keys
|
48
|
+
end
|
49
|
+
|
50
|
+
# Also a CRUD method like #read!, but for checking for the existence of a key.
|
51
|
+
def key?(key)
|
52
|
+
@table.key?(key.to_sym)
|
53
|
+
end
|
54
|
+
|
55
|
+
# The CRUD method for read.
|
56
|
+
def read!(key)
|
57
|
+
@table[key.to_sym]
|
58
|
+
end
|
59
|
+
|
60
|
+
# The CRUD method for create and update.
|
61
|
+
def store!(key, value)
|
62
|
+
@table[key.to_sym] = value
|
63
|
+
end
|
64
|
+
|
65
|
+
# The CRUD method for destroy.
|
66
|
+
def delete!(key)
|
67
|
+
@table.delete(key.to_sym)
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# Like #read but will raise a KeyError if key is not found.
|
72
|
+
#
|
73
|
+
def fetch!(key)
|
74
|
+
key!(key)
|
75
|
+
read!(key)
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
def [](key)
|
80
|
+
read!(key)
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
def []=(key, value)
|
85
|
+
store!(key, value)
|
86
|
+
end
|
87
|
+
|
88
|
+
# CRUDified each.
|
89
|
+
def each!
|
90
|
+
@table.each_key do |key|
|
91
|
+
yield(key, read!(key))
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# CRUDified update method.
|
96
|
+
def update!(other)
|
97
|
+
other.each do |k,v|
|
98
|
+
store!(k,v)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# IMPORTANT! This method does not act in-place like `Hash#merge!`,
|
104
|
+
# rather it works like `Hash#merge`.
|
105
|
+
#
|
106
|
+
def merge!(other)
|
107
|
+
# TODO: is there anyway to #dup a BasicObject subclass instance?
|
108
|
+
o = ::OpenStruct.new(@table, &@table.default_proc)
|
109
|
+
other.each do |k,v|
|
110
|
+
o.store!(k,v)
|
111
|
+
end
|
112
|
+
o
|
113
|
+
end
|
114
|
+
|
115
|
+
#
|
116
|
+
# Is the OpenStruct void of entries?
|
117
|
+
#
|
118
|
+
def empty?
|
119
|
+
@table.empty?
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Get a duplicate of the underlying table.
|
124
|
+
#
|
125
|
+
def to_h
|
126
|
+
@table.dup
|
127
|
+
end
|
128
|
+
|
129
|
+
#
|
130
|
+
def to_enum
|
131
|
+
::Enumerator.new(self, :each!)
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# If key is not present raise a KeyError.
|
136
|
+
#
|
137
|
+
def key!(key)
|
138
|
+
return key if key?(key)
|
139
|
+
::Kernel.raise ::KeyError, ("key not found: %s" % [key.inspect])
|
140
|
+
end
|
141
|
+
|
142
|
+
#
|
143
|
+
#
|
144
|
+
#
|
145
|
+
def inspect
|
146
|
+
"#<OpenStruct: #{@table.inspect}>"
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ostruct2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Trans
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: detroit
|
16
|
+
requirement: &26950880 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *26950880
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: qed
|
27
|
+
requirement: &26950340 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *26950340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: ae
|
38
|
+
requirement: &26949840 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *26949840
|
47
|
+
description: ! 'OStruct2 is a reimplementation of Ruby''s standard ostruct.rb library.
|
48
|
+
|
49
|
+
This new OpenStruct class addresses issues the original has with conflicting
|
50
|
+
|
51
|
+
member names and cloning.'
|
52
|
+
email:
|
53
|
+
- transfire@gmail.com
|
54
|
+
executables: []
|
55
|
+
extensions: []
|
56
|
+
extra_rdoc_files:
|
57
|
+
- HISTORY.rdoc
|
58
|
+
- DEMO.rdoc
|
59
|
+
- README.rdoc
|
60
|
+
files:
|
61
|
+
- .ruby
|
62
|
+
- lib/ostruct2.rb
|
63
|
+
- HISTORY.rdoc
|
64
|
+
- DEMO.rdoc
|
65
|
+
- README.rdoc
|
66
|
+
- Config.rb
|
67
|
+
homepage:
|
68
|
+
licenses:
|
69
|
+
- BSD-2-Clause
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.11
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: A Better OpenStruct
|
92
|
+
test_files: []
|