object_loader 1.0.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/.gemtest +0 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +59 -0
- data/LICENSE.txt +20 -0
- data/README.md +54 -0
- data/Rakefile +36 -0
- data/gemspec.yml +15 -0
- data/lib/object_loader/class_methods.rb +69 -0
- data/lib/object_loader/exceptions/object_not_found.rb +4 -0
- data/lib/object_loader/exceptions.rb +1 -0
- data/lib/object_loader/object_loader.rb +152 -0
- data/lib/object_loader/pending_object.rb +23 -0
- data/lib/object_loader/version.rb +4 -0
- data/lib/object_loader.rb +2 -0
- data/object_loader.gemspec +15 -0
- data/spec/helpers/book.rb +13 -0
- data/spec/helpers/book_review.rb +19 -0
- data/spec/helpers/objects/discrete_structures.rb +6 -0
- data/spec/helpers/objects/load_error.rb +4 -0
- data/spec/helpers/objects/name_error.rb +2 -0
- data/spec/helpers/objects/neuromancer_review.rb +19 -0
- data/spec/helpers/objects/no_method_error.rb +2 -0
- data/spec/helpers/objects/snow_crash.rb +10 -0
- data/spec/helpers/objects/syntax_error.rb +5 -0
- data/spec/helpers/objects.rb +13 -0
- data/spec/helpers/text_book.rb +4 -0
- data/spec/object_loader_spec.rb +122 -0
- data/spec/spec_helper.rb +2 -0
- metadata +117 -0
data/.gemtest
ADDED
File without changes
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --title 'ObjectLoader Documentation' --protected --files ChangeLog.md,LICENSE.txt
|
data/ChangeLog.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
### 1.0.0 / 2011-03-22
|
2
|
+
|
3
|
+
* Renamed Contextify to {ObjectLoader}.
|
4
|
+
* Refactored the API.
|
5
|
+
|
6
|
+
### 0.2.0 / 2010-10-27
|
7
|
+
|
8
|
+
* Added `Contextify::ContextifiedClassMethods`.
|
9
|
+
* Fixed a block-variable shadowing bug in
|
10
|
+
`Contextify::ClassMethods#contextify`.
|
11
|
+
* Removed the `metaid.rb` extension.
|
12
|
+
|
13
|
+
### 0.1.6 / 2010-05-23
|
14
|
+
|
15
|
+
* Moved the Contextify YARD handlers into the
|
16
|
+
[yard-contextify](http://github.com/postmodern/yard-contextify) library.
|
17
|
+
|
18
|
+
### 0.1.5 / 2009-02-02
|
19
|
+
|
20
|
+
* Prevent exceptions from jamming the `Contextify.waiting` queue when
|
21
|
+
loading context files with `Contextify.load_blocks`.
|
22
|
+
* Added specs for loading and recovering from erroneous context files.
|
23
|
+
|
24
|
+
### 0.1.4 / 2009-01-30
|
25
|
+
|
26
|
+
* Require RSpec >= 1.3.0.
|
27
|
+
* Require YARD >= 0.5.3.
|
28
|
+
* Added `Contextify::ClassMethods`.
|
29
|
+
* Added `Contextify::PendingContext#each_class`.
|
30
|
+
* Added `Contextify::PendingContext#each`.
|
31
|
+
* Fixed a bug in `Contextify::PendingContext#each_block`.
|
32
|
+
* Fixed a typo where `Contextify` methods where being defined within
|
33
|
+
the `Contextify.included` method.
|
34
|
+
|
35
|
+
### 0.1.3 / 2009-09-21
|
36
|
+
|
37
|
+
* Require Hoe >= 2.3.3.
|
38
|
+
* Require YARD >= 0.2.3.5.
|
39
|
+
* Require RSpec >= 1.2.8.
|
40
|
+
* Use 'hoe/signing' for signed RubyGems.
|
41
|
+
* Moved to YARD based documentation.
|
42
|
+
* Added YARD handlers for documenting contextify method calls.
|
43
|
+
* Allow self.load_context to load objects that inherit from the contextified class.
|
44
|
+
* All specs pass on JRuby 1.3.1.
|
45
|
+
|
46
|
+
### 0.1.2 / 2009-02-06
|
47
|
+
|
48
|
+
* Added self.load_context_block to Contextify.
|
49
|
+
* Added specs for Contextify.load_block and self.load_context_block.
|
50
|
+
|
51
|
+
### 0.1.1 / 2009-01-17
|
52
|
+
|
53
|
+
* Include missing files in Manifest.
|
54
|
+
* All specs pass in Ruby 1.9.1-rc1.
|
55
|
+
|
56
|
+
### 0.1.0 / 2008-12-29
|
57
|
+
|
58
|
+
* Initial release.
|
59
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2011 Hal Brodigan
|
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 NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# ObjectLoader
|
2
|
+
|
3
|
+
* [Source](http://github.com/postmodern/object_loader)
|
4
|
+
* [Issues](http://github.com/postmodern/object_loader/issues)
|
5
|
+
* [Documentation](http://rubydoc.info/gems/object_loader)
|
6
|
+
* [Email](mailto:postmodern.mod3 at gmail.com)
|
7
|
+
|
8
|
+
## Description
|
9
|
+
|
10
|
+
ObjectLoader can load Ruby Objects containing methods and procs from
|
11
|
+
Ruby files without having to use YAML or define classes named like the file.
|
12
|
+
|
13
|
+
## Examples
|
14
|
+
|
15
|
+
# file: controller.rb
|
16
|
+
class Controller
|
17
|
+
|
18
|
+
include ObjectLoader
|
19
|
+
|
20
|
+
# ...
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
# file: my_controller.rb
|
25
|
+
Controller.object do
|
26
|
+
|
27
|
+
def test1
|
28
|
+
'This is the first test'
|
29
|
+
end
|
30
|
+
|
31
|
+
def test2(mesg)
|
32
|
+
"Hello #{mesg}"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
# load a Controller object from a file.
|
38
|
+
controller = Controller.load_object('my_controller.rb')
|
39
|
+
# => #<Controller: ...>
|
40
|
+
|
41
|
+
controller.test1
|
42
|
+
# => "This is the first test"
|
43
|
+
controller.test2('one two three')
|
44
|
+
# => "Hello one two three"
|
45
|
+
|
46
|
+
## Install
|
47
|
+
|
48
|
+
$ sudo gem install object_loader
|
49
|
+
|
50
|
+
## Copyright
|
51
|
+
|
52
|
+
Copyright (c) 2011 Hal Brodigan
|
53
|
+
|
54
|
+
See {file:LICENSE.txt} for license information.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
gem 'ore-tasks', '~> 0.4'
|
6
|
+
require 'ore/tasks'
|
7
|
+
|
8
|
+
Ore::Tasks.new
|
9
|
+
rescue LoadError => e
|
10
|
+
STDERR.puts e.message
|
11
|
+
STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
|
12
|
+
end
|
13
|
+
|
14
|
+
begin
|
15
|
+
gem 'rspec', '~> 2.4'
|
16
|
+
require 'rspec/core/rake_task'
|
17
|
+
|
18
|
+
RSpec::Core::RakeTask.new
|
19
|
+
rescue LoadError => e
|
20
|
+
task :spec do
|
21
|
+
abort "Please run `gem install rspec` to install RSpec."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
task :test => :spec
|
25
|
+
task :default => :spec
|
26
|
+
|
27
|
+
begin
|
28
|
+
gem 'yard', '~> 0.6.0'
|
29
|
+
require 'yard'
|
30
|
+
|
31
|
+
YARD::Rake::YardocTask.new
|
32
|
+
rescue LoadError => e
|
33
|
+
task :yard do
|
34
|
+
abort "Please run `gem install yard` to install YARD."
|
35
|
+
end
|
36
|
+
end
|
data/gemspec.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
name: object_loader
|
2
|
+
summary: Loads Ruby Objects containing methods and procs from Ruby files.
|
3
|
+
description:
|
4
|
+
ObjectLoader can load Ruby Objects containing methods and procs from Ruby
|
5
|
+
files without having to use YAML or define classes named like the file.
|
6
|
+
license: MIT
|
7
|
+
authors: Postmodern
|
8
|
+
email: postmodern.mod3@gmail.com
|
9
|
+
homepage: http://github.com/postmodern/object_loader
|
10
|
+
has_yard: true
|
11
|
+
|
12
|
+
development_dependencies:
|
13
|
+
ore-tasks: ~> 0.4
|
14
|
+
rspec: ~> 2.4
|
15
|
+
yard: ~> 0.6.0
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module ObjectLoader
|
2
|
+
module ClassMethods
|
3
|
+
#
|
4
|
+
# Loads a block for the Class.
|
5
|
+
#
|
6
|
+
# @param [String] path
|
7
|
+
# The path to load the block from.
|
8
|
+
#
|
9
|
+
# @return [Proc]
|
10
|
+
# The block defined for the Class.
|
11
|
+
#
|
12
|
+
# @since 1.0.0
|
13
|
+
#
|
14
|
+
def load_object_block(path)
|
15
|
+
ObjectLoader.load_blocks(path)[self]
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Loads a compatible object.
|
20
|
+
#
|
21
|
+
# @param [String] path
|
22
|
+
# The path to load the object from.
|
23
|
+
#
|
24
|
+
# @param [Array] arguments
|
25
|
+
# Additional arguments to use when creating the object.
|
26
|
+
#
|
27
|
+
# @return [Object]
|
28
|
+
# The loaded object.
|
29
|
+
#
|
30
|
+
# @since 1.0.0
|
31
|
+
#
|
32
|
+
def load_object(path,*arguments)
|
33
|
+
pending = ObjectLoader.load_blocks(path)
|
34
|
+
|
35
|
+
pending_class, pending_block = pending.find do |klass,block|
|
36
|
+
klass.ancestors.include?(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
if (pending_class && pending_block)
|
40
|
+
obj = pending_class.new(*arguments)
|
41
|
+
obj.instance_eval(&pending_block)
|
42
|
+
obj
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Creates a loadable object using the arguments and block.
|
48
|
+
#
|
49
|
+
# @param [Array] args
|
50
|
+
# Additional arguments to pass to the Classes `new` method.
|
51
|
+
#
|
52
|
+
# @yield []
|
53
|
+
# The given block will be instance evaled into the newly created
|
54
|
+
# object when the object is loaded.
|
55
|
+
#
|
56
|
+
# @since 1.0.0
|
57
|
+
#
|
58
|
+
def object(*args,&block)
|
59
|
+
if (args.empty? && ObjectLoader.is_pending?)
|
60
|
+
ObjectLoader.pending[self] = block
|
61
|
+
return nil
|
62
|
+
else
|
63
|
+
new_object = self.new(*args)
|
64
|
+
new_object.instance_eval(&block) if block
|
65
|
+
return new_object
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'object_loader/exceptions/object_not_found'
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'object_loader/exceptions/object_not_found'
|
2
|
+
require 'object_loader/class_methods'
|
3
|
+
require 'object_loader/pending_object'
|
4
|
+
|
5
|
+
module ObjectLoader
|
6
|
+
#
|
7
|
+
# Includes {ClassMethods} into the class.
|
8
|
+
#
|
9
|
+
# @param [Class] base
|
10
|
+
# The class that {ObjectLoader} is being included into.
|
11
|
+
#
|
12
|
+
def self.included(base)
|
13
|
+
base.extend ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# The pending objects waiting to be fully loaded.
|
18
|
+
#
|
19
|
+
# @return [Array<PendingObject>]
|
20
|
+
# Contexts which are waiting to be loaded.
|
21
|
+
#
|
22
|
+
# @since 1.0.0
|
23
|
+
#
|
24
|
+
def ObjectLoader.queue
|
25
|
+
@@queue ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# The first pending object waiting to be fully loaded.
|
30
|
+
#
|
31
|
+
# @return [PendingObject]
|
32
|
+
# The pending object being loaded.
|
33
|
+
#
|
34
|
+
# @since 1.0.0
|
35
|
+
#
|
36
|
+
def ObjectLoader.pending
|
37
|
+
queue.first
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Determines whether there are pending objects.
|
42
|
+
#
|
43
|
+
# @return [Boolean]
|
44
|
+
# Specifies whether there is a pending object present.
|
45
|
+
#
|
46
|
+
# @since 1.0.0
|
47
|
+
#
|
48
|
+
def ObjectLoader.is_pending?
|
49
|
+
!(queue.empty?)
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Finds the first pending object being loaded from a specific path.
|
54
|
+
#
|
55
|
+
# @param [String] path
|
56
|
+
# The path which is being loaded.
|
57
|
+
#
|
58
|
+
# @return [PendingObject]
|
59
|
+
# The first pending object with the specified path.
|
60
|
+
#
|
61
|
+
# @since 1.0.0
|
62
|
+
#
|
63
|
+
def ObjectLoader.loading(path)
|
64
|
+
queue.find { |pending| pending.path == path }
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Determines whether objects are being loaded from a specific path.
|
69
|
+
#
|
70
|
+
# @param [String] path
|
71
|
+
# The path to check if objects are being loaded from.
|
72
|
+
#
|
73
|
+
# @return [Boolean]
|
74
|
+
# Specifies whether pending objects are being loaded from the
|
75
|
+
# specified path.
|
76
|
+
#
|
77
|
+
# @since 1.0.0
|
78
|
+
#
|
79
|
+
def ObjectLoader.is_loading?(path)
|
80
|
+
!(loading(path).nil?)
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# Loads all object blocks from a specific path.
|
85
|
+
#
|
86
|
+
# @param [String] path
|
87
|
+
# The path to load all object blocks from.
|
88
|
+
#
|
89
|
+
# @return [PendingObject]
|
90
|
+
# The pending object which contains the blocks.
|
91
|
+
#
|
92
|
+
# @since 1.0.0
|
93
|
+
#
|
94
|
+
def ObjectLoader.load_blocks(path)
|
95
|
+
path = File.expand_path(path)
|
96
|
+
|
97
|
+
unless File.file?(path)
|
98
|
+
raise(ObjectNotFound,"#{path.dump} doest not exist",caller)
|
99
|
+
end
|
100
|
+
|
101
|
+
# prevent circular loading of objects
|
102
|
+
unless is_pending?
|
103
|
+
# push on the new pending object
|
104
|
+
queue.unshift(PendingObject.new(path))
|
105
|
+
|
106
|
+
begin
|
107
|
+
load(path)
|
108
|
+
rescue Exception => e
|
109
|
+
# if any error is encountered, pop off the object
|
110
|
+
queue.shift
|
111
|
+
raise(e)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# pop off and return the pending object
|
116
|
+
pending_object = queue.shift
|
117
|
+
|
118
|
+
yield pending_object if block_given?
|
119
|
+
return pending_object
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Loads all objects from a specific path.
|
124
|
+
#
|
125
|
+
# @param [String] path
|
126
|
+
# The path to load all objects from.
|
127
|
+
#
|
128
|
+
# @return [Array]
|
129
|
+
# The array of loaded objects.
|
130
|
+
#
|
131
|
+
# @example
|
132
|
+
# ObjectLoader.load_objects('/path/to/misc_object.rb')
|
133
|
+
# # => [...]
|
134
|
+
#
|
135
|
+
# @since 1.0.0
|
136
|
+
#
|
137
|
+
def ObjectLoader.load_objects(path)
|
138
|
+
new_objects = []
|
139
|
+
|
140
|
+
load_blocks(path) do |pending|
|
141
|
+
pending.each do |klass,block|
|
142
|
+
new_object = klass.new
|
143
|
+
new_object.instance_eval(&block)
|
144
|
+
|
145
|
+
yield new_object if block_given?
|
146
|
+
new_objects << new_object
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
return new_objects
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ObjectLoader
|
2
|
+
class PendingObject < Hash
|
3
|
+
|
4
|
+
# The path being loaded
|
5
|
+
attr_reader :path
|
6
|
+
|
7
|
+
#
|
8
|
+
# Creates a new {PendingObject} object.
|
9
|
+
#
|
10
|
+
# @param [String] path
|
11
|
+
# The path the pending object was loaded from.
|
12
|
+
#
|
13
|
+
def initialize(path)
|
14
|
+
@path = File.expand_path(path)
|
15
|
+
|
16
|
+
super()
|
17
|
+
end
|
18
|
+
|
19
|
+
alias each_class each_key
|
20
|
+
alias each_block each_value
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
begin
|
4
|
+
Ore::Specification.new do |gemspec|
|
5
|
+
# custom logic here
|
6
|
+
end
|
7
|
+
rescue NameError
|
8
|
+
begin
|
9
|
+
require 'ore/specification'
|
10
|
+
retry
|
11
|
+
rescue LoadError
|
12
|
+
STDERR.puts "The '#{__FILE__}' file requires Ore."
|
13
|
+
STDERR.puts "Run `gem install ore-core` to install Ore."
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'object_loader'
|
2
|
+
|
3
|
+
class BookReview
|
4
|
+
|
5
|
+
include ObjectLoader
|
6
|
+
|
7
|
+
# Title of the book
|
8
|
+
attr_accessor :book_title
|
9
|
+
|
10
|
+
# Author of the book
|
11
|
+
attr_accessor :book_author
|
12
|
+
|
13
|
+
# Author of this review
|
14
|
+
attr_accessor :author
|
15
|
+
|
16
|
+
# Summary
|
17
|
+
attr_accessor :summary
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Book.object do
|
2
|
+
|
3
|
+
@title = 'Neuromancer'
|
4
|
+
@author = 'William Gibson'
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
BookReview.object do
|
9
|
+
|
10
|
+
@book_title = 'Neuromancer'
|
11
|
+
@book_author = 'William Gibson'
|
12
|
+
|
13
|
+
@author = 'postmodern'
|
14
|
+
@summary = %{
|
15
|
+
Classic cyber-punk book. Provides you with a very realistic and gritty
|
16
|
+
vision of the future and the characters that inhabit it.
|
17
|
+
}
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'helpers/book'
|
2
|
+
require 'helpers/text_book'
|
3
|
+
require 'helpers/book_review'
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
module Objects
|
7
|
+
DIR = File.expand_path(File.join(File.dirname(__FILE__),'objects'))
|
8
|
+
|
9
|
+
def object_path(name)
|
10
|
+
File.join(Helpers::Objects::DIR,"#{name}.rb")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'helpers/objects'
|
3
|
+
|
4
|
+
require 'object_loader'
|
5
|
+
|
6
|
+
describe ObjectLoader do
|
7
|
+
include Helpers::Objects
|
8
|
+
|
9
|
+
let(:snow_crash_path) { object_path(:snow_crash) }
|
10
|
+
let(:discrete_structures_path) { object_path(:discrete_structures) }
|
11
|
+
let(:neuromancer_path) { object_path(:neuromancer_review) }
|
12
|
+
|
13
|
+
let(:syntax_error_path) { object_path(:syntax_error) }
|
14
|
+
let(:load_error_path) { object_path(:load_error) }
|
15
|
+
let(:name_error_path) { object_path(:name_error) }
|
16
|
+
let(:no_method_error_path) { object_path(:no_method_error) }
|
17
|
+
|
18
|
+
it "should raise ObjectNotFound when loading from non-existant files" do
|
19
|
+
lambda {
|
20
|
+
Book.load_object('not_here.rb')
|
21
|
+
}.should raise_error(ObjectLoader::ObjectNotFound)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should load arbitrary blocks from a file" do
|
25
|
+
blocks = ObjectLoader.load_blocks(snow_crash_path)
|
26
|
+
|
27
|
+
blocks.should_not be_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should recover from SyntaxError exceptions" do
|
31
|
+
lambda {
|
32
|
+
ObjectLoader.load_blocks(syntax_error_path)
|
33
|
+
}.should raise_error(SyntaxError)
|
34
|
+
|
35
|
+
ObjectLoader.loading(syntax_error_path).should be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should recover from LoadError exceptions" do
|
39
|
+
lambda {
|
40
|
+
ObjectLoader.load_blocks(load_error_path)
|
41
|
+
}.should raise_error(LoadError)
|
42
|
+
|
43
|
+
ObjectLoader.loading(load_error_path).should be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should recover from NameError exceptions" do
|
47
|
+
lambda {
|
48
|
+
ObjectLoader.load_blocks(name_error_path)
|
49
|
+
}.should raise_error(NameError)
|
50
|
+
|
51
|
+
ObjectLoader.loading(name_error_path).should be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should recover from NoMethodError exceptions" do
|
55
|
+
lambda {
|
56
|
+
ObjectLoader.load_blocks(no_method_error_path)
|
57
|
+
}.should raise_error(NoMethodError)
|
58
|
+
|
59
|
+
ObjectLoader.loading(no_method_error_path).should be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should load a block for a specific Class from a file" do
|
63
|
+
block = Book.load_object_block(snow_crash_path)
|
64
|
+
|
65
|
+
block.should_not be_nil
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should provide class-level methods for loading an object" do
|
69
|
+
book = Book.load_object(snow_crash_path)
|
70
|
+
|
71
|
+
book.should_not be_nil
|
72
|
+
book.class.should == Book
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should load objects for a specific Class from a file" do
|
76
|
+
book = Book.load_object(snow_crash_path)
|
77
|
+
|
78
|
+
book.should_not be_nil
|
79
|
+
book.class.should == Book
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should load the object of a specific Class from a file with multiple objects" do
|
83
|
+
review = BookReview.load_object(neuromancer_path)
|
84
|
+
|
85
|
+
review.should_not be_nil
|
86
|
+
review.class.should == BookReview
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should not load objects if none are of a specific Class" do
|
90
|
+
book = Book.load_object(neuromancer_path)
|
91
|
+
|
92
|
+
book.should_not be_nil
|
93
|
+
book.class.should == Book
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should load the object which inherit from a specific Class" do
|
97
|
+
book = Book.load_object(discrete_structures_path)
|
98
|
+
|
99
|
+
book.should_not be_nil
|
100
|
+
book.class.should == TextBook
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should return nil when loading objects incompatible with the Class" do
|
104
|
+
BookReview.load_object(snow_crash_path).should be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "loaded objects" do
|
108
|
+
before(:all) do
|
109
|
+
@book = Book.load_object(snow_crash_path)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should have attributes" do
|
113
|
+
@book.title.should == 'Snow Crash'
|
114
|
+
@book.author.should == 'Neal Stephenson'
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should have instance methods" do
|
118
|
+
@book.respond_to?(:rating).should == true
|
119
|
+
@book.rating.should == 10
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Postmodern
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-22 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: ore-tasks
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0.4"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "2.4"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: yard
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.6.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
description: ObjectLoader can load Ruby Objects containing methods and procs from Ruby files without having to use YAML or define classes named like the file.
|
50
|
+
email:
|
51
|
+
- postmodern.mod3@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files:
|
57
|
+
- README.md
|
58
|
+
files:
|
59
|
+
- .gemtest
|
60
|
+
- .rspec
|
61
|
+
- .yardopts
|
62
|
+
- ChangeLog.md
|
63
|
+
- LICENSE.txt
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- gemspec.yml
|
67
|
+
- lib/object_loader.rb
|
68
|
+
- lib/object_loader/class_methods.rb
|
69
|
+
- lib/object_loader/exceptions.rb
|
70
|
+
- lib/object_loader/exceptions/object_not_found.rb
|
71
|
+
- lib/object_loader/object_loader.rb
|
72
|
+
- lib/object_loader/pending_object.rb
|
73
|
+
- lib/object_loader/version.rb
|
74
|
+
- object_loader.gemspec
|
75
|
+
- spec/helpers/book.rb
|
76
|
+
- spec/helpers/book_review.rb
|
77
|
+
- spec/helpers/objects.rb
|
78
|
+
- spec/helpers/objects/discrete_structures.rb
|
79
|
+
- spec/helpers/objects/load_error.rb
|
80
|
+
- spec/helpers/objects/name_error.rb
|
81
|
+
- spec/helpers/objects/neuromancer_review.rb
|
82
|
+
- spec/helpers/objects/no_method_error.rb
|
83
|
+
- spec/helpers/objects/snow_crash.rb
|
84
|
+
- spec/helpers/objects/syntax_error.rb
|
85
|
+
- spec/helpers/text_book.rb
|
86
|
+
- spec/object_loader_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
has_rdoc: yard
|
89
|
+
homepage: http://github.com/postmodern/object_loader
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: "0"
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project: object_loader
|
112
|
+
rubygems_version: 1.6.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Loads Ruby Objects containing methods and procs from Ruby files.
|
116
|
+
test_files:
|
117
|
+
- spec/object_loader_spec.rb
|