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

Add default values for elements #51

Merged
merged 1 commit into from
Aug 1, 2014
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class AtomEntry
# The :as argument makes this available through entry.author instead of .name
element :name, as: :author
element "feedburner:origLink", as: :url
element :summary
# The :default argument specify value for element
# if it will not be found in XML
element :summary, class: Integer, default: 0
element :content, class: AtomContent
element :published
ancestor :ancestor
Expand Down Expand Up @@ -104,6 +106,7 @@ feed.feed_url # The URL of the blog feed
feed.entries.first.title # Title of the first entry
feed.entries.first.author # The author of the first entry
feed.entries.first.url # Permalink on the blog for this entry
feed.entries.first.summary # Returns 0 as value if not found
feed.entries.first.ancestor # The Atom ancestor
feed.entries.first.content # Instance of AtomContent
feed.entries.first.content.text # Entry content text
Expand Down
3 changes: 2 additions & 1 deletion lib/sax-machine/config/sax_element.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SAXMachine
class SAXConfig
class ElementConfig
attr_reader :name, :setter, :data_class, :collection
attr_reader :name, :as, :setter, :data_class, :collection, :default

def initialize(name, options)
@name = name.to_s
Expand All @@ -15,6 +15,7 @@ def initialize(name, options)

@as = options[:as]
@collection = options[:collection]
@default = options[:default]

@setter = if @collection
"add_#{options[:as]}"
Expand Down
11 changes: 11 additions & 0 deletions lib/sax-machine/sax_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end

self.class.sax_config.top_level_elements.each do |name, configs|
configs.each do |config|
next unless config.default

current_value = send("#{config.as.to_s}")
next unless current_value.nil?

send("#{config.setter}", config.default)
end
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/sax-machine/sax_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ def title=(val)
end
end

describe "the default attribute" do
it "is available" do
@klass = Class.new do
include SAXMachine
element :number, default: ""
end

document = @klass.parse("<wrong>data</wrong>")
expect(document.number).to eq("")
end
end

describe "the required attribute" do
it "is available" do
@klass = Class.new do
Expand Down