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 meta methods: dupe_at_threshold_<n>? #63

Merged
merged 2 commits into from
May 19, 2017
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
4 changes: 4 additions & 0 deletions lib/phashion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def duplicate?(other, opts={})
distance_from(other) <= threshold
end

(0..64).each do |n|
define_method("dupe_at_threshold_#{n}?") { |oth| distance_from(oth) <= n }
end

def fingerprint
@hash ||= Phashion.image_hash_for(@filename)
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_phashion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ def test_duplicate_with_custom_distance_threshold
assert(jpg.duplicate?(jpg_x, threshold: 2))
end

def test_duplicate_meta_methods
# note: this test depends on the smaller_jpg test still asserting a distance of 2
# note-2: threshold is a :less-than-or-equal-to comparison, which is a change from version 1.0.8
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.100px.jpg')

refute(jpg.dupe_at_threshold_1?(jpg_x))
assert(jpg.dupe_at_threshold_2?(jpg_x))
assert_raises(NoMethodError) { jpg.dupe_at_threshold_100?(jpg_x) }
end


### distance methods
def test_distance_from_jpg_to_png_dupe
Expand Down