bagman 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,12 @@
1
+ # 0.0.5
2
+
3
+ - Fixed long standing bug where the bag would become something like `'{"serialised":"bag"}': null` [lachie]
4
+
5
+ # 0.0.4
6
+
7
+ - Was never pushed for some reason!
8
+ - `uncast` field type uses `:defaults`. [whalec]
9
+
1
10
  # 0.0.3
2
11
 
3
12
  - Added `bag_field` class method to `Bagman::Document`s. This allows for adding adhoc accessors for conveniently accessing verbatim bag values. [lachie]
@@ -13,6 +13,7 @@ module Bagman
13
13
  instance_eval(&blk)
14
14
  end
15
15
 
16
+
16
17
  alias :fields :columns
17
18
 
18
19
 
@@ -55,6 +56,7 @@ module Bagman
55
56
  write_attribute(index_name, value)
56
57
  end
57
58
 
59
+
58
60
  self.bag[name] = if s = options[:serialize]
59
61
  s[value]
60
62
  elsif value.is_a? Date
@@ -68,9 +70,15 @@ module Bagman
68
70
 
69
71
  def uncast_bag_field(name, type, options, &blk)
70
72
  name = name.to_s
73
+ if default = options[:default]
74
+ default_generator = "__#{name}_default"
75
+ target_class.send :define_method, default_generator do
76
+ default.dup
77
+ end
78
+ end
71
79
 
72
80
  target_class.send :define_method, name do
73
- self.bag[name]
81
+ self.bag[name] || (default_generator && send(default_generator))
74
82
  end
75
83
 
76
84
  target_class.send :define_method, "#{name}=" do |value|
@@ -11,24 +11,40 @@ module Bagman
11
11
  before_save :serialize_bag
12
12
  end
13
13
 
14
+
14
15
  def bag
15
16
  @bag ||= decode_or_initialize_bag
16
17
  end
17
18
 
18
19
 
19
20
  def bag=(bag)
20
- @bag = AngryHash[bag]
21
+ case bag
22
+ when NilClass
23
+ @bag = AngryHash.new
24
+ when String
25
+ @bag = decode_or_initialize_bag(bag)
26
+ else
27
+ @bag = AngryHash[bag]
28
+ end.tap {
29
+ mixin_top_level_mixin
30
+ }
31
+ end
32
+
33
+
34
+ def decode_or_initialize_bag(bag_string=nil)
35
+ begin
36
+ bag_string ||= read_attribute('bag')
37
+ AngryHash[ ActiveSupport::JSON.decode( bag_string ) ]
38
+ rescue
39
+ initialize_bag(AngryHash.new)
40
+ end.tap {|bag|
41
+ mixin_top_level_mixin(bag)
42
+ }
21
43
  end
22
44
 
23
45
 
24
- def decode_or_initialize_bag
25
- begin
26
- AngryHash[ ActiveSupport::JSON.decode( read_attribute('bag') ) ]
27
- rescue
28
- initialize_bag(AngryHash.new)
29
- end.tap {|h|
30
- h.extend self.class.bag.top_level_mixin
31
- }
46
+ def mixin_top_level_mixin(hash=@bag)
47
+ hash.extend self.class.bag.top_level_mixin unless hash.__angry_hash_extension # XXX angry hash should have an API for this
32
48
  end
33
49
 
34
50
 
@@ -45,6 +61,7 @@ module Bagman
45
61
  if @bag
46
62
  encrypt_crypto_pocket
47
63
  write_attribute(:bag, ActiveSupport::JSON.encode(@bag))
64
+ @bag = nil
48
65
  end
49
66
  end
50
67
 
@@ -1,3 +1,3 @@
1
1
  module Bagman
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bagman
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lachie Cox
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-08-11 00:00:00 Z
19
+ date: 2011-09-10 00:00:00 Z
20
20
  dependencies: []
21
21
 
22
22
  description: We built Bagman for a project. We wanted fast prototyping and development, and some level of escape from the schema on relational databases.