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.



220
221
222
# File 'lib/td/client/model.rb', line 220

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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



224
225
226
# File 'lib/td/client/model.rb', line 224

def fields
  @fields
end

Class Method Details

.parse(cols) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/td/client/model.rb', line 212

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) ⇒ Object



226
227
228
# File 'lib/td/client/model.rb', line 226

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

#from_json(obj) ⇒ Object



246
247
248
249
250
251
# File 'lib/td/client/model.rb', line 246

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

#merge(schema) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/td/client/model.rb', line 230

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) ⇒ Object



242
243
244
# File 'lib/td/client/model.rb', line 242

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