Class: Delphin::ProfileTableSchema

Inherits:
Array
  • Object
show all
Defined in:
lib/delphin.rb

Overview

A database schema table in a profile.

This is a list of field labels and their types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init_name) ⇒ ProfileTableSchema

Returns a new instance of ProfileTableSchema.



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

def initialize(init_name)
  super()
  @name = init_name
  @keys = Set.new
  @partials = Set.new
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



275
276
277
# File 'lib/delphin.rb', line 275

def keys
  @keys
end

#nameObject (readonly)

Returns the value of attribute name.



275
276
277
# File 'lib/delphin.rb', line 275

def name
  @name
end

#partialsObject (readonly)

Returns the value of attribute partials.



275
276
277
# File 'lib/delphin.rb', line 275

def partials
  @partials
end

Instance Method Details

#add_field(label, type, key = false, partial = false) ⇒ Object

Add a new field and type.



315
316
317
318
319
# File 'lib/delphin.rb', line 315

def add_field(label, type, key = false, partial = false)
  self.push(Struct.new(:label, :type).new(label, type))
  @keys.add(label) if key
  @partials.add(label) if partial
end

#is_key?(label) ⇒ Boolean

Is the specified label a key?

Returns:

  • (Boolean)


322
323
324
# File 'lib/delphin.rb', line 322

def is_key?(label)
  @keys.member?(label)
end

#is_partial?(label) ⇒ Boolean

Is the specified label a partial?

Returns:

  • (Boolean)


327
328
329
# File 'lib/delphin.rb', line 327

def is_partial?(label)
  @partials.member?(label)
end

#record(text) ⇒ Object

Generate a data record from a line of text.

A data record is a hash of field labels to values.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/delphin.rb', line 298

def record(text)
  data_fields = text.split(/@/)
  field_names = collect {|f| f.label}
  field_types = collect {|f| f.type}
  # Do type conversion if the field is of type integer.
  data_fields = field_types.zip(data_fields).collect do |type, data|
    case type
    when :integer
      data.to_i
    else
      data
    end
  end
  Hash[*field_names.zip(data_fields).flatten]
end

#to_sObject

The string representation is identical to what appears in the relations file.



286
287
288
289
290
291
292
293
# File 'lib/delphin.rb', line 286

def to_s
  "#{name}:\n" + collect do |field|
    s = "  #{field.label} :#{field.type}"
    s += " :key" if is_key?(field.label)
    s += " :partial" if is_partial?(field.label)
    s
  end.join("\n")
end