From ea2de48ec543fb341be31e9ed312b72ede860aee Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 21 Feb 2014 20:51:30 -0700 Subject: inplement query method to find matching status strategy. --- app/models/license_status.rb | 4 ++-- app/models/license_status/active.rb | 4 ++++ app/models/license_status/confidential.rb | 4 ++++ app/models/license_status/expired.rb | 4 ++++ spec/models/license_status_spec.rb | 29 +++++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 spec/models/license_status_spec.rb diff --git a/app/models/license_status.rb b/app/models/license_status.rb index 998792f..41b54c5 100644 --- a/app/models/license_status.rb +++ b/app/models/license_status.rb @@ -5,7 +5,7 @@ class LicenseStatus ALL=[ACTIVE, EXPIRED, CONFIDENTIAL] - def self.find_match(status) - ACTIVE + def self.find_match(status = "") + ALL.find { |x| x.matches?("#{status}".downcase) } || ACTIVE end end diff --git a/app/models/license_status/active.rb b/app/models/license_status/active.rb index 80d950d..76fb4c0 100644 --- a/app/models/license_status/active.rb +++ b/app/models/license_status/active.rb @@ -3,5 +3,9 @@ class LicenseStatus::Active today = DateTime.now licenses.where('issued_at < ? AND expired_at > ?', today, today) end + + def matches?(name) + "active" == name + end end diff --git a/app/models/license_status/confidential.rb b/app/models/license_status/confidential.rb index f3e980a..81178f6 100644 --- a/app/models/license_status/confidential.rb +++ b/app/models/license_status/confidential.rb @@ -2,4 +2,8 @@ class LicenseStatus::Confidential def filter(licenses) licenses.where(confidential: true) end + + def matches?(name) + "confidential" == name + end end diff --git a/app/models/license_status/expired.rb b/app/models/license_status/expired.rb index 1b339c6..393e34f 100644 --- a/app/models/license_status/expired.rb +++ b/app/models/license_status/expired.rb @@ -3,4 +3,8 @@ class LicenseStatus::Expired today = DateTime.now licenses.where('expired_at < ?', today) end + + def matches?(name) + "expired" == name + end end diff --git a/spec/models/license_status_spec.rb b/spec/models/license_status_spec.rb new file mode 100644 index 0000000..09e2319 --- /dev/null +++ b/spec/models/license_status_spec.rb @@ -0,0 +1,29 @@ +require "spec_helper" + +describe LicenseStatus do + describe ".find_match" do + it "returns the active status" do + LicenseStatus.find_match("active").should == LicenseStatus::ACTIVE + LicenseStatus.find_match("ACTIVE").should == LicenseStatus::ACTIVE + LicenseStatus.find_match("Active").should == LicenseStatus::ACTIVE + end + + it "returns the expired status" do + LicenseStatus.find_match("expired").should == LicenseStatus::EXPIRED + LicenseStatus.find_match("EXPIRED").should == LicenseStatus::EXPIRED + LicenseStatus.find_match("Expired").should == LicenseStatus::EXPIRED + end + + it "returns the confidential status" do + LicenseStatus.find_match("confidential").should == LicenseStatus::CONFIDENTIAL + LicenseStatus.find_match("CONFIDENTIAL").should == LicenseStatus::CONFIDENTIAL + LicenseStatus.find_match("Confidential").should == LicenseStatus::CONFIDENTIAL + end + + it "returns the active status as the default" do + [nil, "", "oh hai"].each do |key| + LicenseStatus.find_match(key).should == LicenseStatus::ACTIVE + end + end + end +end -- cgit v1.2.3