aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-09-22 12:11:41 +0200
committerTeddy Wing2017-09-22 12:11:41 +0200
commit9179af1df905ff9980228d2aa0ba77418f2781aa (patch)
tree4cc86f11fb885b86671bf7d222e7c7b86835dc69
parent198659b133aba39cdd92d37fd3b93a223159994b (diff)
downloadchouette-core-9179af1df905ff9980228d2aa0ba77418f2781aa.tar.bz2
ModelAttribute: Add `.methods_by_class_and_type` method
This enables filtering by class like `.methods_by_class` and additionally only selecting attributes matching a certain data type. Refs #4401
-rw-r--r--lib/model_attribute.rb6
-rw-r--r--spec/lib/model_attribute_spec.rb15
2 files changed, 21 insertions, 0 deletions
diff --git a/lib/model_attribute.rb b/lib/model_attribute.rb
index 84706ba4d..e74dd1060 100644
--- a/lib/model_attribute.rb
+++ b/lib/model_attribute.rb
@@ -15,6 +15,12 @@ class ModelAttribute
end
end
+ def self.methods_by_class_and_type(klass, type)
+ methods_by_class(klass).select do |model_attr|
+ model_attr.data_type == type
+ end
+ end
+
def initialize(klass, name, data_type)
@klass = klass
@name = name
diff --git a/spec/lib/model_attribute_spec.rb b/spec/lib/model_attribute_spec.rb
index 2da178d2c..faea0b483 100644
--- a/spec/lib/model_attribute_spec.rb
+++ b/spec/lib/model_attribute_spec.rb
@@ -31,6 +31,21 @@ RSpec.describe ModelAttribute do
end
end
+ describe ".methods_by_class_and_type" do
+ it "returns ModelAttributes of a certain class and type" do
+ ModelAttribute.instance_variable_set(:@__all__, [
+ ModelAttribute.new(:route, :name, :string),
+ ModelAttribute.new(:route, :checked_at, :date),
+ ModelAttribute.new(:journey_pattern, :name, :string),
+ ModelAttribute.new(:journey_pattern, :section_status, :integer)
+ ])
+
+ expect(ModelAttribute.methods_by_class_and_type(:route, :string)).to match_array([
+ ModelAttribute.new(:route, :name, :string)
+ ])
+ end
+ end
+
describe "#code" do
it "returns a string representation of the attribute" do
model_attr = ModelAttribute.new(:route, :name, :string)