filebase 0.2.1 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,19 +5,21 @@ class Filebase
5
5
  class YAML
6
6
 
7
7
  def initialize( root )
8
- @root = root
8
+ @root = root.to_s
9
9
  end
10
10
 
11
11
  def path( key )
12
- @root / key + '.yml'
12
+ File.join( @root, key.to_s ) + '.yml'
13
13
  end
14
14
 
15
15
  def all
16
- Dir[ path('*') ].map { |file| YAML.load( File.read( file ) ) }
16
+ Dir[ path('*') ].map { |file| find( File.basename(file,'.yml' ) ) }.compact
17
17
  end
18
18
 
19
19
  def find( key )
20
- ( ::YAML.load( File.read( path( key ) ) ) if File.exists?( path( key ) ) ) || nil
20
+ obj = ::YAML.load( File.read( path( key ) ) ) rescue nil
21
+ obj['key'] = key if Hash === obj
22
+ obj or nil # convert false to nil
21
23
  end
22
24
 
23
25
  def save( key, object )
@@ -1,47 +1,43 @@
1
1
  module Attributes
2
2
 
3
- def initialize(hash = {} )
4
- attributes = hash
5
- end
3
+ def initialize( hash = {} ) ; self.attributes = hash ; end
6
4
 
7
5
  def attributes=( hash )
8
6
  # copy the hash, converting all keys to strings
9
7
  @attrs = hash.inject({}) { |h,p| k,v = p; h[k.to_s] = v; h }
10
8
  end
11
9
 
12
- def attributes
13
- @attrs ||= {}
14
- end
10
+ def attributes ; @attrs ||= {} ; end
11
+
12
+ def has_key?( key ) ; attributes.has_key?( key.to_s ) ; end
13
+
14
+ def delete( key ) ; attributes.delete( key.to_s ) ; end
15
15
 
16
16
  def method_missing(name,*args)
17
17
  name = name.to_s
18
- if name =~/=$/ and args.length == 1
19
- set(name[0..-2], args[0] )
20
- else
21
- get(name)
18
+ case args.length
19
+ when 0 then get( name )
20
+ when 1
21
+ if name[-1,1] == '='
22
+ set( name[0..-2], args[0] )
23
+ else
24
+ super
25
+ end
26
+ else super
22
27
  end
23
28
  end
24
29
 
25
- def [](name)
26
- get(name)
27
- end
30
+ def [](name) ; get(name) ; end
28
31
 
29
- def []=(name,val)
30
- set(name,val)
31
- end
32
+ def []=(name,val) ; set(name,val) ; end
32
33
 
33
- def set(name, val)
34
- attributes[name.to_s] = val
35
- end
34
+ def set(name, val) ; attributes[name.to_s] = val ; end
36
35
 
37
36
  def get(name)
38
- rval = attributes[name.to_s]
39
- rval.is_a?( Hash ) ? Attributes.new( rval ) : rval
37
+ ( ( rval = attributes[name.to_s] ).is_a?( Hash ) and attributes[name.to_s] = self.class.new( rval ) ) or rval
40
38
  end
41
39
 
42
- def to_h
43
- @attrs
44
- end
40
+ def to_h ; @attrs ; end
45
41
 
46
42
  alias_method :to_hash, :to_h
47
43
 
@@ -25,7 +25,8 @@ class Filebase
25
25
  attr_accessor :db
26
26
  def create( assigns ) ; save( new( assigns ) ) ; end
27
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
28
+ def find( key ) ; attrs = db.find( key ); new( attrs ) if attrs ; end
29
+ def []( key ) ; find( key ) ; end
29
30
  def save( object )
30
31
  raise( Filebase::Error.new, 'attempted to save an object with nil key' ) if object.key.nil? or object.key.empty?
31
32
  db.save( object.key, object.to_h )
@@ -38,7 +39,7 @@ class Filebase
38
39
  module_eval do
39
40
  define_method name do
40
41
  @has_one ||= {}
41
- options[:class] ||= Object.module_eval( name.to_s.camel_case )
42
+ options[:class] ||= Object.module_eval( name.to_s.gsub(/(_)(\w)/) { $2.upcase }.gsub(/^([a-z])/) { $1.upcase } )
42
43
  @has_one[name] ||= options[:class].find( get( name ) )
43
44
  end
44
45
  define_method( name.to_s + '=' ) do | val |
@@ -72,6 +73,8 @@ class Filebase
72
73
  def save ; self.class.save( self ) ; self; end
73
74
  def delete ; self.class.delete( self ) ; self ; end
74
75
  def ==(object) ; key == object.key ; end
76
+ def eql?(object) ; key == object.key ; end
77
+ def hash ; key.hash ; end
75
78
  end
76
79
 
77
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filebase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Yoder