From 3b8f23671fa72800cfc93459bcc247707e142ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Wed, 23 Oct 2024 09:50:06 +0200 Subject: [PATCH] lib: factor out the attributes-setting method (#20828) --- lib/models/base.rb | 27 +++++++++++++++++++++++++++ lib/models/member.rb | 12 +++--------- lib/models/startup.rb | 12 +----------- lib/pad.rb | 2 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/models/base.rb b/lib/models/base.rb index 41b5b29e13e3..49fa6e87e9c4 100644 --- a/lib/models/base.rb +++ b/lib/models/base.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'active_model' require 'yaml' module Beta @@ -8,7 +9,27 @@ module Beta # instantiate all the files in the "FOLDER_IDENTIFIER" of the child # class. class Base + include ActiveModel::AttributeAssignment + class << self + # `interesting_attributes` is a list of attributes that are + # relevant in our files, for which we're willing to define + # accessors. Before this we used OpenStruct but OpenStruct + # declares a method for every single hash key-value pair it is + # instantiated with – which is quite heavy especially when we + # have to do it for 2K+ members/startups/etc. + attr_reader :interesting_attributes + + def interesting(*attrs) + @interesting_attributes ||= [] + + attrs.each do |attr| + attr_accessor attr + + @interesting_attributes << attr + end + end + def all @all ||= fetch_all end @@ -23,5 +44,11 @@ def fetch_all .map { |id, data| new(data.merge('id' => id)) } end end + + def initialize(hash) + attributes = self.class.interesting_attributes + + assign_attributes(hash.slice(*attributes.map(&:to_s))) + end end end diff --git a/lib/models/member.rb b/lib/models/member.rb index 70dfbf650d5d..726f5947fe05 100644 --- a/lib/models/member.rb +++ b/lib/models/member.rb @@ -1,25 +1,19 @@ # frozen_string_literal: true -require 'active_model' - require_relative 'base' module Beta class Member < Base - include ActiveModel::AttributeAssignment - - attr_accessor :fullname, :role, :domaine, :missions, :startups, :previously - FOLDER_IDENTIFIER = '_authors' + interesting :id, :fullname, :role, :domaine, :missions, :startups, :previously + def initialize(hash) - assign_attributes(hash.slice(*%w[fullname role domaine missions startups previously])) + super @missions ||= [] @startups ||= [] @previously ||= [] - - super() end def active_missions diff --git a/lib/models/startup.rb b/lib/models/startup.rb index fafacc3de79b..4db39e678bcc 100644 --- a/lib/models/startup.rb +++ b/lib/models/startup.rb @@ -1,23 +1,13 @@ # frozen_string_literal: true -require 'active_model' - require_relative 'base' module Beta # Startup represents all of our products in # `content/_startpus/*.md`. class Startup < Base - include ActiveModel::AttributeAssignment - - attr_accessor :id, :title, :phases, :mission - FOLDER_IDENTIFIER = '_startups' - def initialize(hash) - assign_attributes(hash.slice(*%w[id title phases mission])) - - super() - end + interesting :id, :title, :phases, :mission, :accessibility_status end end diff --git a/lib/pad.rb b/lib/pad.rb index 4212d1e112cc..bf5899e85b01 100644 --- a/lib/pad.rb +++ b/lib/pad.rb @@ -29,7 +29,7 @@ # 4. et on fait la différence -startup_without_coaches = all_startups - startups_with_coaches +startups_without_coaches = all_startups - startups_with_coaches # 5. le mieux pour explorer votre résultat :