Class: Aequitas::VirtusIntegration::InlineAttributeRuleExtractor::String

Inherits:
Object
  • Object
show all
Defined in:
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb

Instance Attribute Summary

Attributes inherited from Object

#attribute

Instance Method Summary collapse

Methods inherited from Object

#extract_presence_rule, #extract_primitive_type_rule, #initialize, #options

Constructor Details

This class inherits a constructor from Aequitas::VirtusIntegration::InlineAttributeRuleExtractor::Object

Instance Method Details

#extractObject



10
11
12
13
14
# File 'lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb', line 10

def extract
  rules = super
  rules.concat(Array(extract_length_rules))
  rules.concat(Array(extract_format_rules))
end

#extract_format_rulesObject



27
28
29
30
# File 'lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb', line 27

def extract_format_rules
  format = options.fetch(:format, false)
  Rule::Format.rules_for(attribute.name, :with => format) if format
end

#extract_length_rulesObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb', line 16

def extract_length_rules
  length = options.fetch(:length, false)

  case length
  when ::Integer; Rule::Length::Equal.new(attribute.name, :expected => length)
  when ::Range;   Rule::Length::Range.new(attribute.name, :range    => length)
  when ::FalseClass;
  else raise ArgumentError, "expected Integer or Range :length, got: #{length.inspect}"
  end
end