Module: Bagman::Document

Extended by:
ActiveSupport::Concern
Defined in:
lib/bagman/document.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CryptoPocketKey =
'_crypto'

Instance Method Summary collapse

Instance Method Details

#as_json(opts = {}) ⇒ Object



112
113
114
115
116
# File 'lib/bagman/document.rb', line 112

def as_json(opts={})
  bag.dup.tap {|b|
    b.id = id
  }
end

#bagObject



14
15
16
# File 'lib/bagman/document.rb', line 14

def bag
  @bag ||= decode_or_initialize_bag
end

#bag=(bag) ⇒ Object



19
20
21
# File 'lib/bagman/document.rb', line 19

def bag=(bag)
  @bag = AngryHash[bag]
end

#crypto_pocketObject

crypto pocket #



57
58
59
# File 'lib/bagman/document.rb', line 57

def crypto_pocket
  @crypto_pocket ||= decrypt_crypto_pocket
end

#crypto_pocket=(bag) ⇒ Object



62
63
64
# File 'lib/bagman/document.rb', line 62

def crypto_pocket=(bag)
  @crypto_pocket = AngryHash[bag] if bag
end

#decode_or_initialize_bagObject



24
25
26
27
28
29
30
31
32
# File 'lib/bagman/document.rb', line 24

def decode_or_initialize_bag
   begin
     AngryHash[ ActiveSupport::JSON.decode( read_attribute('bag') ) ]
   rescue
     initialize_bag(AngryHash.new)
   end.tap {|h|
     h.extend self.class.bag.top_level_mixin
   }
end

#decrypt_crypto_pocketObject



67
68
69
70
71
72
73
74
75
# File 'lib/bagman/document.rb', line 67

def decrypt_crypto_pocket
  if crypto_pocket = bag[CryptoPocketKey]
    ActiveSupport::JSON.decode( encryptor.dec(crypto_pocket) )
  else
    {}
  end
rescue
  {}
end

#encrypt_crypto_pocketObject



78
79
80
81
82
# File 'lib/bagman/document.rb', line 78

def encrypt_crypto_pocket
  if @crypto_pocket.is_a?(Hash)
    bag[CryptoPocketKey] = encryptor.enc(@crypto_pocket.to_json)
  end
end

#encryptorObject



85
86
87
88
89
90
91
# File 'lib/bagman/document.rb', line 85

def encryptor
  @encryptor ||= begin
                   cfg = Davidson.app_config
                   password = cfg.crypto.password || cfg.missing!('crypto.password')
                   Gibberish::AES.new(password)
                 end
end

#fill_bag_from(source) ⇒ Object

Fills a bag with data from a source document, setting only those values present in the target document



96
97
98
99
100
101
102
103
# File 'lib/bagman/document.rb', line 96

def fill_bag_from(source)
  self.class.bag.columns.each do |column|
    column_name = column.name
    if source.respond_to?(column_name) && self.send(column_name).blank?
      self.send("#{column_name}=", source.send(column_name)) 
    end
  end
end

#initialize_bag(bag) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/bagman/document.rb', line 35

def initialize_bag(bag)
  bag.tap do |b|
    self.class.bag.columns.select { |c| c.options.has_key?(:default) }.each do |column|
      b[column.name] = column.options[:default]
    end
  end 
end

#reload(*args) ⇒ Object



106
107
108
109
# File 'lib/bagman/document.rb', line 106

def reload(*args)
  @bag = nil
  super
end

#serialize_bagObject



44
45
46
47
48
49
# File 'lib/bagman/document.rb', line 44

def serialize_bag
  if @bag
    encrypt_crypto_pocket
    write_attribute(:bag, ActiveSupport::JSON.encode(@bag))
  end
end