ygg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +4 -0
  3. data/Rakefile +2 -0
  4. data/lib/ygg.rb +224 -0
  5. data/lib/ygg/version.rb +3 -0
  6. data/ygg.gemspec +21 -0
  7. metadata +53 -0
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ data/*
6
+ test/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ygg.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/lib/ygg.rb ADDED
@@ -0,0 +1,224 @@
1
+ require "yaml"
2
+ require "singleton"
3
+
4
+ module Ygg
5
+
6
+ # Mappers for simplicity
7
+ def self.load(file)
8
+ Ygg::DSL.instance.eagerly file
9
+ end
10
+
11
+ def self.save
12
+ Ygg::DSL.instance.save
13
+ end
14
+
15
+ def self.rel(*args)
16
+ Ygg::DSL.instance.rel *args
17
+ end
18
+
19
+ module Delegator
20
+ def delegate(target, *methods)
21
+ methods.each do |method|
22
+ self.class.send :define_method, method do |*args, &block|
23
+ target.send method, *args, &block
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ class Relationship
30
+ attr_accessor :source
31
+ attr_accessor :target
32
+
33
+ def initialize(source, target)
34
+ @source = source
35
+ @target = target
36
+
37
+ short = Ygg::DSL.instance.data
38
+ short[:relationship] ||= []
39
+ short[:relationship] << self
40
+ end
41
+ end
42
+
43
+ class DSL
44
+ include Singleton
45
+
46
+ attr_accessor :data
47
+ attr_accessor :source
48
+
49
+ def eagerly(source)
50
+ @source = source
51
+ if File.exists? @source
52
+ @data = YAML.load_file @source
53
+ else
54
+ @data = {}
55
+ end
56
+ end
57
+
58
+ def save # Maybe get it better
59
+ source = @source if @source
60
+ unless source
61
+ raise "There's already a file named 'ygg.yaml'." if File.exists? "ygg.yaml"
62
+ source = "ygg.yaml"
63
+ end
64
+
65
+ File.open source, "w" do |file|
66
+ file.write YAML.dump @data
67
+ end
68
+ end
69
+
70
+ def record(model, id)
71
+ model_sym = model.to_s.downcase.to_sym
72
+ @data[model_sym][id]
73
+ end
74
+
75
+ def add(resource, son)
76
+ Ygg::Relationship.new resource, son
77
+ end
78
+
79
+ def _source(relation, resource, type = nil)
80
+ if relation.source == resource
81
+ if type == :all
82
+ return relation.target
83
+ elsif relation.target.class == type
84
+ return relation.target
85
+ end
86
+ end
87
+ return false
88
+ end
89
+
90
+ def _target(relation, resource, type = nil)
91
+ if relation.target == resource
92
+ if type == :all
93
+ return relation.source
94
+ elsif relation.source.class == type
95
+ return relation.source
96
+ end
97
+ end
98
+ return false
99
+ end
100
+
101
+ def rel(resource, options = {})
102
+ options[:as] ||= :any
103
+ options[:type] ||= :all
104
+ result = []
105
+ @data[:relationship].each do |relation|
106
+ args = [relation, resource, options[:type]]
107
+ case options[:as]
108
+ when :any
109
+ if res = _source(*args)
110
+ result << res
111
+ elsif res = _target(*args)
112
+ result << res
113
+ end
114
+ when :source
115
+ if res = _source(*args)
116
+ result << res
117
+ end
118
+ when :target
119
+ if res = _target(*args)
120
+ result << res
121
+ end
122
+ end
123
+ end
124
+ result
125
+ end
126
+ end
127
+
128
+ class Model
129
+ # Define methods for retrieving relations and
130
+ # creating relations
131
+
132
+ attr_accessor :index
133
+
134
+ # Instance methods
135
+ def >>(resource)
136
+ Ygg::DSL.instance.add self, resource
137
+ end
138
+
139
+ def <<(resource)
140
+ Ygg::DSL.instance.add resource, self
141
+ end
142
+
143
+ def %(type)
144
+ if type == :all
145
+ Ygg::DSL.instance.rel self
146
+ elsif type.kind_of? Class
147
+ Ygg::DSL.instance.rel self, :type => type
148
+ end
149
+ end
150
+
151
+ def >=(type)
152
+ if type == :all
153
+ Ygg::DSL.instance.rel self, :as => :source
154
+ elsif type.kind_of? Class
155
+ Ygg::DSL.instance.rel self, :as => :source, :type => type
156
+ end
157
+ end
158
+
159
+ def <=(type)
160
+ if type == :all
161
+ Ygg::DSL.instance.rel self, :as => :target
162
+ elsif type.kind_of? Class
163
+ Ygg::DSL.instance.rel self, :as => :target, :type => type
164
+ end
165
+ end
166
+
167
+ # Register this datus in the @data
168
+ def initialize(index)
169
+ Ygg::DSL.instance.data ||= {}
170
+ Ygg::DSL.instance.data[Ygg.symbolize(self)] ||= {}
171
+ if index.kind_of? String
172
+ Ygg::DSL.instance.data[Ygg.symbolize(self)][index] = self
173
+ @index = index
174
+ elsif index.kind_of? Hash
175
+ raise "Index missing" unless index[:index]
176
+ index.each do |key, value|
177
+ self.send "#{key}=".to_sym, value
178
+ end
179
+ end
180
+ end
181
+
182
+ # Class methods
183
+ def self.create(data)
184
+ if data.kind_of? Hash
185
+ raise "Index missing" unless data[:index]
186
+ index = data[:index]
187
+ elsif data.kind_of? String
188
+ index = data
189
+ end
190
+ instance = self.new index
191
+
192
+ if data.kind_of? Hash
193
+ data.each do |key, value|
194
+ instance.send "#{key}=".to_sym, value
195
+ end
196
+ end
197
+ Ygg::DSL.instance.save
198
+ instance
199
+ end
200
+
201
+ def self.all
202
+ Ygg::DSL.instance.data[self.to_s.downcase.to_sym] ||= {}
203
+ end
204
+
205
+ def self.get(index)
206
+ Ygg::DSL.instance.record(self, index)
207
+ end
208
+
209
+ def self.related_to(datus, as = nil)
210
+ options = {}
211
+ options[:type] = self
212
+ options[:as] = as if as
213
+ Ygg::DSL.instance.rel datus, options
214
+ end
215
+ end
216
+
217
+ def self.symbolize(object)
218
+ object.class.to_s.downcase.to_sym
219
+ end
220
+ end
221
+
222
+ #include Ygg::Delegator
223
+
224
+ #delegate Ygg::DSL, :save, :record, :relateds
@@ -0,0 +1,3 @@
1
+ module Ygg
2
+ VERSION = "0.0.1"
3
+ end
data/ygg.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ygg/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ygg"
7
+ s.version = Ygg::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Xavier Via"]
10
+ s.email = ["xavierviacanel@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Pure Ruby persistence solution}
13
+ s.description = %q{Pure Ruby persistence solution}
14
+
15
+ s.rubyforge_project = "ygg"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ygg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Xavier Via
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-30 00:00:00.000000000 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Pure Ruby persistence solution
16
+ email:
17
+ - xavierviacanel@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - Rakefile
25
+ - lib/ygg.rb
26
+ - lib/ygg/version.rb
27
+ - ygg.gemspec
28
+ has_rdoc: true
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: ygg
49
+ rubygems_version: 1.5.2
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Pure Ruby persistence solution
53
+ test_files: []