Skip to content

Commit

Permalink
Added Literal#humanize defaulting to #to_s. Special implementations f…
Browse files Browse the repository at this point in the history
…or Date, Time, and DateTime.
  • Loading branch information
gkellogg committed Sep 2, 2014
1 parent 3cbd5ad commit c0a92d5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rdf/model/literal.rb
Expand Up @@ -401,6 +401,15 @@ def to_s
@object.to_s
end

##
# Returns a human-readable value for the literal
#
# @return [String]
# @since 1.1.6
def humanize(lang = :en)
to_s
end

##
# Returns a developer-friendly representation of `self`.
#
Expand Down
17 changes: 17 additions & 0 deletions lib/rdf/model/literal/date.rb
Expand Up @@ -66,6 +66,23 @@ def to_s
@string || @object.strftime(FORMAT)
end

##
# Returns a human-readable value for the literal
#
# @return [String]
# @since 1.1.6
def humanize(lang = :en)
d = object.strftime("%A, %d %B %Y")
if has_timezone?
d += if self.tz == 'Z'
" UTC"
else
" #{self.tz}"
end
end
d
end

##
# Returns the timezone part of arg as a simple literal. Returns the empty string if there is no timezone.
#
Expand Down
18 changes: 18 additions & 0 deletions lib/rdf/model/literal/datetime.rb
Expand Up @@ -102,6 +102,24 @@ def to_s
@string || @object.strftime(FORMAT).sub("+00:00", 'Z')
end

##
# Returns a human-readable value for the literal
#
# @return [String]
# @since 1.1.6
def humanize(lang = :en)
d = object.strftime("%r on %A, %d %B %Y")
if has_timezone?
zone = if self.tz == 'Z'
"UTC"
else
self.tz
end
d.sub!(" on ", " #{zone} on ")
end
d
end

##
# Equal compares as DateTime objects
def ==(other)
Expand Down
17 changes: 17 additions & 0 deletions lib/rdf/model/literal/time.rb
Expand Up @@ -95,6 +95,23 @@ def to_s
@string || @object.strftime('%H:%M:%S%:z').sub("+00:00", 'Z')
end

##
# Returns a human-readable value for the literal
#
# @return [String]
# @since 1.1.6
def humanize(lang = :en)
t = object.strftime("%r")
if has_timezone?
t += if self.tz == 'Z'
" UTC"
else
" #{self.tz}"
end
end
t
end

##
# Equal compares as Time objects
def ==(other)
Expand Down
38 changes: 38 additions & 0 deletions spec/model_literal_spec.rb
Expand Up @@ -137,6 +137,10 @@ def self.literals(*selector)
expect(RDF::Literal.new(value, datatype: RDF::XSD.decimal, canonicalize: true).to_s).to eq str
end

it "humanizes decimal '#{value}' to '#{str}'" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.decimal, canonicalize: false).humanize).to eq value
end

it "instantiates '#{value}' as RDF::Literal::Decimal" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.decimal, canonicalize: true)).to be_a(RDF::Literal::Decimal)
end
Expand Down Expand Up @@ -167,6 +171,10 @@ def self.literals(*selector)
expect(RDF::Literal.new(value, datatype: RDF::XSD.double, canonicalize: true).to_s).to eq str
end

it "humanizes double '#{value}' to '#{str}'" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.double, canonicalize: false).humanize).to eq value
end

it "instantiates '#{value}' as RDF::Literal::Double" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.double, canonicalize: true)).to be_a(RDF::Literal::Double)
end
Expand Down Expand Up @@ -213,6 +221,16 @@ def self.literals(*selector)
end
end

{
"2014-09-01T12:13:14.567" => "12:13:14 PM on Monday, 01 September 2014",
"2014-09-01T12:13:14.567Z" => "12:13:14 PM UTC on Monday, 01 September 2014",
"2014-09-01T12:13:14.567-08:00" => "12:13:14 PM -08:00 on Monday, 01 September 2014",
}.each_pair do |value, str|
it "humanizes dateTime '#{value}' to '#{str}'" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.dateTime, canonicalize: false).humanize).to eq str
end
end

# Date
{
"2010-01-01Z" => "2010-01-01Z",
Expand Down Expand Up @@ -240,6 +258,16 @@ def self.literals(*selector)
end
end

{
"2014-09-01" => "Monday, 01 September 2014",
"2014-09-01Z" => "Monday, 01 September 2014 UTC",
"2014-09-01-08:00" => "Monday, 01 September 2014 -08:00",
}.each_pair do |value, str|
it "humanizes date '#{value}' to '#{str}'" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.date, canonicalize: false).humanize).to eq str
end
end

# Time
{
"00:00:00Z" => "00:00:00Z",
Expand All @@ -266,6 +294,16 @@ def self.literals(*selector)
expect(RDF::Literal.new(value, datatype: RDF::XSD.time, canonicalize: true)).to eq RDF::Literal.new(str, datatype: RDF::XSD.time, canonicalize: false)
end
end

{
"12:13:14.567" => "12:13:14 PM",
"12:13:14.567Z" => "12:13:14 PM UTC",
"12:13:14.567-08:00" => "12:13:14 PM -08:00",
}.each_pair do |value, str|
it "humanizes time '#{value}' to '#{str}'" do
expect(RDF::Literal.new(value, datatype: RDF::XSD.time, canonicalize: false).humanize).to eq str
end
end
end
end

Expand Down

0 comments on commit c0a92d5

Please sign in to comment.