rubysl-ostruct 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/ostruct.rb +1 -0
- data/lib/rubysl/ostruct.rb +2 -0
- data/lib/rubysl/ostruct/ostruct.rb +149 -0
- data/lib/rubysl/ostruct/version.rb +5 -0
- data/rubysl-ostruct.gemspec +21 -0
- data/spec/delete_field_spec.rb +28 -0
- data/spec/element_reference_spec.rb +20 -0
- data/spec/element_set_spec.rb +21 -0
- data/spec/equal_value_spec.rb +27 -0
- data/spec/fixtures/classes.rb +4 -0
- data/spec/frozen_spec.rb +39 -0
- data/spec/initialize_copy_spec.rb +25 -0
- data/spec/initialize_spec.rb +7 -0
- data/spec/inspect_spec.rb +7 -0
- data/spec/marshal_dump_spec.rb +8 -0
- data/spec/marshal_load_spec.rb +11 -0
- data/spec/method_missing_spec.rb +46 -0
- data/spec/new_ostruct_member_spec.rb +31 -0
- data/spec/new_spec.rb +19 -0
- data/spec/shared/inspect.rb +20 -0
- data/spec/table_spec.rb +17 -0
- data/spec/to_h_spec.rb +30 -0
- data/spec/to_s_spec.rb +7 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cec4600c9a826de169dba5d1d1fc1001deb47131
|
4
|
+
data.tar.gz: 2c55660cb71f7a1e0e4f0def92394ad110403c65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eadd488f253fda93f4bbe152d78c4e1b197d80f64da0b1cd0ab8edfbc58564641ca5161ee01fef51791ee746212dd68d8a7f10504b53ea6e88578605eddcf609
|
7
|
+
data.tar.gz: bd53d995542bf0d11e579abfd4b219ef6565b2384f449468efe22cd3a92f943534889ed66814b9b243b91ca9906467d5e5d051cd3a51c010234fea3d02eb0473
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2013, Brian Shirai
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
3. Neither the name of the library nor the names of its contributors may be
|
13
|
+
used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
|
20
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
21
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
22
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
23
|
+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
24
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
25
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Rubysl::Ostruct
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rubysl-ostruct'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rubysl-ostruct
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/ostruct.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rubysl/ostruct"
|
@@ -0,0 +1,149 @@
|
|
1
|
+
#
|
2
|
+
# = ostruct.rb: OpenStruct implementation
|
3
|
+
#
|
4
|
+
# Author:: Yukihiro Matsumoto
|
5
|
+
# Documentation:: Gavin Sinclair
|
6
|
+
#
|
7
|
+
# OpenStruct allows the creation of data objects with arbitrary attributes.
|
8
|
+
# See OpenStruct for an example.
|
9
|
+
#
|
10
|
+
|
11
|
+
#
|
12
|
+
# OpenStruct allows you to create data objects and set arbitrary attributes.
|
13
|
+
# For example:
|
14
|
+
#
|
15
|
+
# require 'ostruct'
|
16
|
+
#
|
17
|
+
# record = OpenStruct.new
|
18
|
+
# record.name = "John Smith"
|
19
|
+
# record.age = 70
|
20
|
+
# record.pension = 300
|
21
|
+
#
|
22
|
+
# puts record.name # -> "John Smith"
|
23
|
+
# puts record.address # -> nil
|
24
|
+
#
|
25
|
+
# It is like a hash with a different way to access the data. In fact, it is
|
26
|
+
# implemented with a hash, and you can initialize it with one.
|
27
|
+
#
|
28
|
+
# hash = { "country" => "Australia", :population => 20_000_000 }
|
29
|
+
# data = OpenStruct.new(hash)
|
30
|
+
#
|
31
|
+
# p data # -> <OpenStruct country="Australia" population=20000000>
|
32
|
+
#
|
33
|
+
class OpenStruct
|
34
|
+
#
|
35
|
+
# Create a new OpenStruct object. The optional +hash+, if given, will
|
36
|
+
# generate attributes and values. For example.
|
37
|
+
#
|
38
|
+
# require 'ostruct'
|
39
|
+
# hash = { "country" => "Australia", :population => 20_000_000 }
|
40
|
+
# data = OpenStruct.new(hash)
|
41
|
+
#
|
42
|
+
# p data # -> <OpenStruct country="Australia" population=20000000>
|
43
|
+
#
|
44
|
+
# By default, the resulting OpenStruct object will have no attributes.
|
45
|
+
#
|
46
|
+
def initialize(hash=nil)
|
47
|
+
@table = {}
|
48
|
+
if hash
|
49
|
+
for k,v in hash
|
50
|
+
@table[k.to_sym] = v
|
51
|
+
new_ostruct_member(k)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Duplicate an OpenStruct object members.
|
57
|
+
def initialize_copy(orig)
|
58
|
+
super
|
59
|
+
@table = @table.dup
|
60
|
+
end
|
61
|
+
|
62
|
+
def marshal_dump
|
63
|
+
@table
|
64
|
+
end
|
65
|
+
def marshal_load(x)
|
66
|
+
@table = x
|
67
|
+
@table.each_key{|key| new_ostruct_member(key)}
|
68
|
+
end
|
69
|
+
|
70
|
+
def modifiable
|
71
|
+
if self.frozen?
|
72
|
+
raise TypeError, "can't modify frozen #{self.class}", caller(2)
|
73
|
+
end
|
74
|
+
@table
|
75
|
+
end
|
76
|
+
protected :modifiable
|
77
|
+
|
78
|
+
def new_ostruct_member(name)
|
79
|
+
name = name.to_sym
|
80
|
+
unless self.respond_to?(name)
|
81
|
+
class << self; self; end.class_eval do
|
82
|
+
define_method(name) { @table[name] }
|
83
|
+
define_method("#{name}=") { |x| modifiable[name] = x }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
name
|
87
|
+
end
|
88
|
+
|
89
|
+
def method_missing(mid, *args) # :nodoc:
|
90
|
+
mname = mid.id2name
|
91
|
+
len = args.length
|
92
|
+
if mname.chomp!('=') && mname != "[]"
|
93
|
+
if len != 1
|
94
|
+
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
95
|
+
end
|
96
|
+
modifiable[new_ostruct_member(mname)] = args[0]
|
97
|
+
elsif len == 0
|
98
|
+
@table[mid]
|
99
|
+
else
|
100
|
+
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Remove the named field from the object.
|
106
|
+
#
|
107
|
+
def delete_field(name)
|
108
|
+
@table.delete name.to_sym
|
109
|
+
end
|
110
|
+
|
111
|
+
InspectKey = :__inspect_key__ # :nodoc:
|
112
|
+
|
113
|
+
#
|
114
|
+
# Returns a string containing a detailed summary of the keys and values.
|
115
|
+
#
|
116
|
+
def inspect
|
117
|
+
str = "#<#{self.class}"
|
118
|
+
|
119
|
+
ids = (Thread.current[InspectKey] ||= [])
|
120
|
+
if ids.include?(object_id)
|
121
|
+
return str << ' ...>'
|
122
|
+
end
|
123
|
+
|
124
|
+
ids << object_id
|
125
|
+
begin
|
126
|
+
first = true
|
127
|
+
for k,v in @table
|
128
|
+
str << "," unless first
|
129
|
+
first = false
|
130
|
+
str << " #{k}=#{v.inspect}"
|
131
|
+
end
|
132
|
+
return str << '>'
|
133
|
+
ensure
|
134
|
+
ids.pop
|
135
|
+
end
|
136
|
+
end
|
137
|
+
alias :to_s :inspect
|
138
|
+
|
139
|
+
attr_reader :table # :nodoc:
|
140
|
+
protected :table
|
141
|
+
|
142
|
+
#
|
143
|
+
# Compare this object and +other+ for equality.
|
144
|
+
#
|
145
|
+
def ==(other)
|
146
|
+
return false unless(other.kind_of?(OpenStruct))
|
147
|
+
return @table == other.table
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/ostruct/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-ostruct"
|
6
|
+
spec.version = RubySL::OpenStruct::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library ostruct.}
|
10
|
+
spec.summary = %q{Ruby standard library ostruct.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-ostruct"
|
12
|
+
spec.license = "BSD"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
describe "OpenStruct#delete_field" do
|
4
|
+
before :each do
|
5
|
+
@os = OpenStruct.new(:name => "John Smith", :age => 70, :pension => 300)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "removes the named field from self's method/value table" do
|
9
|
+
@os.delete_field(:name)
|
10
|
+
@os.send(:table)[:name].should be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
ruby_version_is ""..."1.9.3" do
|
14
|
+
it "does not remove the accessor methods" do
|
15
|
+
@os.delete_field(:name)
|
16
|
+
@os.respond_to?(:name).should be_true
|
17
|
+
@os.respond_to?(:name=).should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ruby_version_is "1.9.3" do
|
22
|
+
it "does remove the accessor methods" do
|
23
|
+
@os.delete_field(:name)
|
24
|
+
@os.respond_to?(:name).should be_false
|
25
|
+
@os.respond_to?(:name=).should be_false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
describe "OpenStruct#[]" do
|
4
|
+
before :each do
|
5
|
+
@os = OpenStruct.new
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_version_is ""..."2.0" do
|
9
|
+
it "raises a NoMethodError" do
|
10
|
+
lambda { @os[:foo] }.should raise_error(NoMethodError)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ruby_version_is "2.0" do
|
15
|
+
it "returns the associated value" do
|
16
|
+
@os.foo = 42
|
17
|
+
@os[:foo].should == 42
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
describe "OpenStruct#[]=" do
|
4
|
+
before :each do
|
5
|
+
@os = OpenStruct.new
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_bug "redmine:4179", "1.9.2" do
|
9
|
+
ruby_version_is ""..."2.0" do
|
10
|
+
it "raises a NoMethodError" do
|
11
|
+
lambda { @os[:foo] = 2 }.should raise_error(NoMethodError)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
ruby_version_is "2.0" do
|
15
|
+
it "sets the associated value" do
|
16
|
+
@os[:foo] = 42
|
17
|
+
@os.foo.should == 42
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
describe "OpenStruct#==" do
|
5
|
+
before(:each) do
|
6
|
+
@os = OpenStruct.new(:name => "John")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns false when the passed argument is no OpenStruct" do
|
10
|
+
(@os == Object.new).should be_false
|
11
|
+
(@os == "Test").should be_false
|
12
|
+
(@os == 10).should be_false
|
13
|
+
(@os == :sym).should be_false
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns true when self and other are equal method/value wise" do
|
17
|
+
(@os == @os).should be_true
|
18
|
+
(@os == OpenStruct.new(:name => "John")).should be_true
|
19
|
+
(@os == OpenStructSpecs::OpenStructSub.new(:name => "John")).should be_true
|
20
|
+
|
21
|
+
(@os == OpenStruct.new(:name => "Jonny")).should be_false
|
22
|
+
(@os == OpenStructSpecs::OpenStructSub.new(:name => "Jonny")).should be_false
|
23
|
+
|
24
|
+
(@os == OpenStruct.new(:name => "John", :age => 20)).should be_false
|
25
|
+
(@os == OpenStructSpecs::OpenStructSub.new(:name => "John", :age => 20)).should be_false
|
26
|
+
end
|
27
|
+
end
|
data/spec/frozen_spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
describe "OpenStruct.new when frozen" do
|
4
|
+
before :each do
|
5
|
+
@os = OpenStruct.new(:name => "John Smith", :age => 70, :pension => 300).freeze
|
6
|
+
end
|
7
|
+
#
|
8
|
+
# method_missing case handled in method_missing_spec.rb
|
9
|
+
#
|
10
|
+
it "is still readable" do
|
11
|
+
@os.age.should eql(70)
|
12
|
+
@os.pension.should eql(300)
|
13
|
+
@os.name.should == "John Smith"
|
14
|
+
end
|
15
|
+
|
16
|
+
ruby_bug "#1219", "1.9.1" do
|
17
|
+
it "is not writeable" do
|
18
|
+
lambda{ @os.age = 42 }.should raise_error( TypeError )
|
19
|
+
end
|
20
|
+
|
21
|
+
it "cannot create new fields" do
|
22
|
+
lambda{ @os.state = :new }.should raise_error( TypeError )
|
23
|
+
end
|
24
|
+
|
25
|
+
it "creates a frozen clone" do
|
26
|
+
f = @os.clone
|
27
|
+
f.age.should == 70
|
28
|
+
lambda{ f.age = 0 }.should raise_error( TypeError )
|
29
|
+
lambda{ f.state = :newer }.should raise_error( TypeError )
|
30
|
+
end
|
31
|
+
end # ruby_bug
|
32
|
+
|
33
|
+
it "creates an unfrozen dup" do
|
34
|
+
d = @os.dup
|
35
|
+
d.age.should == 70
|
36
|
+
d.age = 42
|
37
|
+
d.age.should == 42
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
describe "OpenStruct#initialize_copy" do
|
4
|
+
before :each do
|
5
|
+
@os = OpenStruct.new("age" => 20, "name" => "John")
|
6
|
+
@dup = @os.dup
|
7
|
+
end
|
8
|
+
|
9
|
+
it "is private" do
|
10
|
+
OpenStruct.should have_private_instance_method(:initialize_copy)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "creates an independent method/value table for self" do
|
14
|
+
@dup.age = 30
|
15
|
+
@dup.age.should eql(30)
|
16
|
+
@os.age.should eql(20)
|
17
|
+
end
|
18
|
+
|
19
|
+
ruby_bug "bug 6028", "1.9.3" do
|
20
|
+
it "generates the same methods" do
|
21
|
+
@dup.methods.should == @os.methods
|
22
|
+
@dup.respond_to?(:age).should == @os.respond_to?(:age)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
describe "OpenStruct#marshal_load when passed [Hash]" do
|
4
|
+
it "defines methods based on the passed Hash" do
|
5
|
+
os = OpenStruct.new
|
6
|
+
os.marshal_load(:age => 20, :name => "John")
|
7
|
+
|
8
|
+
os.age.should eql(20)
|
9
|
+
os.name.should == "John"
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
describe "OpenStruct#method_missing when called with a method name ending in '='" do
|
4
|
+
before(:each) do
|
5
|
+
@os = OpenStruct.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "raises an ArgumentError when not passed any additional arguments" do
|
9
|
+
lambda { @os.method_missing(:test=) }.should raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises a TypeError when self is frozen" do
|
13
|
+
@os.freeze
|
14
|
+
lambda { @os.method_missing(:test=, "test") }.should raise_error(TypeError)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "creates accessor methods" do
|
18
|
+
@os.method_missing(:test=, "test")
|
19
|
+
@os.respond_to?(:test=).should be_true
|
20
|
+
@os.respond_to?(:test).should be_true
|
21
|
+
|
22
|
+
@os.test.should == "test"
|
23
|
+
@os.test = "changed"
|
24
|
+
@os.test.should == "changed"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "updates the method/value table with the passed method/value" do
|
28
|
+
@os.method_missing(:test=, "test")
|
29
|
+
@os.send(:table)[:test].should == "test"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "OpenStruct#method_missing when passed additional arguments" do
|
34
|
+
it "raises a NoMethodError" do
|
35
|
+
os = OpenStruct.new
|
36
|
+
lambda { os.method_missing(:test, 1, 2, 3) }.should raise_error(NoMethodError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "OpenStruct#method_missing when not passed any additional arguments" do
|
41
|
+
it "returns the value for the passed method from the method/value table" do
|
42
|
+
os = OpenStruct.new(:age => 20)
|
43
|
+
os.method_missing(:age).should eql(20)
|
44
|
+
os.method_missing(:name).should be_nil
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
describe "OpenStruct#new_ostruct_member when passed [method_name]" do
|
4
|
+
before(:each) do
|
5
|
+
@os = OpenStruct.new
|
6
|
+
@os.instance_variable_set(:@table, :age => 20)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "creates an attribute reader method for the passed method_name" do
|
10
|
+
@os.respond_to?(:age).should be_false
|
11
|
+
@os.send :new_ostruct_member, :age
|
12
|
+
@os.method(:age).call.should == 20
|
13
|
+
end
|
14
|
+
|
15
|
+
it "creates an attribute writer method for the passed method_name" do
|
16
|
+
@os.respond_to?(:age=).should be_false
|
17
|
+
@os.send :new_ostruct_member, :age
|
18
|
+
@os.method(:age=).call(42).should == 42
|
19
|
+
@os.age.should == 42
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not allow overwriting existing methods" do
|
23
|
+
def @os.age
|
24
|
+
10
|
25
|
+
end
|
26
|
+
|
27
|
+
@os.send :new_ostruct_member, :age
|
28
|
+
@os.age.should == 10
|
29
|
+
@os.respond_to?(:age=).should be_false
|
30
|
+
end
|
31
|
+
end
|
data/spec/new_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
describe "OpenStruct.new when passed [Hash]" do
|
4
|
+
before :each do
|
5
|
+
@os = OpenStruct.new(:name => "John Smith", :age => 70, :pension => 300)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "creates an attribute for each key of the passed Hash" do
|
9
|
+
@os.age.should eql(70)
|
10
|
+
@os.pension.should eql(300)
|
11
|
+
@os.name.should == "John Smith"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "OpenStruct.new when passed no arguments" do
|
16
|
+
it "returns a new OpenStruct Object without any attributes" do
|
17
|
+
OpenStruct.new.to_s.should == "#<OpenStruct>"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
describe :ostruct_inspect, :shared => true do
|
2
|
+
it "returns a String representation of self" do
|
3
|
+
os = OpenStruct.new(:name => "John Smith")
|
4
|
+
os.send(@method).should == "#<OpenStruct name=\"John Smith\">"
|
5
|
+
|
6
|
+
os = OpenStruct.new(:age => 20, :name => "John Smith")
|
7
|
+
os.send(@method).should be_kind_of(String)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "correctly handles self-referential OpenStructs" do
|
11
|
+
os = OpenStruct.new
|
12
|
+
os.self = os
|
13
|
+
os.send(@method).should == "#<OpenStruct self=#<OpenStruct ...>>"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "correctly handles OpenStruct subclasses" do
|
17
|
+
os = OpenStructSpecs::OpenStructSub.new(:name => "John Smith")
|
18
|
+
os.send(@method).should == "#<OpenStructSpecs::OpenStructSub name=\"John Smith\">"
|
19
|
+
end
|
20
|
+
end
|
data/spec/table_spec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
describe "OpenStruct#table" do
|
4
|
+
before(:each) do
|
5
|
+
@os = OpenStruct.new("age" => 20, "name" => "John")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "is protected" do
|
9
|
+
OpenStruct.should have_protected_instance_method(:table)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns self's method/value table" do
|
13
|
+
@os.send(:table).should == { :age => 20, :name => "John" }
|
14
|
+
@os.send(:table)[:age] = 30
|
15
|
+
@os.age.should == 30
|
16
|
+
end
|
17
|
+
end
|
data/spec/to_h_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
ruby_version_is "2.0" do
|
4
|
+
describe "OpenStruct#to_h" do
|
5
|
+
before :each do
|
6
|
+
@h = {:name => "John Smith", :age => 70, :pension => 300}
|
7
|
+
@os = OpenStruct.new(@h)
|
8
|
+
@to_h = @os.to_h
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns a Hash with members as keys" do
|
12
|
+
@to_h.should == @h
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns a Hash with keys as symbols" do
|
16
|
+
os = OpenStruct.new("name" => "John Smith", "age" => 70)
|
17
|
+
os.pension = 300
|
18
|
+
os.to_h.should == @h
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does not return the hash used as initializer" do
|
22
|
+
@to_h.should_not equal(@h)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns a Hash that is independent from the struct" do
|
26
|
+
@to_h[:age] = 71
|
27
|
+
@os.age.should == 70
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/to_s_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubysl-ostruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Shirai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Ruby standard library ostruct.
|
42
|
+
email:
|
43
|
+
- brixen@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .travis.yml
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/ostruct.rb
|
55
|
+
- lib/rubysl/ostruct.rb
|
56
|
+
- lib/rubysl/ostruct/ostruct.rb
|
57
|
+
- lib/rubysl/ostruct/version.rb
|
58
|
+
- rubysl-ostruct.gemspec
|
59
|
+
- spec/delete_field_spec.rb
|
60
|
+
- spec/element_reference_spec.rb
|
61
|
+
- spec/element_set_spec.rb
|
62
|
+
- spec/equal_value_spec.rb
|
63
|
+
- spec/fixtures/classes.rb
|
64
|
+
- spec/frozen_spec.rb
|
65
|
+
- spec/initialize_copy_spec.rb
|
66
|
+
- spec/initialize_spec.rb
|
67
|
+
- spec/inspect_spec.rb
|
68
|
+
- spec/marshal_dump_spec.rb
|
69
|
+
- spec/marshal_load_spec.rb
|
70
|
+
- spec/method_missing_spec.rb
|
71
|
+
- spec/new_ostruct_member_spec.rb
|
72
|
+
- spec/new_spec.rb
|
73
|
+
- spec/shared/inspect.rb
|
74
|
+
- spec/table_spec.rb
|
75
|
+
- spec/to_h_spec.rb
|
76
|
+
- spec/to_s_spec.rb
|
77
|
+
homepage: https://github.com/rubysl/rubysl-ostruct
|
78
|
+
licenses:
|
79
|
+
- BSD
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.0.7
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Ruby standard library ostruct.
|
101
|
+
test_files:
|
102
|
+
- spec/delete_field_spec.rb
|
103
|
+
- spec/element_reference_spec.rb
|
104
|
+
- spec/element_set_spec.rb
|
105
|
+
- spec/equal_value_spec.rb
|
106
|
+
- spec/fixtures/classes.rb
|
107
|
+
- spec/frozen_spec.rb
|
108
|
+
- spec/initialize_copy_spec.rb
|
109
|
+
- spec/initialize_spec.rb
|
110
|
+
- spec/inspect_spec.rb
|
111
|
+
- spec/marshal_dump_spec.rb
|
112
|
+
- spec/marshal_load_spec.rb
|
113
|
+
- spec/method_missing_spec.rb
|
114
|
+
- spec/new_ostruct_member_spec.rb
|
115
|
+
- spec/new_spec.rb
|
116
|
+
- spec/shared/inspect.rb
|
117
|
+
- spec/table_spec.rb
|
118
|
+
- spec/to_h_spec.rb
|
119
|
+
- spec/to_s_spec.rb
|