Class: INat::Data::Model
- Inherits:
-
Object
- Object
- INat::Data::Model
- Includes:
- App::Logger::DSL
- Defined in:
- lib/inat/data/model.rb
Direct Known Subclasses
Class Method Summary collapse
- .api_limit(limit = nil) ⇒ Object
- .api_part(part = nil) ⇒ Object
- .api_path(name = nil) ⇒ Object
-
.backs(name, item_type: nil, ids_field: nil, owned: true, back_field: nil)
Defines a new one-to-many field.
-
.block(name, type: nil, &block)
Defines a new write-only field.
- .DDL ⇒ Object
-
.field(name, type: nil, id_field: nil, required: false, index: false, unique: false, primary_key: false)
Defines a new field.
- .fields(include_super = true) ⇒ Object
- .has_path? ⇒ Boolean
- .has_table? ⇒ Boolean
-
.links(name, item_type: nil, ids_field: nil, owned: true, table_name: nil, back_field: nil, link_field: nil, index: false)
Defines a new many-to-many field.
- .table(name = nil) ⇒ Object
Instance Method Summary collapse
- #ignore(*names) ⇒ Object private
-
#initialize ⇒ Model
constructor
A new instance of Model.
- #post_update ⇒ Object
- #process? ⇒ Boolean
- #saved? ⇒ Boolean
- #to_h ⇒ Object
- #update(from_db: false) ⇒ Object
Methods included from App::Logger::DSL
#debug, debug, echo, #echo, error, #error, #info, info, log, #log, #warning, warning
Constructor Details
#initialize ⇒ Model
Returns a new instance of Model.
554 555 556 |
# File 'lib/inat/data/model.rb', line 554 def initialize @mutex = Mutex::new end |
Class Method Details
.api_limit(limit = nil) ⇒ Object
420 421 422 423 424 |
# File 'lib/inat/data/model.rb', line 420 def api_limit limit = nil raise TypeError, "Part name must be an Integer!", caller unless limit == nil || Integer === limit @api_limit = limit if limit != nil @api_limit end |
.api_part(part = nil) ⇒ Object
414 415 416 417 418 |
# File 'lib/inat/data/model.rb', line 414 def api_part part = nil raise TypeError, "Part name must be a Symbol!", caller unless part == nil || Symbol === part @api_part = part if part != nil @api_part end |
.api_path(name = nil) ⇒ Object
404 405 406 407 408 |
# File 'lib/inat/data/model.rb', line 404 def api_path name = nil raise TypeError, "Path name must be a Symbol!", caller unless name == nil || Symbol === name @api_path = name if name != nil @api_path end |
.backs(name, item_type: nil, ids_field: nil, owned: true, back_field: nil)
This method returns an undefined value.
Defines a new one-to-many field
504 505 506 507 508 509 510 511 512 513 |
# File 'lib/inat/data/model.rb', line 504 def backs name, item_type: nil, ids_field: nil, owned: true, back_field: nil raise TypeError, "Field name must be a Symbol!", caller unless Symbol === name raise TypeError, "Item type must be an Entity subclass!", caller unless Class === item_type && INat::Entity > item_type raise TypeError, "Argument 'ids_field' must be a Symbol!", caller unless Symbol === ids_field || ids_field == nil raise TypeError, "Argument 'back_field' must be a Symbol!", caller unless Symbol === back_field || back_field == nil raise TypeError, "Argument 'owned' must be a Boolean!", caller unless Boolean === owned @fields ||= {} @fields[name] = OneToManyField::new self, name, item_type, ids_field, owned, back_field @fields[name].implement end |
.block(name, type: nil, &block)
This method returns an undefined value.
Defines a new write-only field
523 524 525 526 527 528 529 530 |
# File 'lib/inat/data/model.rb', line 523 def block name, type: nil, &block raise TypeError, "Field name must be a Symbol!", caller unless Symbol === name raise TypeError, "Field type must be a Module!", caller unless Module === type raise ArgumentError, "Block is required!", caller unless block_given? @fields ||= {} @fields[name] = SpecialField::new self, name, type, &block @fields[name].implement end |
.DDL ⇒ Object
541 542 543 544 545 546 547 548 549 550 |
# File 'lib/inat/data/model.rb', line 541 def DDL inner = [] outer = [] fields.each do |_, field| i, o = field.DDL inner << i outer << o end "CREATE TABLE IF NOT EXISTS #{ @table } (\n#{ inner.flatten.join(",\n") }\n);\n" + "#{ outer.flatten.join("\n") }\n\n" end |
.field(name, type: nil, id_field: nil, required: false, index: false, unique: false, primary_key: false)
This method returns an undefined value.
Defines a new field
461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/inat/data/model.rb', line 461 def field name, type: nil, id_field: nil, required: false, index: false, unique: false, primary_key: false raise TypeError, "Field name must be a Symbol!", caller unless Symbol === name raise TypeError, "Field type must be a Module!", caller unless Module === type raise TypeError, "Argument 'id_field' must be a Symbol!", caller unless Symbol === id_field || id_field == nil raise TypeError, "Argument 'required' must be a Boolean!", caller unless Boolean === required raise TypeError, "Argument 'index' must be a Boolean!", caller unless Boolean === index raise TypeError, "Argument 'unique' must be a Boolean!", caller unless Boolean === unique raise TypeError, "Argument 'primary_key' must be a Boolean!", caller unless Boolean === primary_key @fields ||= {} @fields[name] = ScalarField::new self, name, type, id_field, required, index, unique, primary_key @fields[name].implement end |
.fields(include_super = true) ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/inat/data/model.rb', line 436 def fields include_super = true @fields ||= {} result = {} if include_super ancestors.reverse.each do |ancestor| if ancestor != self && ancestor.respond_to?(:fields) ancestor_fields = ancestor.fields if Hash === ancestor_fields result.merge! ancestor_fields end end end end result.merge! @fields result.freeze end |
.has_path? ⇒ Boolean
410 411 412 |
# File 'lib/inat/data/model.rb', line 410 def has_path? !!@api_path end |
.has_table? ⇒ Boolean
432 433 434 |
# File 'lib/inat/data/model.rb', line 432 def has_table? !!@table end |
.links(name, item_type: nil, ids_field: nil, owned: true, table_name: nil, back_field: nil, link_field: nil, index: false)
This method returns an undefined value.
Defines a new many-to-many field
482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/inat/data/model.rb', line 482 def links name, item_type: nil, ids_field: nil, owned: true, table_name: nil, back_field: nil, link_field: nil, index: false raise TypeError, "Field name must be a Symbol!", caller unless Symbol === name raise TypeError, "Item type must be an Entity subclass!", caller unless Class === item_type && INat::Entity > item_type raise TypeError, "Argument 'ids_field' must be a Symbol!", caller unless Symbol === ids_field || ids_field == nil raise TypeError, "Argument 'table_name' must be a Symbol!", caller unless Symbol === table_name || table_name == nil raise TypeError, "Argument 'back_field' must be a Symbol!", caller unless Symbol === back_field || back_field == nil raise TypeError, "Argument 'link_field' must be a Symbol!", caller unless Symbol === link_field || link_field == nil raise TypeError, "Argument 'owned' must be a Boolean!", caller unless Boolean === owned raise TypeError, "Argument 'index' must be a Boolean!", caller unless Boolean === index @fields ||= {} @fields[name] = ManyToManyField::new self, name, item_type, ids_field, owned, table_name, back_field, link_field, index @fields[name].implement end |
.table(name = nil) ⇒ Object
426 427 428 429 430 |
# File 'lib/inat/data/model.rb', line 426 def table name = nil raise TypeError, "Table name must be a Symbol!", caller unless name == nil || Symbol === name @table = name if name != nil @table end |
Instance Method Details
#ignore(*names) ⇒ Object (private)
532 533 534 535 536 537 538 539 |
# File 'lib/inat/data/model.rb', line 532 private def ignore *names @fields ||= {} names.each do |name| raise TypeError, "Field name must be a Symbol!", caller unless Symbol === name @fields[name] = IgnoreField::new self, name @fields[name].implement end end |
#post_update ⇒ Object
566 567 568 |
# File 'lib/inat/data/model.rb', line 566 def post_update # do nothing end |
#to_h ⇒ Object
590 591 592 593 594 595 596 597 598 |
# File 'lib/inat/data/model.rb', line 590 def to_h result = {} self.class.fields.each do |key, field| if field.read? result[key] = send "#{ key }" end end result.freeze end |
#update(from_db: false) ⇒ Object
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/inat/data/model.rb', line 570 def update(from_db: false) raise ArgumentError, "Block is required!", caller unless block_given? @process = true @saved = true if from_db result = nil exception = nil @mutex.synchronize do begin result = yield post_update unless from_db rescue Exception => e exception = e end end @saved = true if from_db @process = false raise exception.class, exception., caller, cause: exception if exception result end |