-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Rosetta | ||
class PluralizationRule < ApplicationRecord | ||
OPERATORS = { | ||
less_than_or_equal_to: Pluralization::Operators::LessThanOrEqualTo, | ||
less_than: Pluralization::Operators::LessThan, | ||
equal_to: Pluralization::Operators::EqualTo, | ||
greater_than_or_equal_to: Pluralization::Operators::GreaterThanOrEqualTo, | ||
greater_than: Pluralization::Operators::GreaterThan | ||
}.with_indifferent_access | ||
|
||
belongs_to :locale | ||
|
||
validates :name, :operator, :threshold, presence: true | ||
validates :threshold, numericality: { greater_than_or_equal_to: 0 } | ||
validates :operator, inclusion: { in: OPERATORS.keys } | ||
|
||
def activated?(test_value) | ||
OPERATORS[operator].new(value: test_value, threshold: threshold).check | ||
end | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20240920134441_create_rosetta_pluralization_rules.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateRosettaPluralizationRules < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :rosetta_pluralization_rules do |t| | ||
t.string :name | ||
t.string :operator | ||
t.integer :threshold | ||
t.references :locale, null: false | ||
|
||
t.timestamps | ||
|
||
t.foreign_key :rosetta_locales, column: :locale_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Rosetta | ||
module Pluralization | ||
class BaseOperator | ||
def initialize(value:, threshold:) | ||
@value = value | ||
@threshold = threshold | ||
end | ||
|
||
def check | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rosetta | ||
module Pluralization | ||
module Operators | ||
class EqualTo < BaseOperator | ||
def check | ||
@value == @threshold | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rosetta | ||
module Pluralization | ||
module Operators | ||
class GreaterThan < BaseOperator | ||
def check | ||
@value > @threshold | ||
end | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
lib/rosetta/pluralization/operators/greater_than_or_equal_to.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rosetta | ||
module Pluralization | ||
module Operators | ||
class GreaterThanOrEqualTo < BaseOperator | ||
def check | ||
@value >= @threshold | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rosetta | ||
module Pluralization | ||
module Operators | ||
class LessThan < BaseOperator | ||
def check | ||
@value < @threshold | ||
end | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
lib/rosetta/pluralization/operators/less_than_or_equal_to.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rosetta | ||
module Pluralization | ||
module Operators | ||
class LessThanOrEqualTo < BaseOperator | ||
def check | ||
@value <= @threshold | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
french_less_than_or_equal_to_one: | ||
name: Less than or equal to one | ||
operator: less_than_or_equal_to | ||
threshold: 1 | ||
locale: french |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module PluralizationOperatorHelpers | ||
def operator_class | ||
self.class.name.gsub(/Test\z/, "").constantize | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "test_helper" | ||
|
||
class Rosetta::Pluralization::Operators::EqualToTest < ActiveSupport::TestCase | ||
include PluralizationOperatorHelpers | ||
|
||
test "checks correct counts" do | ||
assert operator_class.new(value: 1, threshold: 1).check | ||
assert_not operator_class.new(value: 2, threshold: 1).check | ||
assert_not operator_class.new(value: 0, threshold: 1).check | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
test/lib/rosetta/pluralization/operators/greater_than_or_equal_to.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "test_helper" | ||
|
||
class Rosetta::Pluralization::Operators::GreaterThanOrEqualToTest < ActiveSupport::TestCase | ||
include PluralizationOperatorHelpers | ||
|
||
test "checks correct counts" do | ||
assert operator_class.new(value: 1, threshold: 1).check | ||
assert operator_class.new(value: 2, threshold: 1).check | ||
assert_not operator_class.new(value: 0, threshold: 1).check | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
test/lib/rosetta/pluralization/operators/greater_than_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "test_helper" | ||
|
||
class Rosetta::Pluralization::Operators::GreaterThanTest < ActiveSupport::TestCase | ||
include PluralizationOperatorHelpers | ||
|
||
test "checks correct counts" do | ||
assert operator_class.new(value: 2, threshold: 1).check | ||
assert_not operator_class.new(value: 1, threshold: 1).check | ||
assert_not operator_class.new(value: 0, threshold: 1).check | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
test/lib/rosetta/pluralization/operators/less_than_or_equal_to_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "test_helper" | ||
|
||
class Rosetta::Pluralization::Operators::LessThanOrEqualToTest < ActiveSupport::TestCase | ||
include PluralizationOperatorHelpers | ||
|
||
test "checks correct counts" do | ||
assert operator_class.new(value: 2, threshold: 2).check | ||
assert operator_class.new(value: 1, threshold: 2).check | ||
assert_not operator_class.new(value: 5, threshold: 2).check | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
test/lib/rosetta/pluralization/operators/less_than_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "test_helper" | ||
|
||
class Rosetta::Pluralization::Operators::LessThanTest < ActiveSupport::TestCase | ||
include PluralizationOperatorHelpers | ||
|
||
test "checks correct counts" do | ||
assert operator_class.new(value: 1, threshold: 2).check | ||
assert_not operator_class.new(value: 2, threshold: 2).check | ||
assert_not operator_class.new(value: 5, threshold: 2).check | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "test_helper" | ||
|
||
module Rosetta | ||
class PluralizationRuleTest < ActiveSupport::TestCase | ||
setup do | ||
@rule = rosetta_pluralization_rules(:french_less_than_or_equal_to_one) | ||
end | ||
|
||
test "valid" do | ||
assert @rule.valid? | ||
end | ||
|
||
%i[name operator threshold].each do |attribute| | ||
test "invalid without #{attribute}" do | ||
@rule.public_send("#{attribute}=", nil) | ||
assert_not @rule.valid? | ||
assert_includes @rule.errors[attribute], "can't be blank" | ||
end | ||
end | ||
|
||
test "operator must be included in the list" do | ||
@rule.operator = "unknown_operator" | ||
assert_not @rule.valid? | ||
assert_includes @rule.errors[:operator], "is not included in the list" | ||
end | ||
|
||
test "value must be positive" do | ||
@rule.threshold = -1 | ||
assert_not @rule.valid? | ||
assert_includes @rule.errors[:threshold], "must be greater than or equal to 0" | ||
end | ||
|
||
test "#activated?" do | ||
assert @rule.activated?(0) | ||
assert @rule.activated?(1) | ||
assert_not @rule.activated?(2) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters