Skip to content

Commit

Permalink
lib: factor out the attributes-setting method (#20828)
Browse files Browse the repository at this point in the history
  • Loading branch information
freesteph authored Oct 23, 2024
1 parent 6845910 commit 3b8f236
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
27 changes: 27 additions & 0 deletions lib/models/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'active_model'
require 'yaml'

module Beta
Expand All @@ -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
Expand All @@ -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
12 changes: 3 additions & 9 deletions lib/models/member.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 1 addition & 11 deletions lib/models/startup.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/pad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 :

Expand Down

0 comments on commit 3b8f236

Please sign in to comment.