set nil if parsing an empty string
This commit is contained in:
parent
94f9fe89c1
commit
6d63903e73
3 changed files with 35 additions and 4 deletions
|
|
@ -50,7 +50,7 @@ module DiasporaFederation
|
|||
end
|
||||
|
||||
self.class.default_values.merge(data).each do |k, v|
|
||||
instance_variable_set("@#{k}", v) if setable?(k, v)
|
||||
instance_variable_set("@#{k}", nilify(v)) if setable?(k, v)
|
||||
end
|
||||
freeze
|
||||
end
|
||||
|
|
@ -113,6 +113,11 @@ module DiasporaFederation
|
|||
val.all? {|v| v.instance_of?(t.first) })
|
||||
end
|
||||
|
||||
def nilify(value)
|
||||
return nil if value.respond_to?(:empty?) && value.empty?
|
||||
value
|
||||
end
|
||||
|
||||
# Serialize the Entity into XML elements
|
||||
# @return [Nokogiri::XML::Element] root node
|
||||
def entity_xml
|
||||
|
|
|
|||
|
|
@ -152,6 +152,25 @@ HTML
|
|||
expect(hcard.searchable).to eq(false)
|
||||
end
|
||||
|
||||
it "name is nil if empty" do
|
||||
changed_html = html.sub(
|
||||
"class=\"fn\">#{person.full_name}<",
|
||||
"class=\"fn\"><"
|
||||
).sub(
|
||||
"class=\"given_name\">#{person.first_name}<",
|
||||
"class=\"given_name\"><"
|
||||
).sub(
|
||||
"class=\"family_name\">#{person.last_name}<",
|
||||
"class=\"family_name\"><"
|
||||
)
|
||||
|
||||
hcard = Discovery::HCard.from_html(changed_html)
|
||||
|
||||
expect(hcard.full_name).to be_nil
|
||||
expect(hcard.first_name).to be_nil
|
||||
expect(hcard.last_name).to be_nil
|
||||
end
|
||||
|
||||
it "reads old-style HTML" do
|
||||
historic_html = <<-HTML
|
||||
<div id="content">
|
||||
|
|
|
|||
|
|
@ -20,17 +20,24 @@ module DiasporaFederation
|
|||
|
||||
it "sets the defaults" do
|
||||
entity = Entities::TestDefaultEntity.new(test1: 1, test2: 2)
|
||||
expect(entity.to_h[:test3]).to be_truthy
|
||||
expect(entity.test3).to be_truthy
|
||||
end
|
||||
|
||||
it "handles callable defaults" do
|
||||
entity = Entities::TestDefaultEntity.new(test1: 1, test2: 2)
|
||||
expect(entity.to_h[:test4]).to be_truthy
|
||||
expect(entity.test4).to be_truthy
|
||||
end
|
||||
|
||||
it "uses provided values over defaults" do
|
||||
entity = Entities::TestDefaultEntity.new(data)
|
||||
expect(entity.to_h[:test3]).to be_falsey
|
||||
expect(entity.test3).to be_falsey
|
||||
expect(entity.test4).to be_falsey
|
||||
end
|
||||
|
||||
it "sets nil if string is empty" do
|
||||
data[:test1] = ""
|
||||
entity = Entities::TestDefaultEntity.new(data)
|
||||
expect(entity.test1).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue