Class: Udb::Presence

Inherits:
Object
  • Object
show all
Defined in:
lib/udb/presence.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) ⇒ Presence

Returns a new instance of Presence.

Parameters:

  • data (Hash, String)

    The presence data from the architecture spec



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/udb/presence.rb', line 23

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.



20
21
22
# File 'lib/udb/presence.rb', line 20

def optional_type
  @optional_type
end

#presenceObject (readonly)

Returns the value of attribute presence.



19
20
21
# File 'lib/udb/presence.rb', line 19

def presence
  @presence
end

Class Method Details

.mandatoryObject

Class methods



51
# File 'lib/udb/presence.rb', line 51

def self.mandatory = "mandatory"

.optionalObject



52
# File 'lib/udb/presence.rb', line 52

def self.optional = "optional"

.optional_type_developmentObject



54
# File 'lib/udb/presence.rb', line 54

def self.optional_type_development = "development"

.optional_type_expansionObject



55
# File 'lib/udb/presence.rb', line 55

def self.optional_type_expansion = "expansion"

.optional_type_localizedObject



53
# File 'lib/udb/presence.rb', line 53

def self.optional_type_localized = "localized"

.optional_type_transitoryObject



56
# File 'lib/udb/presence.rb', line 56

def self.optional_type_transitory = "transitory"

.optional_typesObject



59
60
61
62
63
# File 'lib/udb/presence.rb', line 59

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

.optional_types_objObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/udb/presence.rb', line 77

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 << Presence.new({ self.optional => optional_type })
  end

  @optional_types_obj
end

.presence_typesObject



58
# File 'lib/udb/presence.rb', line 58

def self.presence_types = [mandatory, optional]

.presence_types_objObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/udb/presence.rb', line 65

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 << Presence.new(presence_type)
  end

  @presence_types_obj
end

Instance Method Details

#<(other) ⇒ Boolean

Returns Whether or not this Presence is less-than the other.

Parameters:

  • other (Presence)

    An extension presence object

Returns:

  • (Boolean)

    Whether or not this Presence is less-than the other

Raises:

  • (ArgumentError)


144
145
146
147
# File 'lib/udb/presence.rb', line 144

def <(other)
  raise ArgumentError, "Presence is only comparable to other Presence classes" unless other.is_a?(Presence)
  (self.optional? && other.mandatory?)
end

#<=(other) ⇒ Boolean

Returns Whether or not this Presence is less-than or equal to the other.

Parameters:

  • other (Presence)

    An extension presence object

Returns:

  • (Boolean)

    Whether or not this Presence is less-than or equal to the other

Raises:

  • (ArgumentError)


152
153
154
155
# File 'lib/udb/presence.rb', line 152

def <=(other)
  raise ArgumentError, "Presence is only comparable to other Presence classes" unless other.is_a?(Presence)
  (self < other) || (self == other)
end

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

Overloads:

  • #==(other) ⇒ Boolean

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

    Parameters:

    • other (String)

      A presence string

    Returns:

    • (Boolean)

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

  • #==(other) ⇒ Boolean

    Returns whether or not this Presence has the exact same presence and optional_type as other Ignores optional_type if either self or other have it as nil.

    Parameters:

    • other (Presence)

      An extension presence object

    Returns:

    • (Boolean)

      whether or not this Presence has the exact same presence and optional_type as other Ignores optional_type if either self or other have it as nil.



107
108
109
110
111
112
113
114
115
116
# File 'lib/udb/presence.rb', line 107

def ==(other)
  case other
  when String
    @presence == other
  when Presence
    @presence == other.presence && (@optional_type.nil? || other.optional_type.nil? || @optional_type == other.optional_type)
  else
    raise "Unexpected comparison"
  end
end

#>(other) ⇒ Boolean

Returns Whether or not this Presence is greater-than the other.

Parameters:

  • other (Presence)

    An extension presence object

Returns:

  • (Boolean)

    Whether or not this Presence is greater-than the other

Raises:

  • (ArgumentError)


128
129
130
131
# File 'lib/udb/presence.rb', line 128

def >(other)
  raise ArgumentError, "Presence is only comparable to other Presence classes" unless other.is_a?(Presence)
  (self.mandatory? && other.optional?)
end

#>=(other) ⇒ Boolean

Returns Whether or not this Presence is greater-than or equal to the other.

Parameters:

  • other (Presence)

    An extension presence object

Returns:

  • (Boolean)

    Whether or not this Presence is greater-than or equal to the other

Raises:

  • (ArgumentError)


136
137
138
139
# File 'lib/udb/presence.rb', line 136

def >=(other)
  raise ArgumentError, "Presence is only comparable to other Presence classes" unless other.is_a?(Presence)
  (self > other) || (self == other)
end

#mandatory?Boolean

Returns:

  • (Boolean)


47
# File 'lib/udb/presence.rb', line 47

def mandatory? = (@presence == "mandatory")

#optional?Boolean

Returns:

  • (Boolean)


48
# File 'lib/udb/presence.rb', line 48

def optional? = (@presence == "optional")

#to_sObject



92
93
94
# File 'lib/udb/presence.rb', line 92

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

#to_s_conciseObject



96
97
98
# File 'lib/udb/presence.rb', line 96

def to_s_concise
  "#{presence}"
end

#uses_optional_types?Boolean

Returns True if Presence object differentiates between optional types.

Returns:

  • (Boolean)

    True if Presence object differentiates between optional types.



90
# File 'lib/udb/presence.rb', line 90

def uses_optional_types? = !@optional_type.nil?