Class: Udb::Presence
- Inherits:
-
Object
- Object
- Udb::Presence
- 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
-
#optional_type ⇒ Object
readonly
Returns the value of attribute optional_type.
-
#presence ⇒ Object
readonly
Returns the value of attribute presence.
Class Method Summary collapse
-
.mandatory ⇒ Object
Class methods.
- .optional ⇒ Object
- .optional_type_development ⇒ Object
- .optional_type_expansion ⇒ Object
- .optional_type_localized ⇒ Object
- .optional_type_transitory ⇒ Object
- .optional_types ⇒ Object
- .optional_types_obj ⇒ Object
- .presence_types ⇒ Object
- .presence_types_obj ⇒ Object
Instance Method Summary collapse
- #<(other) ⇒ Boolean
- #<=(other) ⇒ Boolean
- #==(other) ⇒ Object
- #>(other) ⇒ Boolean
- #>=(other) ⇒ Boolean
-
#initialize(data) ⇒ Presence
constructor
A new instance of Presence.
- #mandatory? ⇒ Boolean
- #optional? ⇒ Boolean
- #to_s ⇒ Object
- #to_s_concise ⇒ Object
-
#uses_optional_types? ⇒ Boolean
True if Presence object differentiates between optional types.
Constructor Details
#initialize(data) ⇒ Presence
Returns a new instance of Presence.
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_type ⇒ Object (readonly)
Returns the value of attribute optional_type.
20 21 22 |
# File 'lib/udb/presence.rb', line 20 def optional_type @optional_type end |
#presence ⇒ Object (readonly)
Returns the value of attribute presence.
19 20 21 |
# File 'lib/udb/presence.rb', line 19 def presence @presence end |
Class Method Details
.mandatory ⇒ Object
Class methods
51 |
# File 'lib/udb/presence.rb', line 51 def self.mandatory = "mandatory" |
.optional ⇒ Object
52 |
# File 'lib/udb/presence.rb', line 52 def self.optional = "optional" |
.optional_type_development ⇒ Object
54 |
# File 'lib/udb/presence.rb', line 54 def self.optional_type_development = "development" |
.optional_type_expansion ⇒ Object
55 |
# File 'lib/udb/presence.rb', line 55 def self.optional_type_expansion = "expansion" |
.optional_type_localized ⇒ Object
53 |
# File 'lib/udb/presence.rb', line 53 def self.optional_type_localized = "localized" |
.optional_type_transitory ⇒ Object
56 |
# File 'lib/udb/presence.rb', line 56 def self.optional_type_transitory = "transitory" |
.optional_types ⇒ Object
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_obj ⇒ Object
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_types ⇒ Object
58 |
# File 'lib/udb/presence.rb', line 58 def self.presence_types = [mandatory, optional] |
.presence_types_obj ⇒ Object
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
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
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
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
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
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
47 |
# File 'lib/udb/presence.rb', line 47 def mandatory? = (@presence == "mandatory") |
#optional? ⇒ Boolean
48 |
# File 'lib/udb/presence.rb', line 48 def optional? = (@presence == "optional") |
#to_s ⇒ Object
92 93 94 |
# File 'lib/udb/presence.rb', line 92 def to_s @optional_type.nil? ? "#{presence}" : "#{presence} (#{optional_type})" end |
#to_s_concise ⇒ Object
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.
90 |
# File 'lib/udb/presence.rb', line 90 def uses_optional_types? = !@optional_type.nil? |