Class: Bagman::Bag
- Inherits:
-
Object
- Object
- Bagman::Bag
- Defined in:
- lib/bagman/bag.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
(also: #fields)
readonly
Returns the value of attribute columns.
-
#target_class ⇒ Object
readonly
Returns the value of attribute target_class.
-
#top_level_mixin ⇒ Object
readonly
Returns the value of attribute top_level_mixin.
Class Method Summary collapse
Instance Method Summary collapse
- #bag_field(name, type, options, &blk) ⇒ Object
- #collection(name, type, options = {}, &blk) ⇒ Object
- #crypto_field(name, type, options, &blk) ⇒ Object
- #field(name, *args, &blk) ⇒ Object
-
#index ⇒ Object
Indices.
-
#initialize(target_class, &blk) ⇒ Bag
constructor
A new instance of Bag.
-
#materialise ⇒ Object
Materialise.
- #unbagged_field(name, type, options, &blk) ⇒ Object
- #validate ⇒ Object
-
#validates_presence_of ⇒ Object
Validations.
- #validates_storage_type_of ⇒ Object
Constructor Details
#initialize(target_class, &blk) ⇒ Bag
Returns a new instance of Bag.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/bagman/bag.rb', line 5 def initialize(target_class,&blk) @target_class = target_class @top_level_mixin = Module.new do include AngryHash::Extension end @columns = [] instance_eval(&blk) end |
Instance Attribute Details
#columns ⇒ Object (readonly) Also known as: fields
Returns the value of attribute columns.
3 4 5 |
# File 'lib/bagman/bag.rb', line 3 def columns @columns end |
#target_class ⇒ Object (readonly)
Returns the value of attribute target_class.
3 4 5 |
# File 'lib/bagman/bag.rb', line 3 def target_class @target_class end |
#top_level_mixin ⇒ Object (readonly)
Returns the value of attribute top_level_mixin.
3 4 5 |
# File 'lib/bagman/bag.rb', line 3 def top_level_mixin @top_level_mixin end |
Class Method Details
.serialise_value(value, options) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/bagman/bag.rb', line 109 def self.serialise_value(value, ) if s = [:serialize] s[value] elsif value.is_a? Date value.to_s(:db) else value.to_s end end |
Instance Method Details
#bag_field(name, type, options, &blk) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bagman/bag.rb', line 34 def bag_field(name, type, , &blk) @columns << (column = SimpleColumn.new(name, :bag, type, )) target_class.send :define_method, name do column.type_cast( self.bag[name] ) end if type == :boolean target_class.send :define_method, "#{name}?" do column.type_cast( self.bag[name] ) end end target_class.send :define_method, "#{name}=" do |value| if index_name = column.index_name write_attribute(index_name, value) end self.bag[name] = if s = [:serialize] s[value] elsif value.is_a? Date value.to_s(:db) else value.to_s end end end |
#collection(name, type, options = {}, &blk) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bagman/bag.rb', line 120 def collection(name,type,={},&blk) @top_level_mixin.module_eval do extend_array name, type end target_class.send :define_method, name do self.bag[name] end target_class.send :define_method, "#{name}=" do |value| if value.is_a? Hash value = value.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes } end self.bag[name] = value end end |
#crypto_field(name, type, options, &blk) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bagman/bag.rb', line 63 def crypto_field(name, type, , &blk) name = name.to_s @columns << (column = SimpleColumn.new(name, :crypto, type, )) target_class.send :define_method, name do column.type_cast( self.crypto_pocket[name] ) end target_class.send :define_method, "#{name}_before_type_cast" do self.crypto_pocket[name] end if [:shadow] target_class.send :define_method, "#{name}_shadow" do read_attribute(name) end end target_class.send :define_method, "#{name}=" do |value| # we need to flag bag as dirty, or we're never saved. self.bag_will_change! self.crypto_pocket[name] = final_value = Bagman::Bag.serialise_value(value, ) if pepper = [:shadow] shadowed = case pepper when String Gibberish::SHA256( pepper + '--' + final_value ) when Symbol send(pepper, final_value) else Gibberish::SHA256( final_value ) end write_attribute(name, shadowed) end end end |
#field(name, *args, &blk) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bagman/bag.rb', line 19 def field(name,*args,&blk) = args. type = args.shift || :string if [:unbagged] # no-op # unbagged_field(name, type, options, &blk) elsif [:encrypted] crypto_field(name, type, , &blk) else bag_field(name, type, , &blk) end end |
#index ⇒ Object
Indices
139 140 |
# File 'lib/bagman/bag.rb', line 139 def index(*) end |
#materialise ⇒ Object
Materialise
143 144 |
# File 'lib/bagman/bag.rb', line 143 def materialise(*) end |
#unbagged_field(name, type, options, &blk) ⇒ Object
104 105 |
# File 'lib/bagman/bag.rb', line 104 def unbagged_field(name, type, , &blk) end |
#validate ⇒ Object
153 154 |
# File 'lib/bagman/bag.rb', line 153 def validate(*) end |
#validates_presence_of ⇒ Object
Validations
147 148 |
# File 'lib/bagman/bag.rb', line 147 def validates_presence_of(*) end |
#validates_storage_type_of ⇒ Object
150 151 |
# File 'lib/bagman/bag.rb', line 150 def validates_storage_type_of(*) end |