aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/model_attribute.rb4
-rw-r--r--spec/lib/model_attribute_spec.rb24
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/model_attribute.rb b/lib/model_attribute.rb
index 53c0b7a46..78c9168eb 100644
--- a/lib/model_attribute.rb
+++ b/lib/model_attribute.rb
@@ -17,6 +17,10 @@ class ModelAttribute
.uniq
end
+ def self.group_by_class
+ all.group_by(&:klass)
+ end
+
def self.methods_by_class(klass)
all.select do |model_attr|
model_attr.klass == klass
diff --git a/spec/lib/model_attribute_spec.rb b/spec/lib/model_attribute_spec.rb
index 9e10bf40f..0ca90430f 100644
--- a/spec/lib/model_attribute_spec.rb
+++ b/spec/lib/model_attribute_spec.rb
@@ -30,6 +30,30 @@ RSpec.describe ModelAttribute do
end
end
+ describe ".group_by_class" do
+ it "returns all ModelAttributes grouped by klass" do
+ ModelAttribute.instance_variable_set(:@__all__, [
+ ModelAttribute.new(:route, :name, :string),
+ ModelAttribute.new(:route, :published_name, :string),
+ ModelAttribute.new(:journey_pattern, :name, :string),
+ ModelAttribute.new(:vehicle_journey, :number, :integer)
+ ])
+
+ expect(ModelAttribute.group_by_class).to eq({
+ route: [
+ ModelAttribute.new(:route, :name, :string),
+ ModelAttribute.new(:route, :published_name, :string),
+ ],
+ journey_pattern: [
+ ModelAttribute.new(:journey_pattern, :name, :string),
+ ],
+ vehicle_journey: [
+ ModelAttribute.new(:vehicle_journey, :number, :integer)
+ ]
+ })
+ end
+ end
+
describe ".methods_by_class" do
it "returns all ModelAttributes for a given class" do
ModelAttribute.instance_variable_set(:@__all__, [