aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-09-22 12:25:36 +0200
committerTeddy Wing2017-09-22 12:25:36 +0200
commit404b143af9ed49912e1e0065c832639188aa87de (patch)
tree1cb3060bb7bb7d7531818946d423897c0ceff701
parent9179af1df905ff9980228d2aa0ba77418f2781aa (diff)
downloadchouette-core-404b143af9ed49912e1e0065c832639188aa87de.tar.bz2
ModelAttribute: Add `.classes` method
This allows people to get a list of the classes defined in `ModelAttribute`. The classes are turned into constant-cased strings. Not sure if that's useful, but this is how it was described in the ticket. Refs #4401
-rw-r--r--lib/model_attribute.rb8
-rw-r--r--spec/lib/model_attribute_spec.rb16
2 files changed, 24 insertions, 0 deletions
diff --git a/lib/model_attribute.rb b/lib/model_attribute.rb
index e74dd1060..53c0b7a46 100644
--- a/lib/model_attribute.rb
+++ b/lib/model_attribute.rb
@@ -9,6 +9,14 @@ class ModelAttribute
all << new(klass, name, data_type)
end
+ def self.classes
+ all
+ .map(&:klass)
+ .map(&:to_s)
+ .map(&:camelize)
+ .uniq
+ 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 faea0b483..9e10bf40f 100644
--- a/spec/lib/model_attribute_spec.rb
+++ b/spec/lib/model_attribute_spec.rb
@@ -14,6 +14,22 @@ RSpec.describe ModelAttribute do
end
end
+ describe ".classes" do
+ it "returns the list of classes of ModelAttributes in .all" do
+ ModelAttribute.instance_variable_set(:@__all__, [
+ ModelAttribute.new(:route, :name, :string),
+ ModelAttribute.new(:journey_pattern, :name, :string),
+ ModelAttribute.new(:time_table, :start_date, :date)
+ ])
+
+ expect(ModelAttribute.classes).to match_array([
+ 'Route',
+ 'JourneyPattern',
+ 'TimeTable'
+ ])
+ end
+ end
+
describe ".methods_by_class" do
it "returns all ModelAttributes for a given class" do
ModelAttribute.instance_variable_set(:@__all__, [