Skip to content

Commit

Permalink
Coerce dates to ruby Time.
Browse files Browse the repository at this point in the history
  • Loading branch information
amclain committed May 10, 2020
1 parent 28e7086 commit d80caed
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 20 deletions.
5 changes: 5 additions & 0 deletions lib/halation/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ def self.boolean(value)
!!value
end

# @return [Date, nil]
def self.date(value)
value && Time.parse(value)
end

end
end
2 changes: 1 addition & 1 deletion lib/halation/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run
exif["Artist"] = @roll.artist || @config.artist
exif["Copyright"] = @roll.copyright || @config.copyright

date_created = Time.new(frame.date || @roll.date)
date_created = frame.date || @roll.date
exif["DateTimeOriginal"] = date_created
exif["CreateDate"] = date_created

Expand Down
4 changes: 2 additions & 2 deletions lib/halation/roll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Roll
attr_reader :artist
# Copyright for the roll of film.
attr_reader :copyright
# Default date for all frames (optional).
# Default date for all frames in ISO 8601 format (optional).
attr_reader :date
# Tag of the cameara used.
attr_reader :camera
Expand Down Expand Up @@ -42,7 +42,7 @@ def load_file(file_path)
YAML.load_file(file_path).tap do |roll|
@artist = Coerce.string(roll["artist"])
@copyright = Coerce.string(roll["copyright"])
@date = Coerce.string(roll["date"])
@date = Coerce.date(roll["date"])
@camera = Coerce.string(roll["camera"])
@lens = Coerce.string(roll["lens"])
@iso = Coerce.integer(roll["iso"])
Expand Down
4 changes: 2 additions & 2 deletions lib/halation/roll/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Roll
class Frame
# Frame number.
attr_reader :number
# Date the frame was taken.
# Date the frame was captured in ISO 8601 format.
attr_reader :date
# Tag of the lens used.
attr_reader :lens
Expand All @@ -33,7 +33,7 @@ class Frame

def initialize(yaml)
@number = Coerce.integer(yaml["number"])
@date = Coerce.string(yaml["date"])
@date = Coerce.date(yaml["date"])
@lens = Coerce.string(yaml["lens"])
@focal_length = Coerce.integer(yaml["focal_length"])
@shutter = Coerce.string(yaml["shutter"])
Expand Down
14 changes: 14 additions & 0 deletions spec/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@
Coerce.integer(nil).should be nil
end
end

describe "a date" do
specify "from a string without a timestamp" do
Coerce.date("2016-02-03").should eq Time.parse("2016-02-03")
end

specify "from a string with a timestamp" do
Coerce.date("2016-02-03 01:02:03").should eq Time.parse("2016-02-03 01:02:03")
end

specify "from nil" do
Coerce.date(nil).should be nil
end
end
end
20 changes: 10 additions & 10 deletions spec/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
let(:exif_results) {[
{
# Frame 1
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-02-03 01:02:03"),
"LensModel" => "Z180mm f/4.5W-N",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -52,7 +52,7 @@
},
{
# Frame 2
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z100-200mm f/5.2W",
"ExposureTime" => 0.5,
"FNumber" => 8,
Expand All @@ -62,7 +62,7 @@
},
{
# Frame 3
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -72,7 +72,7 @@
},
{
# Frame 4
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -82,7 +82,7 @@
},
{
# Frame 5
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -92,7 +92,7 @@
},
{
# Frame 6
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -102,7 +102,7 @@
},
{
# Frame 7
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -112,7 +112,7 @@
},
{
# Frame 8
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -122,7 +122,7 @@
},
{
# Frame 9
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand All @@ -132,7 +132,7 @@
},
{
# Frame 10
"DateTimeOriginal" => Time.new("2016-01-01"),
"DateTimeOriginal" => Time.parse("2016-01-02"),
"LensModel" => "Z110mm f/2.8W",
"ExposureTime" => 1.0/250,
"FNumber" => 8,
Expand Down
2 changes: 1 addition & 1 deletion spec/roll/frame_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

shared_examples :frame do
its(:number) { should eq number.to_i }
its(:date) { should eq date.to_s }
its(:date) { should eq Time.parse(date) }
its(:lens) { should eq lens.to_s }
its(:focal_length) { should eq focal_length && focal_length.to_i }
its(:shutter) { should eq shutter.to_s }
Expand Down
4 changes: 2 additions & 2 deletions spec/roll_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
it "has values" do
subject.artist.should eq "Me"
subject.copyright.should eq "2016 Me"
subject.date.should eq "2016-01-01"
subject.date.should eq Time.parse("2016-01-02")
subject.camera.should eq "rz67"
subject.lens.should eq "110"
subject.iso.should eq 100
Expand All @@ -39,7 +39,7 @@
case i
when 0
frame.number.should eq 1
frame.date.should eq "2016-01-02"
frame.date.should eq Time.parse("2016-02-03 01:02:03")
frame.lens.should eq "180"
frame.focal_length.should be nil
frame.shutter.should eq "1/250"
Expand Down
4 changes: 2 additions & 2 deletions spec/samples/set_1/roll.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
artist: "Me"
copyright: "2016 Me"
date: "2016-01-01" # default
date: "2016-01-02" # default
camera: "rz67"
lens: 110 # default
iso: 100
frames:
- number: 1
date: "2016-01-02" # overridden
date: "2016-02-03 01:02:03" # overridden
lens: 180 # overridden
shutter: "1/250"
aperture: 8
Expand Down

0 comments on commit d80caed

Please sign in to comment.