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



128
129
130
131
132
133
134
135
136
# File 'lib/bagman/bag.rb', line 128

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



40
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
# File 'lib/bagman/bag.rb', line 40

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bagman/bag.rb', line 139

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



82
83
84
85
86
87
88
89
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
# File 'lib/bagman/bag.rb', line 82

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
32
33
34
35
36
37
# 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)

  elsif options[:uncast]
    uncast_bag_field(name, type, options, &blk)

  else
    bag_field(name, type, options, &blk)

  end
end

#indexObject

Indices



158
159
# File 'lib/bagman/bag.rb', line 158

def index(*)
end

#materialiseObject

Materialise



162
163
# File 'lib/bagman/bag.rb', line 162

def materialise(*)
end

#unbagged_field(name, type, options, &blk) ⇒ Object



123
124
# File 'lib/bagman/bag.rb', line 123

def unbagged_field(name, type, options, &blk)
end

#uncast_bag_field(name, type, options, &blk) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bagman/bag.rb', line 69

def uncast_bag_field(name, type, options, &blk)
  name = name.to_s

  target_class.send :define_method, name do
    self.bag[name]
  end

  target_class.send :define_method, "#{name}=" do |value|
    self.bag[name] = value
  end
end

#validateObject



172
173
# File 'lib/bagman/bag.rb', line 172

def validate(*)
end

#validates_presence_ofObject

Validations



166
167
# File 'lib/bagman/bag.rb', line 166

def validates_presence_of(*)
end

#validates_storage_type_ofObject



169
170
# File 'lib/bagman/bag.rb', line 169

def validates_storage_type_of(*)
end