Class: ExtensionPresence

Inherits:
Object
  • Object
show all
Defined in:
lib/arch_obj_models/extension.rb

Overview

Is the extension mandatory, optional, various kinds of optional, etc. Accepts two kinds of YAML schemas:

String
  Example => presence: mandatory
Hash
  Must have the key "optional" with a String value
  Example => presence:
               optional: development

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ExtensionPresence

Returns a new instance of ExtensionPresence.

Parameters:

  • data (Hash, String)

    The presence data from the architecture spec



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/arch_obj_models/extension.rb', line 455

def initialize(data)
  if data.is_a?(String)
    raise "Unknown extension presence of #{data}" unless ["mandatory","optional"].include?(data)

    @presence = data
    @optional_type = nil
  elsif data.is_a?(Hash)
    data.each do |key, value|
      if key == "optional"
        raise ArgumentError, "Extension presence hash #{data} missing type of optional" if value.nil?
        raise ArgumentError, "Unknown extension presence optional #{value} for type of optional" unless
          ["localized", "development", "expansion", "transitory"].include?(value)

        @presence = key
        @optional_type = value
      else
        raise ArgumentError, "Extension presence hash #{data} has unsupported key of #{key}"
      end
    end
  else
    raise ArgumentError, "Extension presence is a #{data.class} but only String or Hash are supported"
  end
end

Instance Attribute Details

#optional_typeObject (readonly)

Returns the value of attribute optional_type.



452
453
454
# File 'lib/arch_obj_models/extension.rb', line 452

def optional_type
  @optional_type
end

#presenceObject (readonly)

Returns the value of attribute presence.



451
452
453
# File 'lib/arch_obj_models/extension.rb', line 451

def presence
  @presence
end

Class Method Details

.mandatoryObject

Class methods



483
# File 'lib/arch_obj_models/extension.rb', line 483

def self.mandatory = "mandatory"

.optionalObject



484
# File 'lib/arch_obj_models/extension.rb', line 484

def self.optional = "optional"

.optional_type_developmentObject



486
# File 'lib/arch_obj_models/extension.rb', line 486

def self.optional_type_development = "development"

.optional_type_expansionObject



487
# File 'lib/arch_obj_models/extension.rb', line 487

def self.optional_type_expansion = "expansion"

.optional_type_localizedObject



485
# File 'lib/arch_obj_models/extension.rb', line 485

def self.optional_type_localized = "localized"

.optional_type_transitoryObject



488
# File 'lib/arch_obj_models/extension.rb', line 488

def self.optional_type_transitory = "transitory"

.optional_typesObject



491
492
493
494
495
# File 'lib/arch_obj_models/extension.rb', line 491

def self.optional_types = [
optional_type_localized,
optional_type_development,
optional_type_expansion,
optional_type_transitory]

.optional_types_objObject



509
510
511
512
513
514
515
516
517
518
519
# File 'lib/arch_obj_models/extension.rb', line 509

def self.optional_types_obj
  return @optional_types_obj unless @optional_types_obj.nil?

  @optional_types_obj = []

  optional_types.each do |optional_type|
    @optional_types_obj << ExtensionPresence.new({ self.optional => optional_type })
  end

  @optional_types_obj
end

.presence_typesObject



490
# File 'lib/arch_obj_models/extension.rb', line 490

def self.presence_types = [mandatory, optional]

.presence_types_objObject



497
498
499
500
501
502
503
504
505
506
507
# File 'lib/arch_obj_models/extension.rb', line 497

def self.presence_types_obj
  return @presence_types_obj unless @presence_types_obj.nil?

  @presence_types_obj = []

  presence_types.each do |presence_type|
    @presence_types_obj << ExtensionPresence.new(presence_type)
  end

  @presence_types_obj
end

Instance Method Details

#<=>(other) ⇒ Object

Sorts by presence, then by optional_type

Raises:

  • (ArgumentError)


543
544
545
546
547
548
549
550
551
# File 'lib/arch_obj_models/extension.rb', line 543

def <=>(other)
  raise ArgumentError, "ExtensionPresence is only comparable to other ExtensionPresence classes" unless other.is_a?(ExtensionPresence)

  if @presence != other.presence
    @presence <=> other.presence
  else
    @optional_type <=> other.optional_type
  end
end

#==(other) ⇒ Boolean #==(other) ⇒ Boolean

Overloads:

  • #==(other) ⇒ Boolean

    Returns whether or not this ExtensionPresence has the same presence (ignores optional_type).

    Parameters:

    • other (String)

      A presence string

    Returns:

    • (Boolean)

      whether or not this ExtensionPresence has the same presence (ignores optional_type)

  • #==(other) ⇒ Boolean

    Returns whether or not this ExtensionPresence has the exact same presence and optional_type as other.

    Parameters:

    Returns:

    • (Boolean)

      whether or not this ExtensionPresence has the exact same presence and optional_type as other



531
532
533
534
535
536
537
538
539
540
# File 'lib/arch_obj_models/extension.rb', line 531

def ==(other)
  case other
  when String
    @presence == other
  when ExtensionPresence
    @presence == other.presence && @optional_type == other.optional_type
  else
    raise "Unexpected comparison"
  end
end

#mandatory?Boolean

Returns:

  • (Boolean)


479
# File 'lib/arch_obj_models/extension.rb', line 479

def mandatory? = (@presence == mandatory)

#optional?Boolean

Returns:

  • (Boolean)


480
# File 'lib/arch_obj_models/extension.rb', line 480

def optional? = (@presence == optional)

#to_sObject



521
522
523
# File 'lib/arch_obj_models/extension.rb', line 521

def to_s
  @optional_type.nil? ? "#{presence}" : "#{presence} (#{optional_type})"
end