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(bag_string = nil) ⇒ 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
- #mixin_top_level_mixin(hash = @bag) ⇒ Object
- #reload(*args) ⇒ Object
- #serialize_bag ⇒ Object
Instance Method Details
#as_json(opts = {}) ⇒ Object
129 130 131 132 133 |
# File 'lib/bagman/document.rb', line 129 def as_json(opts={}) bag.dup.tap {|b| b.id = id } end |
#bag ⇒ Object
15 16 17 |
# File 'lib/bagman/document.rb', line 15 def bag @bag ||= decode_or_initialize_bag end |
#bag=(bag) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bagman/document.rb', line 20 def bag=(bag) case bag when NilClass @bag = AngryHash.new when String @bag = decode_or_initialize_bag(bag) else @bag = AngryHash[bag] end.tap { mixin_top_level_mixin } end |
#crypto_pocket ⇒ Object
crypto pocket #
74 75 76 |
# File 'lib/bagman/document.rb', line 74 def crypto_pocket @crypto_pocket ||= decrypt_crypto_pocket end |
#crypto_pocket=(bag) ⇒ Object
79 80 81 |
# File 'lib/bagman/document.rb', line 79 def crypto_pocket=(bag) @crypto_pocket = AngryHash[bag] if bag end |
#decode_or_initialize_bag(bag_string = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bagman/document.rb', line 34 def decode_or_initialize_bag(bag_string=nil) begin bag_string ||= read_attribute('bag') AngryHash[ ActiveSupport::JSON.decode( bag_string ) ] rescue initialize_bag(AngryHash.new) end.tap {|bag| mixin_top_level_mixin(bag) } end |
#decrypt_crypto_pocket ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/bagman/document.rb', line 84 def decrypt_crypto_pocket if crypto_pocket = bag[CryptoPocketKey] ActiveSupport::JSON.decode( encryptor.dec(crypto_pocket) ) else {} end rescue {} end |
#encrypt_crypto_pocket ⇒ Object
95 96 97 98 99 |
# File 'lib/bagman/document.rb', line 95 def encrypt_crypto_pocket if @crypto_pocket.is_a?(Hash) bag[CryptoPocketKey] = encryptor.enc(@crypto_pocket.to_json) end end |
#encryptor ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/bagman/document.rb', line 102 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
113 114 115 116 117 118 119 120 |
# File 'lib/bagman/document.rb', line 113 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
51 52 53 54 55 56 57 |
# File 'lib/bagman/document.rb', line 51 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 |
#mixin_top_level_mixin(hash = @bag) ⇒ Object
46 47 48 |
# File 'lib/bagman/document.rb', line 46 def mixin_top_level_mixin(hash=@bag) hash.extend self.class.bag.top_level_mixin unless hash.__angry_hash_extension # XXX angry hash should have an API for this end |
#reload(*args) ⇒ Object
123 124 125 126 |
# File 'lib/bagman/document.rb', line 123 def reload(*args) @bag = nil super end |
#serialize_bag ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/bagman/document.rb', line 60 def serialize_bag if @bag encrypt_crypto_pocket write_attribute(:bag, ActiveSupport::JSON.encode(@bag)) @bag = nil end end |