filebase 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/attributes.rb +48 -0
  2. data/lib/filebase.rb +11 -0
  3. data/lib/model.rb +81 -0
  4. metadata +63 -0
data/lib/attributes.rb ADDED
@@ -0,0 +1,48 @@
1
+ module Attributes
2
+
3
+ def initialize(hash = {} )
4
+ attributes = hash
5
+ end
6
+
7
+ def attributes=( hash )
8
+ # copy the hash, converting all keys to strings
9
+ @attrs = hash.inject({}) { |h,p| k,v = p; h[k.to_s] = v; h }
10
+ end
11
+
12
+ def attributes
13
+ @attrs ||= {}
14
+ end
15
+
16
+ def method_missing(name,*args)
17
+ name = name.to_s
18
+ if name =~/=$/ and args.length == 1
19
+ set(name[0..-2], args[0] )
20
+ else
21
+ get(name)
22
+ end
23
+ end
24
+
25
+ def [](name)
26
+ get(name)
27
+ end
28
+
29
+ def []=(name,val)
30
+ set(name,val)
31
+ end
32
+
33
+ def set(name, val)
34
+ attributes[name.to_s] = val
35
+ end
36
+
37
+ def get(name)
38
+ rval = attributes[name.to_s]
39
+ rval.is_a?( Hash ) ? Attributes.new( rval ) : rval
40
+ end
41
+
42
+ def to_h
43
+ @attrs
44
+ end
45
+
46
+ alias_method :to_hash, :to_h
47
+
48
+ end
data/lib/filebase.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'drivers/yaml'
3
+ class Filebase
4
+ class << self ; attr_accessor :storage ; end
5
+ self.storage = YAML
6
+ def initialize( root = '.', storage = nil )
7
+ @storage = ( storage || Filebase.storage ).new( root )
8
+ end
9
+ # delegate everything to the storage object ...
10
+ def method_missing( name,*args ) ; @storage.send( name, *args ) ; end
11
+ end
data/lib/model.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'attributes'
2
+
3
+ class Filebase
4
+
5
+ Error = RuntimeError
6
+
7
+ module Model
8
+
9
+ def self.[]( path )
10
+ Module.new do |mixin|
11
+ ( class << mixin ; self ; end ).module_eval do
12
+ define_method( :included ) do | model |
13
+ model.module_eval do
14
+ @db = Filebase.new( path )
15
+ extend Mixins::ClassMethods ; include Attributes ; include Mixins::InstanceMethods
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module Mixins
23
+
24
+ module ClassMethods
25
+ attr_accessor :db
26
+ def create( assigns ) ; save( new( assigns ) ) ; end
27
+ def all ; db.all.map { |attrs| new( attrs ) } ; end
28
+ def find( key ) ; attrs = db.find( key ); new( attrs.merge( :key => key ) ) if attrs ; end
29
+ def save( object )
30
+ raise( Filebase::Error.new, 'attempted to save an object with nil key' ) if object.key.nil? or object.key.empty?
31
+ db.save( object.key, object.to_h )
32
+ end
33
+ def delete( object )
34
+ raise( Filebase::Error.new, 'attempted to delete an object with nil key' ) if object.key.nil? or object.key.empty?
35
+ db.delete( object.key )
36
+ end
37
+ def has_one( name, options = {} )
38
+ module_eval do
39
+ define_method name do
40
+ @has_one ||= {}
41
+ options[:class] ||= Object.module_eval( name.to_s.camel_case )
42
+ @has_one[name] ||= options[:class].find( get( name ) )
43
+ end
44
+ define_method( name.to_s + '=' ) do | val |
45
+ @has_one ||= {}; @has_one[name] = nil
46
+ set( name, String === val ? val : val.key )
47
+ end
48
+ end
49
+ end
50
+ def has_many( name, options = {} )
51
+ module_eval do
52
+ define_method( name ) do
53
+ @has_many ||= {}
54
+ options[:class] ||= Object.module_eval( name.to_s.camel_case )
55
+ @has_many[name] ||= ( get( name ) || [] ).uniq.map { |key| options[:class].find( key ) }
56
+ end
57
+ # when we save, make sure to pick up any changes to the array
58
+ (class<<self;self;end).module_eval do
59
+ old_save = instance_method(:save)
60
+ define_method :save do |object|
61
+ object.set( name, object.send( name ).map{ |x| x.key }.uniq )
62
+ old_save.bind(self).call(object)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ module InstanceMethods
70
+ def initialize( assigns ) ; super ; assign( assigns ) ; end
71
+ def assign( assigns ) ; assigns.each { |k,v| self.send( "#{k}=", v ) }; self ; end
72
+ def save ; self.class.save( self ) ; self; end
73
+ def delete ; self.class.delete( self ) ; self ; end
74
+ def ==(object) ; key == object.key ; end
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filebase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Yoder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: extensions
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description:
25
+ email: dan@zeraweb.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/attributes.rb
34
+ - lib/filebase.rb
35
+ - lib/model.rb
36
+ has_rdoc: true
37
+ homepage: http://dev.zeraweb.com/waves
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.6
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.0.1
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Simple file-based database with model support.
62
+ test_files: []
63
+