Class: TreasureData::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/td/client/model.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = []) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • fields (Array) (defaults to: [])


300
301
302
# File 'lib/td/client/model.rb', line 300

def initialize(fields=[])
  @fields = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



305
306
307
# File 'lib/td/client/model.rb', line 305

def fields
  @fields
end

Class Method Details

.parse(cols) ⇒ Schema

Parameters:

  • cols (String)

Returns:



291
292
293
294
295
296
297
# File 'lib/td/client/model.rb', line 291

def self.parse(cols)
  fields = cols.split(',').map {|col|
    name, type, *_ = col.split(':')
    Field.new(name, type)
  }
  Schema.new(fields)
end

Instance Method Details

#add_field(name, type) ⇒ Array

Parameters:

  • name (String)
  • type (String)

Returns:

  • (Array)


310
311
312
# File 'lib/td/client/model.rb', line 310

def add_field(name, type)
  @fields << Field.new(name, type)
end

#from_json(obj) ⇒ self

Parameters:

  • obj (Object)

Returns:

  • (self)


335
336
337
338
339
340
# File 'lib/td/client/model.rb', line 335

def from_json(obj)
  @fields = obj.map {|f|
    Field.new(f[0], f[1])
  }
  self
end

#merge(schema) ⇒ Schema

Parameters:

Returns:



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/td/client/model.rb', line 316

def merge(schema)
  nf = @fields.dup
  schema.fields.each {|f|
    if i = nf.find_index {|sf| sf.name == f.name }
      nf[i] = f
    else
      nf << f
    end
  }
  Schema.new(nf)
end

#to_json(*args) ⇒ Array<Field>

Returns:



329
330
331
# File 'lib/td/client/model.rb', line 329

def to_json(*args)
  @fields.map {|f| [f.name, f.type] }.to_json(*args)
end