Module: Datoki::Def_Field

Defined in:
lib/datoki.rb

Overview

class self ===

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



51
52
53
# File 'lib/datoki.rb', line 51

def fields
  @fields
end

#fields_as_requiredObject (readonly)

Returns the value of attribute fields_as_required.



51
52
53
# File 'lib/datoki.rb', line 51

def fields_as_required
  @fields_as_required
end

#onsObject (readonly)

Returns the value of attribute ons.



51
52
53
# File 'lib/datoki.rb', line 51

def ons
  @ons
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



51
52
53
# File 'lib/datoki.rb', line 51

def table_name
  @table_name
end

Instance Method Details

#allow(sym) ⇒ Object



136
137
138
# File 'lib/datoki.rb', line 136

def allow sym
  fields[@current_field][:allow][sym] = true;
end

#create(raw) ⇒ Object



492
493
494
495
# File 'lib/datoki.rb', line 492

def create raw
  raw[:create] = self.to_s
  new raw
end

#disable(*props) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/datoki.rb', line 446

def disable *props
  props.each { |prop|
    case prop
    when :min, :max
      field.delete prop
    when :strip, :null
      field[:allow][prop] = false
    else
      field[:cleaners][prop] = false
    end
  }
end

#enable(*props) ⇒ Object



435
436
437
438
439
440
441
442
443
444
# File 'lib/datoki.rb', line 435

def enable *props
  props.each { |prop|
    case prop
    when :strip, :null
      field[:allow][prop] = true
    else
      field[:cleaners][prop] = true
    end
  }
end

#equal_to(*args) ⇒ Object



464
465
466
467
# File 'lib/datoki.rb', line 464

def equal_to *args
  field[:cleaners][:equal_to] ||= []
  field[:cleaners][:equal_to].concat args
end

#field(*args) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/datoki.rb', line 164

def field *args
  # === Setup a default table if none specified:
  if !@table_name && Datoki.db
    t_name = self.to_s.downcase.to_sym
    table(t_name) if Datoki.db.tables.include?(t_name)
  end

  return fields[@current_field] if args.empty?
  return fields[args.first] unless block_given?

  name = args.first

  fail "#{name.inspect} already defined." if fields[name]
  fields_as_required[:"#{name}!"] = name

  fields[name] = {
    :name         => name,
    :type         => :unknown,
    :english_name => name.to_s.freeze,
    :allow        => {:null => false},
    :disable      => {},
    :cleaners     => {},
    :on           => {}
  }

  @current_field = name

  if field? :chars
    field[:allow][:strip] = true
  end

  if schema[name]
    if schema[name].has_key? :max_length
      fields[name][:max] = schema[name][:max_length]
    end
  end

  yield

  fail("Type not specified for #{name.inspect}") if field[:type] == :unknown

  # === check :allow_null and :min are not both set.
  if field?(:chars) && field[:allow][:null] && field.has_key?(:min) && field[:min] < 1
    fail "#{field[:type].inspect} can't be both: allow :null && :min = #{field[:min]}"
  end

  # === Ensure schema matches with field definition:
  schema_match

  field[:html_escape] = case
                        when field[:html_escape]
                          field[:html_escape]
                        when field?(:numeric)
                          :number
                        when field?(:chars)
                          :string
                        else
                          fail "Unknown html_escape for: #{field[:name].inspect}"
                        end

  @current_field = nil
end

#field?(*args) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/datoki.rb', line 160

def field? *args
  inspect_field?(:type, field[:name], *args)
end

#field_on(action, meth_name_sym) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/datoki.rb', line 305

def field_on action, meth_name_sym
  fail "Invalid action: #{action.inspect}" unless Actions.include? action
  if field
    field[:on][action] ||= {}
    field[:on][action][meth_name_sym] = true
  else
    @ons[action] ||= {}
    @ons[action][meth_name_sym] = true
  end
  self
end

#href(*args) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
# File 'lib/datoki.rb', line 347

def href *args
  field[:html_escape] = :href
  case args.map(&:class)
  when []
    varchar 1, 255
  when [NilClass]
    varchar nil, 1, (schema[field[:name]] ? schema[field[:name]][:max_length] : 255)
  else
    varchar *args
  end
