Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting and deleting of attributes on supported XML::Node types #3902

Merged
merged 2 commits into from
Mar 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/xml/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@ struct XML::Node
end

# Gets the attribute content for the *attribute* given by name.
#
# Raises `KeyError` if attribute is not found.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for removal?

def [](attribute : String) : String
attributes[attribute].content || raise(KeyError.new("Missing attribute: #{attribute}"))
end

# Gets the attribute content for the *attribute* given by name.
#
# Returns `nil` if attribute is not found.
def []?(attribute : String) : String?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

attributes[attribute]?.try &.content
end

# Sets *attribute* of this node to *value*.
# Raises `XML::Error` if this node does not support attributes.
def []=(name : String, value : String)
raise XML::Error.new("Can't set attribute of #{type}", 0) unless element?
attributes[name] = value
end

# Deletes attribute given by *name*.
# Returns attributes value, or `nil` if attribute not found.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra newline in between would do good. See previous comments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sija there is no consistency regarding if the docs for the returned / raised should be in a new paragraph. Applying your proposed changes will left xml/node docs inconsistent.

Let's merge this as it is and unify formatting later. I agree a new line looks better in the html rendering but in an .cr file I would probably forgot to include it.

def delete(name : String)
attributes.delete(name)
end

# Compares with *other*.
def ==(other : Node)
@node == other.@node
Expand Down