Class: MediaTypes::Scheme::Rules
- Inherits:
-
Hash
- Object
- Hash
- MediaTypes::Scheme::Rules
- Defined in:
- lib/media_types/scheme/rules.rb
Instance Attribute Summary collapse
-
#expected_type ⇒ Object
readonly
Returns the value of attribute expected_type.
Instance Method Summary collapse
- #[](key) ⇒ Object (also: #get)
- #[]=(key, val) ⇒ Object
- #add(key, val, optional: false) ⇒ Object
- #allow_empty? ⇒ Boolean
- #clone ⇒ Object
- #default=(input_default) ⇒ Object
- #delete(key) ⇒ Object (also: #remove)
- #duplicate_string_key_name(key, val) ⇒ Object
- #duplicate_symbol_key_name(key, val) ⇒ Object
- #fetch(key, &block) ⇒ Object
- #get_original_key_type(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(allow_empty:, expected_type:) ⇒ Rules
constructor
A new instance of Rules.
- #inspect(indent = 0) ⇒ Object
- #inspect_format_attribute(indent, key, value = ) ⇒ Object
-
#merge(rules) ⇒ Rules
Merges another set of rules into a clone of this one.
-
#required(loose:) ⇒ Array<Symbol>
Returns the keys that are not options.
- #validate_input(key, val) ⇒ Object
- #validate_key_name(key, val) ⇒ Object
Constructor Details
#initialize(allow_empty:, expected_type:) ⇒ Rules
Returns a new instance of Rules.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/media_types/scheme/rules.rb', line 11 def initialize(allow_empty:, expected_type:) super({}) self.allow_empty = allow_empty self.expected_type = expected_type self.optional_keys = [] self.strict_keys = [] self.original_key_type = {} self.default = MissingValidation.new end |
Instance Attribute Details
#expected_type ⇒ Object
Returns the value of attribute expected_type.
9 10 11 |
# File 'lib/media_types/scheme/rules.rb', line 9 def expected_type @expected_type end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
27 28 29 |
# File 'lib/media_types/scheme/rules.rb', line 27 def [](key) __getobj__[normalize_key(key)] end |
#[]=(key, val) ⇒ Object
74 75 76 |
# File 'lib/media_types/scheme/rules.rb', line 74 def []=(key, val) add(key, val, optional: false) end |
#add(key, val, optional: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/media_types/scheme/rules.rb', line 31 def add(key, val, optional: false) validate_input(key, val) normalized_key = normalize_key(key) __getobj__[normalized_key] = val if optional == :loose strict_keys << normalized_key else optional_keys << normalized_key if optional end original_key_type[normalized_key] = key.class self end |
#allow_empty? ⇒ Boolean
23 24 25 |
# File 'lib/media_types/scheme/rules.rb', line 23 def allow_empty? allow_empty end |
#clone ⇒ Object
108 109 110 111 112 |
# File 'lib/media_types/scheme/rules.rb', line 108 def clone super.tap do |cloned| cloned.__setobj__(__getobj__.clone) end end |
#default=(input_default) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/media_types/scheme/rules.rb', line 163 def default=(input_default) unless default.nil? raise DuplicateAnyRuleError if !(default.is_a?(MissingValidation) || default.is_a?(NotStrict)) && !(input_default.is_a?(MissingValidation) || input_default.is_a?(NotStrict)) raise DuplicateNotStrictRuleError if default.is_a?(NotStrict) && input_default.is_a?(NotStrict) raise NotStrictOverwritingAnyError if !(default.is_a?(MissingValidation) || default.is_a?(NotStrict)) && input_default.is_a?(NotStrict) raise AnyOverwritingNotStrictError if default.is_a?(NotStrict) && !(input_default.is_a?(MissingValidation) || input_default.is_a?(NotStrict)) end super(input_default) end |
#delete(key) ⇒ Object Also known as: remove
82 83 84 85 |
# File 'lib/media_types/scheme/rules.rb', line 82 def delete(key) __getobj__.delete(normalize_key(key)) self end |
#duplicate_string_key_name(key, val) ⇒ Object
68 69 70 71 72 |
# File 'lib/media_types/scheme/rules.rb', line 68 def duplicate_string_key_name(key, val) raise DuplicateStringKeyError.new(val.class.name.split('::').last, key) if get_original_key_type(key) == String raise StringOverwritingSymbolError.new(val.class.name.split('::').last, key) end |
#duplicate_symbol_key_name(key, val) ⇒ Object
62 63 64 65 66 |
# File 'lib/media_types/scheme/rules.rb', line 62 def duplicate_symbol_key_name(key, val) raise DuplicateSymbolKeyError.new(val.class.name.split('::').last, key) if get_original_key_type(key) == Symbol raise SymbolOverwritingStringError.new(val.class.name.split('::').last, key) end |
#fetch(key, &block) ⇒ Object
78 79 80 |
# File 'lib/media_types/scheme/rules.rb', line 78 def fetch(key, &block) __getobj__.fetch(normalize_key(key), &block) end |
#get_original_key_type(key) ⇒ Object
157 158 159 160 161 |
# File 'lib/media_types/scheme/rules.rb', line 157 def get_original_key_type(key) raise format('Key %<key>s does not exist', key: key) unless has_key?(key) original_key_type[normalize_key(key)] end |
#has_key?(key) ⇒ Boolean
153 154 155 |
# File 'lib/media_types/scheme/rules.rb', line 153 def has_key?(key) __getobj__.key?(normalize_key(key)) end |
#inspect(indent = 0) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/media_types/scheme/rules.rb', line 134 def inspect(indent = 0) prefix = ' ' * indent return "#{prefix}[Error]Depth limit reached[/Error]" if indent > 5_000 [ "#{prefix}[Rules n=#{keys.length} optional=#{optional_keys.length} strict=#{strict_keys.length} allow_empty=#{allow_empty?}]", "#{prefix} #{inspect_format_attribute(indent, '*', default)}", *keys.map { |key| "#{prefix} #{inspect_format_attribute(indent, key)}" }, "#{prefix}[/Rules]" ].join("\n") end |
#inspect_format_attribute(indent, key, value = ) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/media_types/scheme/rules.rb', line 146 def inspect_format_attribute(indent, key, value = self[key]) [ [key.to_s, optional_keys.include?(key) && '(optional)' || nil].compact.join(' '), value.is_a?(Scheme) || value.is_a?(Rules) ? "\n#{value.inspect(indent + 2)}" : value.inspect ].join(': ') end |
#merge(rules) ⇒ Rules
Merges another set of rules into a clone of this one
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/media_types/scheme/rules.rb', line 120 def merge(rules) clone.instance_exec do __setobj__(__getobj__.merge(rules)) if rules.respond_to?(:optional_keys, true) optional_keys.push(*rules.send(:optional_keys)) end if rules.respond_to?(:strict_keys, true) strict_keys.push(*rules.send(:strict_keys)) end self end end |
#required(loose:) ⇒ Array<Symbol>
Returns the keys that are not options
#see #merge
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/media_types/scheme/rules.rb', line 95 def required(loose:) clone.tap do |cloned| optional_keys.each do |key| cloned.delete(key) end if loose strict_keys.each do |key| cloned.delete(key) end end end end |
#validate_input(key, val) ⇒ Object
46 47 48 49 50 |
# File 'lib/media_types/scheme/rules.rb', line 46 def validate_input(key, val) raise KeyTypeError, "Unexpected key type #{key.class.name}, please use either a symbol or string." unless key.is_a?(String) || key.is_a?(Symbol) validate_key_name(key, val) end |
#validate_key_name(key, val) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/media_types/scheme/rules.rb', line 52 def validate_key_name(key, val) return unless has_key?(key) if key.is_a?(Symbol) duplicate_symbol_key_name(key, val) else duplicate_string_key_name(key, val) end end |