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
- #as_json(opts = {}) ⇒ Object
- #bag ⇒ Object
- #bag=(bag) ⇒ Object
-
#crypto_pocket ⇒ Object
crypto pocket #.
- #crypto_pocket=(bag) ⇒ Object
- #decode_or_initialize_bag ⇒ Object
- #decrypt_crypto_pocket ⇒ Object
- #encrypt_crypto_pocket ⇒ Object
- #encryptor ⇒ Object
-
#fill_bag_from(source) ⇒ Object
Fills a bag with data from a source document, setting only those values present in the target document.
- #initialize_bag(bag) ⇒ Object
- #reload(*args) ⇒ Object
- #serialize_bag ⇒ Object
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 |
#bag ⇒ Object
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_pocket ⇒ Object
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_bag ⇒ Object
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_pocket ⇒ Object
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_pocket ⇒ Object
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 |
#encryptor ⇒ Object
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..has_key?(:default) }.each do |column| b[column.name] = column.[: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_bag ⇒ Object
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 |