Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data completeness subview class #15432

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/data_completeness.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
# The completeness of each type of member data as a percentage
class DataCompleteness
# @param [Array<PersonCard>]
def initialize(person_cards:)
@person_cards = person_cards
end

CARDS = %i(social bio contacts identifiers).freeze
Percentages = Struct.new(*CARDS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Percentages class isn't documented…

# The percentage of data completeness for categories defined in CARDS
# @return [struct DataCompleteness::Percentages]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see somewhere that you should be including this struct here? I can't see anything in http://www.rubydoc.info/gems/yard/file/docs/Tags.md about this, though I could easily be overlooking something

def percentages
Percentages.new(*CARDS.map { |card| completeness(card) })
end

private

attr_reader :person_cards

def completeness(card)
((person_cards.count { |p| p.send(card.to_s).any? } / person_cards.count.to_f) * 100).floor
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a query method, but it's named as if it's a command method. (See, for example, http://martinfowler.com/bliki/CommandQuerySeparation.html)

Methods that are returning you something should simply be named for what they return, not what they do to return that — so here just calling this completeness would be better.

end
6 changes: 2 additions & 4 deletions lib/page/term_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'everypolitician'
require_relative '../everypolitician_extensions'
require_relative '../person_cards'
require_relative '../data_completeness'

module Page
class TermTable
Expand Down Expand Up @@ -66,11 +67,8 @@ def people
end
end

CARDS = %i(social bio contacts identifiers).freeze
Percentages = Struct.new(*CARDS)
def percentages
pc = ->(card) { ((people.count { |p| p.send(card.to_s).any? } / people.count.to_f) * 100).floor }
Percentages.new(*CARDS.map { |card| pc.call(card) })
@pc ||= DataCompleteness.new(person_cards: people).percentages
end

private
Expand Down