end

#html_escapeObject



93
94
95
96
97
98
99
100
# File 'lib/datoki.rb', line 93

def html_escape
  @html_escape ||= begin
                     fields.inject({}) { |memo, (name, meta)|
                       memo[name] = meta[:html_escape]
                       memo
                     }
                   end
end

#included_in(arr) ⇒ Object



469
470
471
472
# File 'lib/datoki.rb', line 469

def included_in arr
  field[:cleaners][:included_in] ||= []
  field[:cleaners][:included_in].concat arr
end

#initialize_def_fieldObject



53
54
55
56
57
58
59
60
61
# File 'lib/datoki.rb', line 53

def initialize_def_field
  @ons                = {}
  @fields             = {} # Ex: {:name=>{}, :age=>{}}
  @fields_as_required = {} # Ex: {:name!=>:name}
  @current_field      = nil
  @schema             = {}
  @schema_match       = false
  @table_name         = nil
end

#inspect_field?(target, name, *args) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/datoki.rb', line 119

def inspect_field? target, name, *args
  case target
  when :type
    meta = fields[name]
    fail "Unknown field: #{name.inspect}" unless meta
    return true if args.include?(meta[:type])
    return true if args.include?(:chars) && Char_Types.include?(meta[:type])
    args.include?(:numeric) && Numeric_Types.include?(meta[:type])
  else
    fail "Unknown arg: #{target.inspect}"
  end
end

#matches(v = :blok) ⇒ Object



487
488
489
490
# File 'lib/datoki.rb', line 487

def matches v = :blok
  field[:cleaners][:match] ||= []
  field[:cleaners][:match] << (v == :blok ? Proc.new : v)
end

#primary_keyObject



330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/datoki.rb', line 330

def primary_key
  field[:primary_key] = true
  if field?(:unknown)
    if schema[field[:name]]
      type schema[field[:name]][:type]
    else
      type :integer
    end
  end

  true
end

#pseudoObject



132
133
134
# File 'lib/datoki.rb', line 132

def pseudo
  fields[@current_field][:pseudo] = true
end

#returning_fieldsObject

This method removes keys that are meant to be secret: e.g. encrypted passwords. This decreases the chance they end up in logs.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/datoki.rb', line 145

def returning_fields
  return [] unless table_name
  s = Datoki.db.schema(table_name)
  return [] unless s
  s.map { |pair|
    name, meta = pair
    field = fields[name]
    if !field || !field[:secret]
      name
    else
      nil
    end
  }.compact
end

#schema(*args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/datoki.rb', line 102

def schema *args
  case args.size

  when 0
    @schema 

  when 1
    result = @schema[args.first]
    fail "Unknown field: #{args.first.inspect}" unless result
    result

  else
    fail "Unknown args: #{args.inspect}"

  end
end

#schema_match(target = :current) ⇒ Object

def field



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/datoki.rb', line 227

def schema_match target = :current
  return true if !@table_name
  return true if schema_match?

  if target == :all # === do a schema match on entire table
    schema.each { |name, db_schema|
      orig_field = @current_field
      @current_field = name
      schema_match
      @current_field = orig_field
    }

    @schema_match = true
    return true
  end # === if target

  name      = @current_field
  db_schema = schema[@current_field]

  return true if db_schema && !field
  return true if field[:schema_has_been_matched]
  return true if field[:pseudo]

  if db_schema[:allow_null] != field[:allow][:null]
    fail Schema_Conflict, "#{name}: :allow_null: #{db_schema[:allow_null].inspect} != #{field[:allow][:null].inspect}"
  end

  if field?(:chars)
    if !field[:min].is_a?(Numeric) || field[:min] < 0
      fail ":min not properly defined for #{name.inspect}: #{field[:min].inspect}"
    end

    if !field[:max].is_a?(Numeric)
      fail ":max not properly defined for #{name.inspect}: #{field[:max].inspect}"
    end
  end

  if db_schema.has_key?(:max_length)
    if field[:max] != db_schema[:max_length]
      fail Schema_Conflict, "#{name}: :max: #{db_schema[:max_length].inspect} != #{field[:max].inspect}"
    end
  end

  if !!db_schema[:primary_key] != !!field[:primary_key]
    fail Schema_Conflict, "#{name}: :primary_key: #{db_schema[:primary_key].inspect} != #{field[:primary_key].inspect}"
  end

  # === match :type
  if field[:type] != :string_ish
    db_type = Datoki.db_type_to_ruby db_schema[:db_type], db_schema[:type]
    type    = field[:type]
    if db_type != type
      fail Schema_Conflict, "#{name}: :type: #{db_type.inspect} != #{type.inspect}"
    end
  end

  # === match :max_length
  db_max = db_schema[:max_length]
  max    = field[:max]
  if !db_max.nil? && db_max != max
    fail Schema_Conflict, "#{name}: :max_length: #{db_max.inspect} != #{max.inspect}"
  end

  # === match :min_length
  db_min = db_schema[:min_length]
  min    = field[:min]
  if !db_min.nil? && db_min != min
    fail Schema_Conflict, "#{name}: :min_length: #{db_min.inspect} != #{min.inspect}"
  end

  # === match :allow_null
  if db_schema[:allow_null] != field[:allow][:null]
    fail Schema_Conflict, "#{name}: :allow_null: #{db_schema[:allow_null].inspect} != #{field[:allow][:null].inspect}"
  end

  field[:schema_has_been_matched] = true
