Class: Gcloud::Bigquery::Schema::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/bigquery/schema.rb

Constant Summary collapse

MODES =
%w( NULLABLE REQUIRED REPEATED )
TYPES =
%w( STRING INTEGER FLOAT BOOLEAN TIMESTAMP RECORD )

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, description: nil, mode: :nullable, fields: nil) ⇒ Field

Returns a new instance of Field.



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/gcloud/bigquery/schema.rb', line 248

def initialize name, type, description: nil,
               mode: :nullable, fields: nil
  @gapi = Google::Apis::BigqueryV2::TableFieldSchema.new
  @gapi.update! name: name
  @gapi.update! type: verify_type(type)
  @gapi.update! description: description if description
  @gapi.update! mode: verify_mode(mode) if mode
  if fields
    @fields = fields
    check_for_changed_fields!
  end
  @original_json = @gapi.to_json
end

Class Method Details

.from_gapi(gapi) ⇒ Object



318
319
320
321
322
323
# File 'lib/gcloud/bigquery/schema.rb', line 318

def self.from_gapi gapi
  new("to-be-replaced", "STRING").tap do |f|
    f.instance_variable_set :@gapi, gapi
    f.instance_variable_set :@original_json, gapi.to_json
  end
end

Instance Method Details

#==(other) ⇒ Object



333
334
335
336
# File 'lib/gcloud/bigquery/schema.rb', line 333

def == other
  return false unless other.is_a? Field
  to_gapi.to_h == other.to_gapi.to_h
end

#changed?Boolean

Returns:

  • (Boolean)


313
314
315
# File 'lib/gcloud/bigquery/schema.rb', line 313

def changed?
  @original_json == to_gapi.to_json
end

#check_for_changed_fields!Object



304
305
306
307
308
309
310
# File 'lib/gcloud/bigquery/schema.rb', line 304

def check_for_changed_fields!
  return if frozen?
  fields.each(&:check_for_changed_fields!)
  gapi_fields = Array(fields).map(&:to_gapi)
  gapi_fields = nil if gapi_fields.empty?
  @gapi.update! fields: gapi_fields
end

#descriptionObject



278
279
280
# File 'lib/gcloud/bigquery/schema.rb', line 278

def description
  @gapi.description
end

#description=(new_description) ⇒ Object



282
283
284
# File 'lib/gcloud/bigquery/schema.rb', line 282

def description= new_description
  @gapi.update! description: new_description
end

#fieldsObject



294
295
296
# File 'lib/gcloud/bigquery/schema.rb', line 294

def fields
  @fields ||= Array(@gapi.fields).map { |f| Field.from_gapi f }
end

#fields=(new_fields) ⇒ Object



298
299
300
# File 'lib/gcloud/bigquery/schema.rb', line 298

def fields= new_fields
  @fields = new_fields
end

#modeObject



286
287
288
# File 'lib/gcloud/bigquery/schema.rb', line 286

def mode
  @gapi.mode
end

#mode=(new_mode) ⇒ Object



290
291
292
# File 'lib/gcloud/bigquery/schema.rb', line 290

def mode= new_mode
  @gapi.update! mode: verify_mode(new_mode)
end

#nameObject



262
263
264
# File 'lib/gcloud/bigquery/schema.rb', line 262

def name
  @gapi.name
end

#name=(new_name) ⇒ Object



266
267
268
# File 'lib/gcloud/bigquery/schema.rb', line 266

def name= new_name
  @gapi.update! name: new_name
end

#to_gapiObject



326
327
328
329
330
# File 'lib/gcloud/bigquery/schema.rb', line 326

def to_gapi
  # make sure any changes are saved.
  check_for_changed_fields!
  @gapi
end

#typeObject



270
271
272
# File 'lib/gcloud/bigquery/schema.rb', line 270

def type
  @gapi.type
end

#type=(new_type) ⇒ Object



274
275
276
# File 'lib/gcloud/bigquery/schema.rb', line 274

def type= new_type
  @gapi.update! type: verify_type(new_type)
end