the-blob 0.0.1
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/lib/handlers/instance_handler.rb +70 -0
- data/lib/handlers/link_handler.rb +25 -0
- data/lib/the_blob/instance.rb +7 -0
- data/lib/the_blob/instance_methods.rb +31 -0
- data/lib/the_blob.rb +13 -0
- metadata +49 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path '../../the_blob/instance', __FILE__
|
2
|
+
|
3
|
+
module InstanceHandler
|
4
|
+
|
5
|
+
class InstanceNotFound < RuntimeError; end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
#TODO move this into seperate class
|
10
|
+
def load_instances
|
11
|
+
dir = File.expand_path("app/instances", ".")
|
12
|
+
Dir["#{dir}/*"].each do |file|
|
13
|
+
puts file
|
14
|
+
require file
|
15
|
+
create_methods(file)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_methods(file)
|
20
|
+
klass = file.scan(/\/([^\/]+)\.rb/).first.first
|
21
|
+
make_class_getter(klass)
|
22
|
+
make_index_getters(klass)
|
23
|
+
end
|
24
|
+
|
25
|
+
def make_class_getter(klass)
|
26
|
+
class_eval <<-CODE
|
27
|
+
def #{klass}s
|
28
|
+
instance_lists["#{klass.capitalize}"]
|
29
|
+
end
|
30
|
+
CODE
|
31
|
+
end
|
32
|
+
|
33
|
+
def make_index_getters(klass)
|
34
|
+
Module.const_get(klass.capitalize).get_instance_indices.each do |index|
|
35
|
+
class_eval <<-CODE
|
36
|
+
def emit_#{klass}_by_#{index}(id)
|
37
|
+
instance = instances["#{klass.capitalize}"]["__#{index}"][id]
|
38
|
+
instance || raise(InstanceNotFound)
|
39
|
+
end
|
40
|
+
CODE
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.included(base)
|
47
|
+
base.extend(ClassMethods)
|
48
|
+
base.load_instances
|
49
|
+
end
|
50
|
+
|
51
|
+
def absorb(instance)
|
52
|
+
klass = instance.class
|
53
|
+
instance_lists[klass.to_s] << instance
|
54
|
+
klass.get_instance_indices.each do |index|
|
55
|
+
instances[klass.to_s]["__#{index}"][instance.send(index)] = instance
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def instances
|
62
|
+
hash = proc{|a| Hash.new{|hash, key| hash[key] = a} }
|
63
|
+
@instances ||= hash.call( hash.call( hash.call( nil )))
|
64
|
+
end
|
65
|
+
|
66
|
+
def instance_lists
|
67
|
+
@instance_lists ||= Hash.new{|hash, key| hash[key] = Array.new }
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module LinkHandler
|
2
|
+
|
3
|
+
def create_link (instance_1, instance_2)
|
4
|
+
links[instance_1][instance_2.class] << instance_2
|
5
|
+
links[instance_2][instance_1.class] << instance_1
|
6
|
+
nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def delete_link(instance_1, instance_2)
|
10
|
+
links[instance_1][instance_2.class].delete(instance_2)
|
11
|
+
links[instance_2][instance_1.class].delete(instance_1)
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_links(instance, klass)
|
16
|
+
links[instance][klass].dup
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def links
|
22
|
+
@links ||= Hash.new{|hash, key| hash[key] = Hash.new{|hash, key| hash[key] = []} }
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module InstanceMethods
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.instance_eval do
|
5
|
+
def self.get_next_id
|
6
|
+
@next_id ||= 0
|
7
|
+
@next_id += 1
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.instance_indices(*ind)
|
11
|
+
indices = ind << :id
|
12
|
+
attr_accessor *indices
|
13
|
+
@instance_indices = indices
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get_instance_indices
|
17
|
+
@instance_indices ||= []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(attrs = {})
|
23
|
+
@id = self.class.get_next_id
|
24
|
+
attrs.each{ |name, value| name.inspect; send("#{name}=", value) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def id
|
28
|
+
@id
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/the_blob.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path '../handlers/instance_handler', __FILE__
|
2
|
+
require File.expand_path '../handlers/link_handler', __FILE__
|
3
|
+
|
4
|
+
module TheBlob
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
include LinkHandler
|
9
|
+
include InstanceHandler
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: the-blob
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kelly Stannard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: The blob provides hash-based instance handling.
|
15
|
+
email: kwstannard@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/the_blob.rb
|
21
|
+
- lib/handlers/instance_handler.rb
|
22
|
+
- lib/handlers/link_handler.rb
|
23
|
+
- lib/the_blob/instance.rb
|
24
|
+
- lib/the_blob/instance_methods.rb
|
25
|
+
homepage: https://github.com/kwstannard/the_blob
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.24
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: The blob provides hash-based instance handling.
|
49
|
+
test_files: []
|