Class: TreasureData::Schema
- Inherits:
-
Object
- Object
- TreasureData::Schema
- Defined in:
- lib/td/client/model.rb
Defined Under Namespace
Classes: Field
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
Instance Method Summary collapse
- #add_field(name, type, sql_alias = nil) ⇒ Array
- #from_json(obj) ⇒ self
-
#initialize(fields = []) ⇒ Schema
constructor
A new instance of Schema.
- #merge(schema) ⇒ Schema
- #to_json(*args) ⇒ Array<Field>
Constructor Details
#initialize(fields = []) ⇒ Schema
Returns a new instance of Schema.
318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/td/client/model.rb', line 318 def initialize(fields=[]) @fields = fields @names = {} @fields.each do |f| raise ArgumentError, "Column name '#{f.name}' is duplicated." if @names.key?(f.name) @names[f.name] = true if f.sql_alias && f.name != f.sql_alias raise ArgumentError, "SQL Column alias '#{f.sql_alias}' is duplicated." if @names.key?(f.sql_alias) @names[f.sql_alias] = true end end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
332 333 334 |
# File 'lib/td/client/model.rb', line 332 def fields @fields end |
Class Method Details
.parse(columns) ⇒ Schema
306 307 308 309 310 311 312 313 314 315 |
# File 'lib/td/client/model.rb', line 306 def self.parse(columns) schema = Schema.new columns.each {|column| unless /\A(?<name>.*)(?::(?<type>[^:@]+))(?:@(?<sql_alias>[^:@]+))?\z/ =~ column raise ParameterValidationError, "type must be specified" end schema.add_field(name, type, sql_alias) } schema end |
Instance Method Details
#add_field(name, type, sql_alias = nil) ⇒ Array
337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/td/client/model.rb', line 337 def add_field(name, type, sql_alias=nil) if @names.key?(name) raise ParameterValidationError, "Column name '#{name}' is duplicated." end @names[name] = true if sql_alias && sql_alias != name && @names.key?(sql_alias) raise ParameterValidationError, "SQL Column alias '#{sql_alias}' is duplicated." end @names[sql_alias] = true @fields << Field.new(name, type, sql_alias) end |
#from_json(obj) ⇒ self
370 371 372 373 374 375 |
# File 'lib/td/client/model.rb', line 370 def from_json(obj) @fields = obj.map {|f| Field.new(*f) } self end |
#merge(schema) ⇒ Schema
351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/td/client/model.rb', line 351 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>
364 365 366 |
# File 'lib/td/client/model.rb', line 364 def to_json(*args) @fields.map {|f| f.sql_alias ? [f.name, f.type, f.sql_alias] : [f.name, f.type] }.to_json(*args) end |