end

#schema_match?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/datoki.rb', line 63

def schema_match?
  @schema_match
end

#secretObject



326
327
328
# File 'lib/datoki.rb', line 326

def secret
  field[:secret] = true
end

#set_to(v = :blok) ⇒ Object



459
460
461
462
# File 'lib/datoki.rb', line 459

def set_to v = :blok
  field[:cleaners][:set_to] ||= []
  field[:cleaners][:set_to] << (v == :blok ? Proc.new : v)
end

#table(name) ⇒ Object



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
# File 'lib/datoki.rb', line 67

def table name
  fail ArgumentError, "Table name must be a Symbol: #{name.inspect}" unless name.is_a?(Symbol)
  if !@schema.empty? || @table_name
    fail "Schema/table already defined: #{@table_name.inspect}"
  end

  db_schema = Datoki.db.schema(name)

  if !db_schema
    fail ArgumentError, "Schema not found for: #{name.inspect}"
  end

  @table_name = name
  self.const_set(:TABLE, DB[@table_name])

  db_schema.each { |pair|
    @schema[pair.first] = pair.last
  }

  if @schema.empty?
    @schema_match = true
  end

  schema
end

#text(*args) ⇒ Object



343
344
345
# File 'lib/datoki.rb', line 343

def text *args
  type :text, *args
end

#type(name, *args) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/datoki.rb', line 367

def type name, *args
  field[:type] = name

  if field? :chars

    enable :strip

    if field?(:text)
      field[:max] ||= 4000
    else
      field[:max] ||= 255
    end

    if schema[name] && !schema[name][:allow_null]
      field[:min] = 1
    end

  end # === if field? :chars

  case args.map(&:class)

  when []
    # do nothing

  when [Array]
    field[:options] = args.first
    enable(:null) if field[:options].include? nil
    disable :min, :max

  when [NilClass]
    if field?(:chars)
      fail "A :min and :max is required for String fields."
    end

    enable :null

  when [NilClass, Fixnum, Fixnum]
    field[:allow][:null] = true
    field[:min] = args[-2]
    field[:max] = args.last

  when [Fixnum, Fixnum]
    field[:min], field[:max] = args

  when [Proc], [Regexp]
    matches *args

  when [Fixnum, Fixnum, Proc], [Fixnum, Fixnum, Regexp]
    field[:min] = args.shift
    field[:max] = args.shift
    matches *args

  else
    fail "Unknown args: #{args.inspect}"

  end # === case

end

#unique_index(name, msg = nil) ⇒ Object



317
318
319
320
321
322
323
324
# File 'lib/datoki.rb', line 317

def unique_index name, msg = nil
  field[:unique_index] = name
  if msg
    field[:error_msgs] ||= {}
    field[:error_msgs][:unique] = msg
  end
  self
end

#update(raw) ⇒ Object



497
498
499
500
# File 'lib/datoki.rb', line 497

def update raw
  raw[:update] = self.to_s
  new raw
end