класс ActiveSupport::OrderedOptions
Упорядоченные параметры
OrderedOptions наследуется от Hash и предоставляет динамические методы доступа.
С помощью Hash, пары ключ-значение обычно управляются так:
h = {}
h[:boy] = 'John'
h[:girl] = 'Mary'
h[:boy] # => 'John'
h[:girl] # => 'Mary'
h[:dog] # => nil
Используя OrderedOptions, вышеприведенный код можно переписать так:
h = ActiveSupport::OrderedOptions.new h.boy = 'John' h.girl = 'Mary' h.boy # => 'John' h.girl # => 'Mary' h.dog # => nil
Чтобы генерировать исключение, когда значение пустое, добавьте восклицательный знак к имени ключа, например:
h.dog! # => raises KeyError: :dog is blank
Общедоступные методы экземпляров
# File activesupport/lib/active_support/ordered_options.rb, line 41 def [](key) super(key.to_sym) end
Вызывает метод суперкласса
Также алиасируется как: _get
# File activesupport/lib/active_support/ordered_options.rb, line 37 def []=(key, value) super(key.to_sym, value) end
Вызывает метод суперкласса
_get(key)
Псевдоним для: []
# File activesupport/lib/active_support/ordered_options.rb, line 45 def dig(key, *identifiers) super(key.to_sym, *identifiers) end
Вызывает метод суперкласса
# File activesupport/lib/active_support/ordered_options.rb, line 68 def extractable_options? true end
# File activesupport/lib/active_support/ordered_options.rb, line 72
def inspect
"#<#{self.class.name} #{super}>"
end # File activesupport/lib/active_support/ordered_options.rb, line 49
def method_missing(name, *args)
name_string = +name.to_s
if name_string.chomp!("=")
self[name_string] = args.first
else
bangs = name_string.chomp!("!")
if bangs
self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
else
self[name_string]
end
end
end # File activesupport/lib/active_support/ordered_options.rb, line 64 def respond_to_missing?(name, include_private) true end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.