class HField

FIELDS_TO_PGTYPES = {

fields.boolean: 'bool',
fields.integer: 'int4',
fields.text: 'text',
fields.html: 'text',
fields.date: 'date',
fields.datetime: 'timestamp',
fields.binary: 'bytea',
fields.many2one: 'int4',
fields.serialized: 'text',

}

Public Class Methods

autoComplete(modelName, fieldName, args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 50
def self.autoComplete(modelName, fieldName, args = {})
  
  return args if args[:type] != "automatic"
  
  if fieldName =~ /^(\w+)_id$/
    return self.manyToOne($1, args)
  elsif fieldName =~ /^(\w+)_table$/
    return self.oneToMany($1, "#{modelName}_id", args)
  elsif fieldName =~ /^(\w+)_join$/
    return self.manyToMany($1, "#{modelName}_#{fieldName}", "#{modelName}_id", "#{$1}_id", args)
  elsif fieldName =~ /^(\w+)_virtual$/
    return self.virtual($1, nil, args)
  end

  return HField.new(args)

end
automatic(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 68
def self.automatic(args = {})
  args[:type] = "automatic"
  return HField.new(args)
end
binary(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 112
def self.binary(args = {})
  args[:type] = "binary"
  args[:default] = '' unless args[:default]
  return HField.new(args)
end
boolean(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 123
def self.boolean(args = {})
  args[:type] = "boolean"
  args[:default] = false unless args[:default]
  return HField.new(args)
end
char(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 77
def self.char(args = {})
  args[:type] = "char"
  args[:default] = '' unless args[:default]
  return HField.new(args)
end
date(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 101
def self.date(args = {})
  args[:type] = "date"
  args[:default] = '' unless args[:default]
  return HField.new(args)
end
dateTime(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 107
def self.dateTime(args = {})
  args[:type] = "datetime"
  return HField.new(args)
end
float(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 95
def self.float(args = {})
  args[:type] = "float"
  args[:default] = 0 unless args[:default]
  return HField.new(args)
end
integer(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 89
def self.integer(args = {})
  args[:type] = "integer"
  args[:default] = 0 unless args[:default]
  return HField.new(args)
end
magic(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 73
def self.magic(args = {})
  return self.automatic(args)
end
manyToMany(modelName, joinTable, referenceFieldName, referenceFieldName2, args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 154
def self.manyToMany(modelName, joinTable, referenceFieldName, referenceFieldName2, args = {})
  args[:modelNameReference] = modelName
  args[:joinTable] = joinTable
  args[:referenceFieldName] = referenceFieldName
  args[:referenceFieldName2] = referenceFieldName2
  #args[:constraint] = 'NOT NULL'
  args[:type] = "manyToMany"
  return HField.new(args)
end
manyToOne(modelName, args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 145
def self.manyToOne(modelName, args = {})
  args[:modelNameReference] = modelName
  args[:onDelete] = 'CASCADE'
  args[:onUpdate] = 'CASCADE'
  #args[:constraint] = 'NOT NULL'
  args[:type] = "manyToOne"
  return HField.new(args)
end
new(args) click to toggle source
# File lib/hdb/hfield.rb, line 19
def initialize(args)
  args.each { |key, value| self[key] = value }
end
oneToMany(modelName, referenceFieldName, args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 138
def self.oneToMany(modelName, referenceFieldName, args = {})
  args[:modelNameReference] = modelName
  args[:referenceFieldName] = referenceFieldName
  args[:type] = "oneToMany"
  return HField.new(args)
end
oneToOne(modelName, args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 129
def self.oneToOne(modelName, args = {})
  args[:modelNameReference] = modelName
  #args[:constraint] = 'NOT NULL'
  args[:onDelete] = 'CASCADE' # CASCADE or RESTRICT
  args[:onUpdate] = 'CASCADE'
  args[:type] = "oneToOne"
  return HField.new(args)
end
primaryKey(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 172
def self.primaryKey(args = {})
  args[:type] = 'integer'
  args[:system] = true
  args[:constraint] = 'SERIAL PRIMARY KEY'
  return HField.new(args)
end
selection(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 118
def self.selection(args = {})
  args[:type] = "selection"
  return HField.new(args)
end
text(args = {}) click to toggle source
# File lib/hdb/hfield.rb, line 83
def self.text(args = {})
  args[:type] = "text"
  args[:default] = '' unless args[:default]
  return HField.new(args)
end
virtual(functionName = nil, object = nil, args = {}, &block) click to toggle source
# File lib/hdb/hfield.rb, line 164
def self.virtual(functionName = nil, object = nil, args = {}, &block)
  args[:object] = object
  args[:functionName] = functionName
  args[:type] = "virtual"
  args[:block] = block
  return HField.new(args)
end

Public Instance Methods

defaultValue() click to toggle source
# File lib/hdb/hfield.rb, line 23
def defaultValue()
  
  case self[:type]
  when "char"
    return 'true'
  when "text"
    return ''
  when "integer"
    return 0
  when "float"
    return 0
  when "date"
    return '' 
  when "dateTime"
    return ''
  when "binary"
    return ''
  when "selection"
    return ''
  when "boolean"
    return 'true'
  else
    return nil
  end

end