Skip to content

Commit

Permalink
Merge pull request #51 from kirillplatonov/master
Browse files Browse the repository at this point in the history
Add default values for elements
  • Loading branch information
krasnoukhov committed Aug 1, 2014
2 parents d250349 + 8fce585 commit 72dc8ef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
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

0 comments on commit 72dc8ef

Please sign in to comment.