aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/model_attribute.rb12
-rw-r--r--spec/lib/model_attribute_spec.rb26
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/model_attribute.rb b/lib/model_attribute.rb
index 98bb54b67..8f1ecabed 100644
--- a/lib/model_attribute.rb
+++ b/lib/model_attribute.rb
@@ -9,6 +9,12 @@ class ModelAttribute
all << new(klass, name, data_type)
end
+ def self.methods_by_class(klass)
+ all.select do |model_attr|
+ model_attr.klass == klass.downcase.to_sym
+ end
+ end
+
def initialize(klass, name, data_type)
@klass = klass
@name = name
@@ -59,4 +65,10 @@ class ModelAttribute
def code
"#{@klass}##{@name}"
end
+
+ def ==(other)
+ klass == other.klass &&
+ name == other.name &&
+ data_type == other.data_type
+ end
end
diff --git a/spec/lib/model_attribute_spec.rb b/spec/lib/model_attribute_spec.rb
index ad1dd863f..73114ae1e 100644
--- a/spec/lib/model_attribute_spec.rb
+++ b/spec/lib/model_attribute_spec.rb
@@ -14,6 +14,23 @@ RSpec.describe ModelAttribute do
end
end
+ describe ".methods_by_class" do
+ it "returns all ModelAttributes for a given class" do
+ ModelAttribute.instance_variable_set(:@__all__, [
+ ModelAttribute.new(:route, :name, :string),
+ ModelAttribute.new(:route, :published_name, :string),
+ ModelAttribute.new(:route, :direction, :string),
+ ModelAttribute.new(:journey_pattern, :name, :string)
+ ])
+
+ expect(ModelAttribute.methods_by_class('Route')).to match_array([
+ ModelAttribute.new(:route, :name, :string),
+ ModelAttribute.new(:route, :published_name, :string),
+ ModelAttribute.new(:route, :direction, :string)
+ ])
+ end
+ end
+
describe "#code" do
it "returns a string representation of the attribute" do
model_attr = ModelAttribute.new(:route, :name, :string)
@@ -21,4 +38,13 @@ RSpec.describe ModelAttribute do
expect(model_attr.code).to eq('route#name')
end
end
+
+ describe "#==" do
+ it "returns true when :klass, :name, and :data_type attributes match" do
+ route_name = ModelAttribute.new(:route, :name, :string)
+ other_route_name = ModelAttribute.new(:route, :name, :string)
+
+ expect(route_name == other_route_name).to be true
+ end
+ end
end