Class: MendixBridge::DSL::EntityBuilder
- Inherits:
-
Object
- Object
- MendixBridge::DSL::EntityBuilder
- Defined in:
- lib/mendix_bridge/dsl.rb
Constant Summary collapse
- CARDINALITIES =
%i[one many].freeze
- ATTRIBUTE_TYPES =
%i[ autonumber binary boolean datetime decimal enumeration hash_string integer long string ].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #access(role, create: false, delete: false, read: [], write: [], where: nil) ⇒ Object
- #association(name, target:, cardinality: :one, owner: :default) ⇒ Object
- #attribute(name, type, required: false, default: nil, length: 200, enumeration: nil) ⇒ Object
-
#initialize(name, persistable) ⇒ EntityBuilder
constructor
A new instance of EntityBuilder.
- #result ⇒ Object
Constructor Details
#initialize(name, persistable) ⇒ EntityBuilder
Returns a new instance of EntityBuilder.
432 433 434 435 436 437 438 |
# File 'lib/mendix_bridge/dsl.rb', line 432 def initialize(name, persistable) @name = name.to_s @persistable = persistable @attributes = [] @associations = [] @access_rules = [] end |
Class Method Details
.build(name, persistable: true, &block) ⇒ Object
426 427 428 429 430 |
# File 'lib/mendix_bridge/dsl.rb', line 426 def self.build(name, persistable: true, &block) builder = new(name, persistable) builder.instance_eval(&block) if block builder.result end |
Instance Method Details
#access(role, create: false, delete: false, read: [], write: [], where: nil) ⇒ Object
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/mendix_bridge/dsl.rb', line 493 def access( role, create: false, delete: false, read: [], write: [], where: nil ) role = role.to_s raise ArgumentError, "duplicate entity access role: #{role}" if @access_rules.any? { |rule| rule.role == role } @access_rules << Model::AccessRule.new( role:, create: !!create, delete: !!delete, read: access_attributes(read), write: access_attributes(write), xpath: where&.to_s ) end |
#association(name, target:, cardinality: :one, owner: :default) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/mendix_bridge/dsl.rb', line 478 def association(name, target:, cardinality: :one, owner: :default) cardinality = cardinality.to_sym unless CARDINALITIES.include?(cardinality) raise ArgumentError, "cardinality must be :one or :many" end ensure_unique!(@associations, name, "association") @associations << Model::Association.new( name: name.to_s, target: target.to_s, cardinality: cardinality.to_s, owner: owner.to_s ) end |
#attribute(name, type, required: false, default: nil, length: 200, enumeration: nil) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/mendix_bridge/dsl.rb', line 440 def attribute( name, type, required: false, default: nil, length: 200, enumeration: nil ) type = type.to_sym unless ATTRIBUTE_TYPES.include?(type) raise ArgumentError, "unsupported attribute type #{type.inspect}; " \ "expected one of: #{ATTRIBUTE_TYPES.join(', ')}" end ensure_unique!(@attributes, name, "attribute") if type == :enumeration && enumeration.nil? raise ArgumentError, "enumeration attributes require enumeration: \"Module.Name\"" end = case type when :string { length: } when :enumeration { enumeration: enumeration.to_s } else {} end @attributes << Model::Attribute.new( name: name.to_s, type: type.to_s, required:, default:, options: ) end |