Class: Esquema::Property
- Inherits:
-
Object
- Object
- Esquema::Property
- Defined in:
- lib/esquema/property.rb
Overview
Represents a property in the Esquema schema.
Constant Summary collapse
- DB_TO_JSON_TYPE_MAPPINGS =
Mapping of database types to JSON types.
{ date: "date", datetime: "date-time", time: "time", string: "string", text: "string", integer: "integer", float: "number", number: "number", decimal: "number", boolean: "boolean", array: "array", object: "object" }.freeze
- NUMERIC_CONSTRAINT_KEYWORDS =
%i[minimum maximum exclusiveMinimum exclusiveMaximum multipleOf].freeze
- STRING_CONSTRAINT_KEYWORDS =
%i[maxLength minLength pattern format].freeze
- ARRAY_CONSTRAINT_KEYWORDS =
%i[maxItems minItems uniqueItems items].freeze
- OBJECT_CONSTRAINT_KEYWORDS =
%i[maxProperties minProperties properties additionalProperties dependencies].freeze
- LOGICAL_KEYWORDS =
%i[allOf anyOf oneOf not].freeze
- GENERIC_KEYWORDS =
%i[type default title description enum const].freeze
- KEYWORDS =
( NUMERIC_CONSTRAINT_KEYWORDS + STRING_CONSTRAINT_KEYWORDS + ARRAY_CONSTRAINT_KEYWORDS + OBJECT_CONSTRAINT_KEYWORDS + LOGICAL_KEYWORDS + GENERIC_KEYWORDS ).freeze
- FORMAT_OPTIONS =
%i[date-time date time email hostname ipv4 ipv6 uri uuid uri-reference uri-template].freeze
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Converts the Property instance to a JSON representation.
- #build_additionalproperties ⇒ Object
- #build_allof ⇒ Object
- #build_anyof ⇒ Object
- #build_const ⇒ Object
-
#build_default ⇒ Object?
Builds the default attribute for the Property.
- #build_dependencies ⇒ Object
-
#build_description ⇒ String?
Builds the description attribute for the Property.
-
#build_enum ⇒ Array?
Builds the enum attribute for the Property.
- #build_exclusivemaximum ⇒ Object
- #build_exclusiveminimum ⇒ Object
- #build_format ⇒ Object
-
#build_items ⇒ Hash?
Builds the items attribute for the Property.
- #build_maximum ⇒ Object
-
#build_maxitems ⇒ Object
rubocop:disable Metrics/AbcSize.
- #build_maxlength ⇒ Object
- #build_maxproperties ⇒ Object
- #build_minimum ⇒ Object
-
#build_minitems ⇒ Object
rubocop:disable Metrics/AbcSize.
- #build_minlength ⇒ Object
- #build_minproperties ⇒ Object
- #build_multipleof ⇒ Object
- #build_not ⇒ Object
- #build_oneof ⇒ Object
- #build_pattern ⇒ Object
- #build_properties ⇒ Object
-
#build_title ⇒ String
Builds the title attribute for the Property.
-
#build_type ⇒ String?
Builds the type attribute for the Property.
- #build_uniqueitems ⇒ Object
-
#initialize(object, options = {}) ⇒ Property
constructor
Initializes a new Property instance.
Constructor Details
#initialize(object, options = {}) ⇒ Property
Initializes a new Property instance.
An object can be any of the following instance types:
An ActiveRecord column. Example: ActiveRecord::ConnectionAdapters::SQLite3::Column
An ActiveRecord association reflection. Example: ActiveRecord::Reflection::BelongsToReflection
An Esquema virtual column. Example: Esquema::VirtualColumn
52 53 54 55 56 57 |
# File 'lib/esquema/property.rb', line 52 def initialize(object, = {}) raise ArgumentError, "property must have a name" unless object.respond_to?(:name) @object = object @options = end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
41 42 43 |
# File 'lib/esquema/property.rb', line 41 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
41 42 43 |
# File 'lib/esquema/property.rb', line 41 def @options end |
Instance Method Details
#as_json ⇒ Hash
Converts the Property instance to a JSON representation.
62 63 64 65 66 67 68 69 |
# File 'lib/esquema/property.rb', line 62 def as_json KEYWORDS.each_with_object({}) do |property, hash| value = send("build_#{property.downcase}") next if value.nil? || (value.is_a?(String) && value.empty?) hash[property] = value end.compact end |
#build_additionalproperties ⇒ Object
210 211 212 |
# File 'lib/esquema/property.rb', line 210 def build_additionalproperties [:additionalProperties] end |
#build_allof ⇒ Object
218 219 220 |
# File 'lib/esquema/property.rb', line 218 def build_allof [:allOf] end |
#build_anyof ⇒ Object
222 223 224 |
# File 'lib/esquema/property.rb', line 222 def build_anyof [:anyOf] end |
#build_const ⇒ Object
234 235 236 |
# File 'lib/esquema/property.rb', line 234 def build_const [:const] end |
#build_default ⇒ Object?
Builds the default attribute for the Property.
81 82 83 84 85 86 87 |
# File 'lib/esquema/property.rb', line 81 def build_default return unless object.respond_to?(:default) default_value = object.default || [:default].presence @default = TypeCaster.cast(object.type, default_value) unless default_value.nil? end |
#build_dependencies ⇒ Object
214 215 216 |
# File 'lib/esquema/property.rb', line 214 def build_dependencies [:dependencies] end |
#build_description ⇒ String?
Builds the description attribute for the Property.
103 104 105 |
# File 'lib/esquema/property.rb', line 103 def build_description [:description] end |
#build_enum ⇒ Array?
Builds the enum attribute for the Property.
124 125 126 |
# File 'lib/esquema/property.rb', line 124 def build_enum [:enum] end |
#build_exclusivemaximum ⇒ Object
140 141 142 |
# File 'lib/esquema/property.rb', line 140 def build_exclusivemaximum [:exclusiveMaximum] end |
#build_exclusiveminimum ⇒ Object
136 137 138 |
# File 'lib/esquema/property.rb', line 136 def build_exclusiveminimum [:exclusiveMinimum] end |
#build_format ⇒ Object
160 161 162 |
# File 'lib/esquema/property.rb', line 160 def build_format [:format] end |
#build_items ⇒ Hash?
Builds the items attribute for the Property.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/esquema/property.rb', line 110 def build_items return unless object.try(:collection?) case object.type when :array { type: DB_TO_JSON_TYPE_MAPPINGS[object.item_type] } else Builder.new(object.klass).build_schema end end |
#build_maximum ⇒ Object
132 133 134 |
# File 'lib/esquema/property.rb', line 132 def build_maximum [:maximum] end |
#build_maxitems ⇒ Object
rubocop:disable Metrics/AbcSize
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/esquema/property.rb', line 164 def build_maxitems # rubocop:disable Metrics/AbcSize raise ArgumentError, "maxItems must be an integer" if [:maxItems] && ![:maxItems].is_a?(Integer) if [:maxItems]&.negative? raise ArgumentError, "maxItems must be a non-negative integer" end if [:maxItems] && [:type] != :array raise ArgumentError, "maxItems must be use for array type properties only." end [:maxItems] end |
#build_maxlength ⇒ Object
148 149 150 |
# File 'lib/esquema/property.rb', line 148 def build_maxlength [:maxLength] end |
#build_maxproperties ⇒ Object
202 203 204 |
# File 'lib/esquema/property.rb', line 202 def build_maxproperties [:maxProperties] end |
#build_minimum ⇒ Object
128 129 130 |
# File 'lib/esquema/property.rb', line 128 def build_minimum [:minimum] end |
#build_minitems ⇒ Object
rubocop:disable Metrics/AbcSize
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/esquema/property.rb', line 179 def build_minitems # rubocop:disable Metrics/AbcSize raise ArgumentError, "minItems must be an integer" if [:minItems] && ![:minItems].is_a?(Integer) if [:minItems]&.negative? raise ArgumentError, "minItems must be a non-negative integer" end if [:minItems] && [:type] != :array raise ArgumentError, "minItems must be use for array type properties only." end [:minItems] end |
#build_minlength ⇒ Object
152 153 154 |
# File 'lib/esquema/property.rb', line 152 def build_minlength [:minLength] end |
#build_minproperties ⇒ Object
206 207 208 |
# File 'lib/esquema/property.rb', line 206 def build_minproperties [:minProperties] end |
#build_multipleof ⇒ Object
144 145 146 |
# File 'lib/esquema/property.rb', line 144 def build_multipleof [:multipleof] end |
#build_not ⇒ Object
230 231 232 |
# File 'lib/esquema/property.rb', line 230 def build_not [:not] end |
#build_oneof ⇒ Object
226 227 228 |
# File 'lib/esquema/property.rb', line 226 def build_oneof [:oneOf] end |
#build_pattern ⇒ Object
156 157 158 |
# File 'lib/esquema/property.rb', line 156 def build_pattern [:pattern] end |
#build_properties ⇒ Object
198 199 200 |
# File 'lib/esquema/property.rb', line 198 def build_properties [:properties] end |
#build_title ⇒ String
Builds the title attribute for the Property.
74 75 76 |
# File 'lib/esquema/property.rb', line 74 def build_title [:title].presence || object.name.to_s.humanize end |
#build_type ⇒ String?
Builds the type attribute for the Property.
92 93 94 95 96 97 98 |
# File 'lib/esquema/property.rb', line 92 def build_type return DB_TO_JSON_TYPE_MAPPINGS[:array] if object.try(:collection?) return unless object.respond_to?(:type) @type = DB_TO_JSON_TYPE_MAPPINGS[object.type] end |
#build_uniqueitems ⇒ Object
194 195 196 |
# File 'lib/esquema/property.rb', line 194 def build_uniqueitems [:uniqueItems] end |