Class: Bagman::Bag

Inherits:
Object
  • Object
show all
Defined in:
lib/bagman/bag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#columnsObject (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_classObject (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_mixinObject (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, options)
  if s = options[: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, options, &blk)
  @columns << (column = SimpleColumn.new(name, :bag, type, options))

  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 = options[: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,options={},&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, options, &blk)
  name = name.to_s
  @columns << (column = SimpleColumn.new(name, :crypto, type, options))

  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 options[: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, options)

    if pepper = options[: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)
  options = args.extract_options!
  type    = args.shift || :string

  if options[:unbagged]
    # no-op
    # unbagged_field(name, type, options, &blk)
  elsif options[:encrypted]
    crypto_field(name, type, options, &blk)
  else
    bag_field(name, type, options, &blk)
  end
end

#indexObject

Indices



139
140
# File 'lib/bagman/bag.rb', line 139

def index(*)
end

#materialiseObject

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, options, &blk)
end

#validateObject



153
154
# File 'lib/bagman/bag.rb', line 153

def validate(*)
end

#validates_presence_ofObject

Validations



147
148
# File 'lib/bagman/bag.rb', line 147

def validates_presence_of(*)
end

#validates_storage_type_ofObject



150
151
# File 'lib/bagman/bag.rb', line 150

def validates_storage_type_of(*)
end