Module: Attributor::Type::ClassMethods
- Defined in:
- lib/attributor/type.rb
Instance Method Summary collapse
- #anonymous? ⇒ Boolean
-
#anonymous_type(val = true) ⇒ Object
Allow a type to be marked as if it was anonymous (i.e. not referenceable by name).
- #as_json_schema(shallow: false, example: nil, attribute_options: {}) ⇒ Object
-
#check_option!(name, definition) ⇒ Object
HELPER FUNCTIONS.
-
#compile_dsl(options, block) ⇒ Object
By default, non complex types will not have a DSL subdefinition this handles such case.
-
#constructable? ⇒ Boolean
Does this type support the generation of subtypes?.
-
#describe(_root = false, example: nil) ⇒ Object
Default describe for simple types…only their name (stripping the base attributor module).
- #describe_option(option_name, option_value) ⇒ Object
- #dsl_compiler ⇒ Object
-
#dump(value, **_opts) ⇒ Object
Generic encoding of the attribute.
-
#example(_context = nil, options: {}) ⇒ Object
Default, overridable example function.
- #family ⇒ Object
- #generate_subcontext(context, subname) ⇒ Object
- #id ⇒ Object
-
#json_schema_string_format ⇒ Object
Default no format in case it’s a string type.
-
#load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
Generic decoding and coercion of the attribute.
- #options ⇒ Object
-
#valid_type?(value) ⇒ Boolean
Default, overridable valid_type? function.
-
#validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) ⇒ Object
TODO: refactor this to take just the options instead of the full attribute? TODO: delegate to subclass.
Instance Method Details
#anonymous? ⇒ Boolean
73 74 75 76 77 78 79 |
# File 'lib/attributor/type.rb', line 73 def anonymous? if @_anonymous.nil? name.nil? # if nothing is set, consider it anonymous if the class does not have a name else @_anonymous end end |
#anonymous_type(val = true) ⇒ Object
Allow a type to be marked as if it was anonymous (i.e. not referenceable by name)
69 70 71 |
# File 'lib/attributor/type.rb', line 69 def anonymous_type(val = true) @_anonymous = val end |
#as_json_schema(shallow: false, example: nil, attribute_options: {}) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/attributor/type.rb', line 186 def as_json_schema( shallow: false, example: nil, attribute_options: {} ) type_name = self.ancestors.find { |k| k.name && !k.name.empty? }.name hash = { type: json_schema_type, 'x-type_name': type_name.gsub( Attributor::MODULE_PREFIX_REGEX, '' )} # Add a format, if the type has defined if hash[:type] == :string && the_format = json_schema_string_format hash[:format] = the_format end # Common options hash[:enum] = [:values] if [:values] hash end |
#check_option!(name, definition) ⇒ Object
HELPER FUNCTIONS
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/attributor/type.rb', line 127 def check_option!(name, definition) case name when :min raise AttributorException, "Value for option :min does not implement '<='. Got: (#{definition.inspect})" unless definition.respond_to?(:<=) when :max raise AttributorException, "Value for option :max does not implement '>='. Got(#{definition.inspect})" unless definition.respond_to?(:>=) when :regexp # could go for a respoind_to? :=~ here, but that seems overly... cute... and not useful. raise AttributorException, "Value for option :regexp is not a Regexp object. Got (#{definition.inspect})" unless definition.is_a? ::Regexp else return :unknown end :ok end |
#compile_dsl(options, block) ⇒ Object
By default, non complex types will not have a DSL subdefinition this handles such case
152 153 154 155 156 157 |
# File 'lib/attributor/type.rb', line 152 def compile_dsl(, block) raise AttributorException, 'Basic structures cannot take extra block definitions' if block # Simply create a DSL compiler to store the options, and not to parse any DSL sub_definition = dsl_compiler.new() sub_definition end |
#constructable? ⇒ Boolean
Does this type support the generation of subtypes?
64 65 66 |
# File 'lib/attributor/type.rb', line 64 def constructable? false end |
#describe(_root = false, example: nil) ⇒ Object
Default describe for simple types…only their name (stripping the base attributor module)
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/attributor/type.rb', line 160 def describe(_root = false, example: nil) type_name = Attributor.type_name(self) hash = { name: type_name.gsub(Attributor::MODULE_PREFIX_REGEX, ''), family: family, id: id } hash[:anonymous] = @_anonymous unless @_anonymous.nil? hash[:example] = example if example hash end |
#describe_option(option_name, option_value) ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/attributor/type.rb', line 199 def describe_option( option_name, option_value ) return case option_name when :description option_value else option_value # By default, describing an option returns the hash with the specification end end |
#dsl_compiler ⇒ Object
147 148 149 |
# File 'lib/attributor/type.rb', line 147 def dsl_compiler DSLCompiler end |
#dump(value, **_opts) ⇒ Object
Generic encoding of the attribute
92 93 94 |
# File 'lib/attributor/type.rb', line 92 def dump(value, **_opts) value end |
#example(_context = nil, options: {}) ⇒ Object
Default, overridable example function
121 122 123 |
# File 'lib/attributor/type.rb', line 121 def example(_context = nil, options: {}) raise AttributorException, "#{self} must implement #example" end |
#family ⇒ Object
177 178 179 |
# File 'lib/attributor/type.rb', line 177 def family 'any' end |
#generate_subcontext(context, subname) ⇒ Object
143 144 145 |
# File 'lib/attributor/type.rb', line 143 def generate_subcontext(context, subname) context + [subname] end |
#id ⇒ Object
172 173 174 175 |
# File 'lib/attributor/type.rb', line 172 def id return nil if name.nil? name.gsub('::'.freeze, '-'.freeze) end |
#json_schema_string_format ⇒ Object
Default no format in case it’s a string type
182 183 184 |
# File 'lib/attributor/type.rb', line 182 def json_schema_string_format nil end |
#load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
Generic decoding and coercion of the attribute.
82 83 84 85 86 87 88 89 |
# File 'lib/attributor/type.rb', line 82 def load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **) return nil if value.nil? unless value.is_a?(native_type) raise Attributor::IncompatibleTypeError.new(context: context, value_type: value.class, type: self) end value end |
#options ⇒ Object
207 208 209 |
# File 'lib/attributor/type.rb', line 207 def {} end |
#valid_type?(value) ⇒ Boolean
Default, overridable valid_type? function
114 115 116 117 118 |
# File 'lib/attributor/type.rb', line 114 def valid_type?(value) return value.is_a?(native_type) if respond_to?(:native_type) raise AttributorException, "#{self} must implement #valid_type? or #native_type" end |
#validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) ⇒ Object
TODO: refactor this to take just the options instead of the full attribute? TODO: delegate to subclass
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/attributor/type.rb', line 98 def validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) # rubocop:disable Style/OptionalArguments errors = [] attribute..each do |option, opt_definition| case option when :max errors << "#{Attributor.humanize_context(context)} value (#{value}) is larger than the allowed max (#{opt_definition.inspect})" unless value <= opt_definition when :min errors << "#{Attributor.humanize_context(context)} value (#{value}) is smaller than the allowed min (#{opt_definition.inspect})" unless value >= opt_definition when :regexp errors << "#{Attributor.humanize_context(context)} value (#{value}) does not match regexp (#{opt_definition.inspect})" unless value =~ opt_definition end end errors end |