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
- #uncast_bag_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
136 137 138 139 140 141 142 143 144 |
# File 'lib/bagman/bag.rb', line 136 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bagman/bag.rb', line 41 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
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bagman/bag.rb', line 147 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bagman/bag.rb', line 90 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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bagman/bag.rb', line 20 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) elsif [:uncast] uncast_bag_field(name, type, , &blk) else bag_field(name, type, , &blk) end end |
#index ⇒ Object
Indices
166 167 |
# File 'lib/bagman/bag.rb', line 166 def index(*) end |
#materialise ⇒ Object
Materialise
170 171 |
# File 'lib/bagman/bag.rb', line 170 def materialise(*) end |
#unbagged_field(name, type, options, &blk) ⇒ Object
131 132 |
# File 'lib/bagman/bag.rb', line 131 def unbagged_field(name, type, , &blk) end |
#uncast_bag_field(name, type, options, &blk) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bagman/bag.rb', line 71 def uncast_bag_field(name, type, , &blk) name = name.to_s if default = [:default] default_generator = "__#{name}_default" target_class.send :define_method, default_generator do default.dup end end target_class.send :define_method, name do self.bag[name] || (default_generator && send(default_generator)) end target_class.send :define_method, "#{name}=" do |value| self.bag[name] = value end end |
#validate ⇒ Object
180 181 |
# File 'lib/bagman/bag.rb', line 180 def validate(*) end |
#validates_presence_of ⇒ Object
Validations
174 175 |
# File 'lib/bagman/bag.rb', line 174 def validates_presence_of(*) end |
#validates_storage_type_of ⇒ Object
177 178 |
# File 'lib/bagman/bag.rb', line 177 def validates_storage_type_of(*) end |