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

#on_docObject (readonly)

Returns the value of attribute on_doc.



276
277
278
# File 'lib/datoki.rb', line 276

def on_doc
  @on_doc
end

#onsObject (readonly)

Returns the value of attribute ons.



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

def ons
  @ons
end

Instance Method Details

#allow(sym) ⇒ Object



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

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

#create(raw) ⇒ Object



448
449
450
451
# File 'lib/datoki.rb', line 448

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

#disable(*props) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/datoki.rb', line 394

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

def



383
384
385
386
387
388
389
390
391
392
# File 'lib/datoki.rb', line 383

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



412
413
414
415
# File 'lib/datoki.rb', line 412

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

#field(*args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
# File 'lib/datoki.rb', line 141

def field *args
  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)


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

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

#field_on(action, meth_name_sym) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/datoki.rb', line 283

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



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/datoki.rb', line 312

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



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

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

#included_in(arr) ⇒ Object



417
418
419
420
# File 'lib/datoki.rb', line 417

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
62
63
64
# File 'lib/datoki.rb', line 53

def initialize_def_field
  @on_doc             = []
  @ons                = {}
  @fields             = {} # Ex: {:name=>{}, :age=>{}}
  @fields_as_required = {} # Ex: {:name!=>:name}
  @current_field      = nil
  @schema             = {}
  @schema_match       = false
  @table_name         = nil
  name = self.to_s.downcase.to_sym
  table(name) if Datoki.db.tables.include?(name)
end

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

Returns:

  • (Boolean)


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

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

#match(*args) ⇒ Object



435
436
437
438
439
# File 'lib/datoki.rb', line 435

def match *args
  fail "Not allowed for #{field[:type].inspect}" unless field?(:chars)
  field[:cleaners][:match] ||= []
  field[:cleaners][:match] << args
end

#not_match(*args) ⇒ Object



441
442
443
444
445
446
# File 'lib/datoki.rb', line 441

def not_match *args
  fail "Not allowed for #{field[:type].inspect}" unless field?(:chars)
  field[:cleaners][:not_match] ||= []
  field[:cleaners][:not_match] << args
  self
end

#on(*args) ⇒ Object



277
278
279
280
281
# File 'lib/datoki.rb', line 277

def on *args
  return(field_on *args) if !block_given?
  @on_doc << [args, Proc.new]
  self
end

#primary_keyObject



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/datoki.rb', line 295

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

#schema(*args) ⇒ Object



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

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



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

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]

  if db_schema && !field && db_schema[:type] != :datetime
    fail Schema_Conflict, "#{name}: #{name.inspect} has not been defined."
  end

  return true if field[:schema_match]

  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
  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

  # === 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_match] = true
end

#schema_match?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/datoki.rb', line 66

def schema_match?
  @schema_match
end

#set_to(*args) ⇒ Object



407
408
409
410
# File 'lib/datoki.rb', line 407

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

#table(name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datoki.rb', line 70

def table name
  if !@schema.empty? || @table_name
    fail "Schema/table already defined: #{@table_name.inspect}"
  end

  db_schema = Datoki.db.schema(name)

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

  @table_name = name

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

  if @schema.empty?
    @schema_match = true
  end

  schema
end

#text(*args) ⇒ Object



308
309
310
# File 'lib/datoki.rb', line 308

def text *args
  type :text, *args
end

#type(name, *args) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/datoki.rb', line 332

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

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

  end # === case

end