classy_struct 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ tmp/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 amikula
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = airystruct
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 amikula. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "classy_struct"
8
+ gem.summary = %Q{A better-performing alternative to OpenStruct}
9
+ gem.description = %Q{A better-performing alternative to OpenStruct}
10
+ gem.email = "amikula@gmail.com"
11
+ gem.homepage = "http://github.com/amikula/classy_struct"
12
+ gem.authors = ["amikula"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "classy_struct #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,44 @@
1
+ class ClassyStruct
2
+ def self.new
3
+ Class.new(ClassyStructClass)
4
+ end
5
+
6
+ class ClassyStructClass
7
+ def initialize(hash=hil)
8
+ if hash
9
+ hash.each_pair do |k,v|
10
+ if v.is_a?(Hash)
11
+ v = self.class.node_class(k).new(v)
12
+ end
13
+
14
+ send("#{k}=", v)
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.node_class(name)
20
+ @__node_classes ||= {}
21
+ @__node_classes[name.to_sym] ||= ClassyStruct.new
22
+ end
23
+
24
+ def new_child(key)
25
+ self.send("#{key}=", self.class.node_class(key).new)
26
+ end
27
+
28
+ def method_missing(name, *args)
29
+ base = (name.to_s =~ /=$/) ? name.to_s[0..-2] : name
30
+
31
+ self.class.class_eval <<-EOF
32
+ def #{base}
33
+ @#{base}
34
+ end
35
+
36
+ def #{base}=(val)
37
+ @#{base} = val
38
+ end
39
+ EOF
40
+
41
+ send(name, *args)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,112 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe ClassyStruct do
4
+ before :each do
5
+ @foo_struct = ClassyStruct.new
6
+ @bar_struct = ClassyStruct.new
7
+ end
8
+
9
+ describe :new do
10
+ it "returns something that is not a ClassyStruct" do
11
+ ClassyStruct.new.should_not be_a(ClassyStruct)
12
+ end
13
+
14
+ it "returns something that inherits from ClassyStruct" do
15
+ ClassyStruct.new.ancestors.should include(ClassyStruct::ClassyStructClass)
16
+ end
17
+ end
18
+
19
+ describe :method_missing do
20
+ it 'assigns and returns values' do
21
+ o = @foo_struct.new
22
+
23
+ o.bar = :baz
24
+ o.bar.should == :baz
25
+ end
26
+
27
+ it 'adds methods to the base class' do
28
+ o = @foo_struct.new
29
+
30
+ o.bar = :baz
31
+
32
+ o.methods.should include('bar')
33
+ o.class.instance_methods.should include('bar')
34
+
35
+ p = @foo_struct.new
36
+ p.methods.should include('bar')
37
+
38
+ p.should_not_receive(:method_missing)
39
+ p.bar
40
+ end
41
+
42
+ it 'does not add methods to other class instances' do
43
+ o = @foo_struct.new
44
+ o.bar = :baz
45
+
46
+ @foo_struct.instance_methods.should include('bar')
47
+ @bar_struct.instance_methods.should_not include('bar')
48
+ end
49
+ end
50
+
51
+ describe ClassyStruct::ClassyStructClass do
52
+ describe :child_node do
53
+ it 'creates a child node whose class is a ClassyStructClass' do
54
+ @foo_struct.node_class(:bar).ancestors.should include(ClassyStruct::ClassyStructClass)
55
+ end
56
+
57
+ it 'creates child nodes whose class is the same regardless of the instance' do
58
+ klazz1 = @foo_struct.node_class(:bar)
59
+ klazz2 = @foo_struct.node_class(:bar)
60
+
61
+ klazz1.should == klazz2
62
+ end
63
+
64
+ it 'creates child nodes whose class is different for different ClassyStruct instances' do
65
+ klazz1 = @foo_struct.node_class(:bar)
66
+ klazz2 = @bar_struct.node_class(:bar)
67
+
68
+ klazz1.should_not == klazz2
69
+ end
70
+ end
71
+
72
+ describe :initialize do
73
+ it 'initializes attributes with a hash provided' do
74
+ o = @foo_struct.new(:foo => :bar, 'baz' => :xyzzy)
75
+
76
+ o.foo.should == :bar
77
+ o.baz.should == :xyzzy
78
+ end
79
+
80
+ it 'initializes sub-hashes by creating new ClassyStruct child nodes' do
81
+ o = @foo_struct.new(:foo => :bar, 'baz' => {:xyzzy => 'something', 'thud' => 'splat'})
82
+
83
+ o.foo.should == :bar
84
+ o.baz.should_not be_a(Hash)
85
+ o.baz.xyzzy.should == 'something'
86
+ o.baz.thud.should == 'splat'
87
+ end
88
+
89
+ it 'creates persistent ClassyStructClass objects for child nodes' do
90
+ o = @foo_struct.new(:foo => :bar, 'baz' => {:xyzzy => 'something', 'thud' => 'splat'})
91
+ p = @foo_struct.new(:foo => :bar, 'baz' => {:xyzzy => 'something', 'thud' => 'splat'})
92
+
93
+ o.class.should == p.class
94
+ o.baz.class.should == p.baz.class
95
+ end
96
+ end
97
+
98
+ describe :new_child do
99
+ it 'returns a new child node whose class is the node_class for the provided key' do
100
+ o = @foo_struct.new
101
+
102
+ o.new_child(:foo).class.should == @foo_struct.node_class(:foo)
103
+ end
104
+
105
+ it 'assigns the attribute provided with the new child node' do
106
+ o = @foo_struct.new
107
+
108
+ o.new_child(:foo).should == o.foo
109
+ end
110
+ end
111
+ end
112
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'classy_struct'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classy_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - amikula
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: A better-performing alternative to OpenStruct
26
+ email: amikula@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/classy_struct.rb
42
+ - spec/classy_struct_spec.rb
43
+ - spec/spec.opts
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/amikula/classy_struct
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A better-performing alternative to OpenStruct
73
+ test_files:
74
+ - spec/classy_struct_spec.rb
75
+ - spec/spec_helper.rb