提交 2ef4d5ed 编写于 作者: F Francesco Rodriguez

fix output messages - docs [ci skip]

上级 77eb1fb2
......@@ -75,14 +75,14 @@ def initialize(base)
@messages = {}
end
def initialize_dup(other) #:nodoc:
def initialize_dup(other) # :nodoc:
@messages = other.messages.dup
super
end
# Clear the error messages.
#
# person.errors.full_messages # => ["name can not be nil"]
# person.errors.full_messages # => ["name can not be nil"]
# person.errors.clear
# person.errors.full_messages # => []
def clear
......@@ -92,9 +92,9 @@ def clear
# Returns +true+ if the error messages include an error for the given key
# +attribute+, +false+ otherwise.
#
# person.errors.messages # => { name: ["can not be nil"] }
# person.errors.messages # => {:name=>["can not be nil"]}
# person.errors.include?(:name) # => true
# person.errors.include?(:age) # => false
# person.errors.include?(:age) # => false
def include?(attribute)
(v = messages[attribute]) && v.any?
end
......@@ -103,7 +103,7 @@ def include?(attribute)
# Get messages for +key+.
#
# person.errors.messages # => { name: ["can not be nil"] }
# person.errors.messages # => {:name=>["can not be nil"]}
# person.errors.get(:name) # => ["can not be nil"]
# person.errors.get(:age) # => nil
def get(key)
......@@ -177,7 +177,7 @@ def size
# Returns all message values.
#
# person.errors.messages # => { name: ["can not be nil", "must be specified"] }
# person.errors.messages # => {:name=>["can not be nil", "must be specified"]}
# person.errors.values # => [["can not be nil", "must be specified"]]
def values
messages.values
......@@ -185,7 +185,7 @@ def values
# Returns all message keys.
#
# person.errors.messages # => { name: ["can not be nil", "must be specified"] }
# person.errors.messages # => {:name=>["can not be nil", "must be specified"]}
# person.errors.keys # => [:name]
def keys
messages.keys
......@@ -240,8 +240,8 @@ def to_xml(options={})
# object. You can pass the <tt>:full_messages</tt> option. This determines
# if the json object should contain full messages or not (false by default).
#
# person.as_json # => { name: ["can not be nil"] }
# person.as_json(full_messages: true) # => { name: ["name can not be nil"] }
# person.as_json # => {:name=>["can not be nil"]}
# person.as_json(full_messages: true) # => {:name=>["name can not be nil"]}
def as_json(options=nil)
to_hash(options && options[:full_messages])
end
......@@ -249,8 +249,8 @@ def as_json(options=nil)
# Returns a Hash of attributes with their error messages. If +full_messages+
# is +true+, it will contain full messages (see +full_message+).
#
# person.to_hash # => { name: ["can not be nil"] }
# person.to_hash(true) # => { name: ["name can not be nil"] }
# person.to_hash # => {:name=>["can not be nil"]}
# person.to_hash(true) # => {:name=>["name can not be nil"]}
def to_hash(full_messages = false)
if full_messages
messages = {}
......@@ -273,7 +273,7 @@ def to_hash(full_messages = false)
# # => ["is invalid", "must be implemented"]
#
# person.errors.messages
# # => { name: ["must be implemented", "is invalid"] }
# # => {:name=>["must be implemented", "is invalid"]}
#
# If +message+ is a symbol, it will be translated using the appropriate
# scope (see +generate_message+).
......@@ -286,9 +286,9 @@ def to_hash(full_messages = false)
# <tt>:strict</tt> option can also be set to any other exception.
#
# person.errors.add(:name, nil, strict: true)
# # => ActiveModel::StrictValidationFailed: name is invalid
# # => ActiveModel::StrictValidationFailed: name is invalid
# person.errors.add(:name, nil, strict: NameIsInvalid)
# # => NameIsInvalid: name is invalid
# # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
def add(attribute, message = nil, options = {})
......@@ -306,7 +306,7 @@ def add(attribute, message = nil, options = {})
#
# person.errors.add_on_empty(:name)
# person.errors.messages
# # => { name: ["can't be empty"] }
# # => {:name=>["can't be empty"]}
def add_on_empty(attributes, options = {})
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
......@@ -320,7 +320,7 @@ def add_on_empty(attributes, options = {})
#
# person.errors.add_on_blank(:name)
# person.errors.messages
# # => { name: ["can't be blank"] }
# # => {:name=>["can't be blank"]}
def add_on_blank(attributes, options = {})
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
......
......@@ -18,7 +18,7 @@ class Array
# end
#
# options(1, 2) # => {}
# options(1, 2, a: :b) # => {a: :b}
# options(1, 2, a: :b) # => {:a=>:b}
def extract_options!
if last.is_a?(Hash) && last.extractable_options?
pop
......
......@@ -23,7 +23,7 @@ class Array
# The last point is particularly worth comparing for some enumerables:
#
# Array(foo: :bar) # => [[:foo, :bar]]
# Array.wrap(foo: :bar) # => [{foo: :bar}]
# Array.wrap(foo: :bar) # => [{:foo=>:bar}]
#
# There's also a related idiom that uses the splat operator:
#
......
......@@ -7,7 +7,7 @@ class Hash
# h1.deep_merge(h2) #=> {x: {y: [7, 8, 9]}, z: "xyz"}
# h2.deep_merge(h1) #=> {x: {y: [4, 5, 6]}, z: [7, 8, 9]}
# h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
# #=> {x: {y: [4, 5, 6, 7, 8, 9]}, z: [7, 8, 9, "xyz"]}
# #=> {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}
def deep_merge(other_hash, &block)
dup.deep_merge!(other_hash, &block)
end
......
......@@ -21,7 +21,7 @@ def slice(*keys)
# Returns a hash containing the removed key/value pairs.
#
# { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
# # => {c: 3, d: 4}
# # => {:c=>3, :d=>4}
def slice!(*keys)
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
omit = slice(*self.keys - keys)
......@@ -32,8 +32,8 @@ def slice!(*keys)
# Removes and returns the key/value pairs matching the given keys.
#
# { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => { a: 1, b: 2 }
# { a: 1, b: 2 }.extract!(:a, :x) # => { a: 1 }
# { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
# { a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
def extract!(*keys)
keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
end
......
......@@ -736,7 +736,7 @@ end
person = Person.new
person.valid? # => false
person.errors
# => {name: ["can't be blank", "is too short (minimum is 3 characters)"]}
# => {:name=>["can't be blank", "is too short (minimum is 3 characters)"]}
person = Person.new(name: "John Doe")
person.valid? # => true
......
......@@ -2372,7 +2372,7 @@ This method is similar in purpose to `Kernel#Array`, but there are some differen
The last point is particularly worth comparing for some enumerables:
```ruby
Array.wrap(foo: :bar) # => [{foo: :bar}]
Array.wrap(foo: :bar) # => [{:foo=>:bar}]
Array(foo: :bar) # => [[:foo, :bar]]
```
......@@ -2557,7 +2557,7 @@ Ruby has a built-in method `Hash#merge` that merges two hashes:
```ruby
{a: 1, b: 1}.merge(a: 0, c: 2)
# => {a: 0, b: 1, c: 2}
# => {:a=>0, :b=>1, :c=>2}
```
Active Support defines a few more ways of merging hashes that may be convenient.
......@@ -2602,7 +2602,7 @@ Active Support defines `Hash#deep_merge`. In a deep merge, if a key is found in
```ruby
{a: {b: 1}}.deep_merge(a: {c: 2})
# => {a: {b: 1, c: 2}}
# => {:a=>{:b=>1, :c=>2}}
```
The method `deep_merge!` performs a deep merge in place.
......@@ -2641,17 +2641,17 @@ The method `diff` returns a hash that represents a diff of the receiver and the
# => {}, first rule
{a: 1}.diff(a: 2)
# => {a: 1}, second rule
# => {:a=>1}, second rule
{a: 1}.diff(b: 2)
# => {a: 1, b: 2}, third rule
# => {:a=>1, :b=>2}, third rule
{a: 1, b: 2, c: 3}.diff(b: 1, c: 3, d: 4)
# => {a: 1, b: 2, d: 4}, all rules
# => {:a=>1, :b=>2, :d=>4}, all rules
{}.diff({}) # => {}
{a: 1}.diff({}) # => {a: 1}
{}.diff(a: 1) # => {a: 1}
{a: 1}.diff({}) # => {:a=>1}
{}.diff(a: 1) # => {:a=>1}
```
An important property of this diff hash is that you can retrieve the original hash by applying `diff` twice:
......@@ -2671,7 +2671,7 @@ NOTE: Defined in `active_support/core_ext/hash/diff.rb`.
The method `except` returns a hash with the keys in the argument list removed, if present:
```ruby
{a: 1, b: 2}.except(:a) # => {b: 2}
{a: 1, b: 2}.except(:a) # => {:b=>2}
```
If the receiver responds to `convert_key`, the method is called on each of the arguments. This allows `except` to play nice with hashes with indifferent access for instance:
......@@ -2776,7 +2776,7 @@ The method `symbolize_keys` returns a hash that has a symbolized version of the
```ruby
{nil => nil, 1 => 1, "a" => "a"}.symbolize_keys
# => {1 => 1, nil => nil, a: "a"}
# => {1=>1, nil=>nil, :a=>"a"}
```
WARNING. Note in the previous example only one key was symbolized.
......@@ -2785,7 +2785,7 @@ The result in case of collision is undefined:
```ruby
{"a" => 1, a: 2}.symbolize_keys
# => {a: 2}, in my test, can't rely on this result though
# => {:a=>2}, in my test, can't rely on this result though
```
This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionController::UrlRewriter` defines
......@@ -2836,17 +2836,17 @@ Ruby has built-in support for taking slices out of strings and arrays. Active Su
```ruby
{a: 1, b: 2, c: 3}.slice(:a, :c)
# => {c: 3, a: 1}
# => {:c=>3, :a=>1}
{a: 1, b: 2, c: 3}.slice(:b, :X)
# => {b: 2} # non-existing keys are ignored
# => {:b=>2} # non-existing keys are ignored
```
If the receiver responds to `convert_key` keys are normalized:
```ruby
{a: 1, b: 2}.with_indifferent_access.slice("a")
# => {a: 1}
# => {:a=>1}
```
NOTE. Slicing may come in handy for sanitizing option hashes with a white list of keys.
......@@ -2855,8 +2855,8 @@ There's also `slice!` which in addition to perform a slice in place returns what
```ruby
hash = {a: 1, b: 2}
rest = hash.slice!(:a) # => {b: 2}
hash # => {a: 1}
rest = hash.slice!(:a) # => {:b=>2}
hash # => {:a=>1}
```
NOTE: Defined in `active_support/core_ext/hash/slice.rb`.
......@@ -2867,8 +2867,8 @@ The method `extract!` removes and returns the key/value pairs matching the given
```ruby
hash = {a: 1, b: 2}
rest = hash.extract!(:a) # => {a: 1}
hash # => {b: 2}
rest = hash.extract!(:a) # => {:a=>1}
hash # => {:b=>2}
```
The method `extract!` returns the same subclass of Hash, that the receiver is.
......
......@@ -434,7 +434,7 @@ ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*a
event.name # => "process_action.action_controller"
event.duration # => 10 (in milliseconds)
event.payload # => { extra: :information }
event.payload # => {:extra=>information}
Rails.logger.info "#{event} Received!"
end
......@@ -477,7 +477,7 @@ Now you can listen to this event with:
```ruby
ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data|
puts data.inspect # { this: :data }
puts data.inspect # {:this=>:data}
end
```
......
......@@ -546,7 +546,7 @@ Also, a key can translate to a (potentially nested) hash of grouped translations
```ruby
I18n.t 'activerecord.errors.messages'
# => { inclusion: "is not included in the list", exclusion: ... }
# => {:inclusion=>"is not included in the list", :exclusion=> ... }
```
#### "Lazy" Lookup
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册