Skip to content

Commit

Permalink
Move classes for reports page into separate file
Browse files Browse the repository at this point in the history
They were in the routes while I was extracting but they can't stay there
long term, so moving them to their own file.
  • Loading branch information
chrismytton committed Jul 1, 2016
1 parent 79908e2 commit be09f86
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 142 deletions.
144 changes: 2 additions & 142 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require 'app/jobs'
require 'csv_export'
require 'country_proxy'
require 'reports'

helpers Helpers

Expand Down Expand Up @@ -168,155 +169,14 @@
erb :term
end

# Require including class to define a `stats` method which returns a hash with
# :female, :male and :total keys.
module GenderStats
def female
@female ||= stats[:female].to_f
end

def male
@male ||= stats[:male].to_f
end

def total
@total ||= stats[:total].to_f
end

def female_percentage
@female_percentage ||= female / total * 100
end

def male_percentage
@male_percentage ||= male / total * 100
end
end

class LegislatureReport
extend Forwardable
include GenderStats

# Delegate 'name' and 'slug' method calls to the 'legislature' object.
def_delegators :legislature, :name, :slug

attr_reader :legislature
attr_reader :raw_stats

def initialize(legislature, raw_stats)
@legislature = legislature
@raw_stats = raw_stats
end

def stats
@stats ||= raw_stats[:totals][:overall]
end

def groups
@groups ||= raw_stats[:totals][:parties].map { |slug, group_stats| GroupReport.new(slug, group_stats, raw_stats, legislative_periods) }
end

def legislative_periods
@legislative_periods ||= legislature.legislative_periods.map { |lp| LegislativePeriodReport.new(lp, raw_stats) }
end
end

class GroupReport
include GenderStats

attr_reader :slug
attr_reader :stats
attr_reader :raw_stats

def initialize(slug, stats, raw_stats, legislative_periods)
@slug = slug
@stats = stats
@raw_stats = raw_stats
@legislative_periods = legislative_periods
end

def name
stats[:name]
end

def id_slug
@id_slug ||= slug.to_s.sub('/', '-')
end

def legislative_periods
@legislative_periods.map do |term|
term_stats = raw_stats[:terms]["term/#{term.slug}".to_sym]
group_stats = term_stats[:parties][slug]
GroupLegilativePeriodReport.new(term, group_stats) if group_stats
end.compact
end
end

class GroupLegilativePeriodReport
include GenderStats

attr_reader :term
attr_reader :stats

def initialize(term, stats)
@term = term
@stats = stats
end

def name
term.name
end
end

class LegislativePeriodReport
extend Forwardable
include GenderStats

# Delegate 'name' and 'slug' method calls to the 'legislative_period' object.
def_delegators :legislative_period, :name, :slug

attr_reader :legislative_period
attr_reader :raw_stats

def initialize(legislative_period, raw_stats)
@legislative_period = legislative_period
@raw_stats = raw_stats
end

def legislative_period_stats
@legislative_period_stats ||= raw_stats[:terms]["term/#{legislative_period.slug}".to_sym]
end

def stats
@stats ||= legislative_period_stats[:overall]
end

def groups
@groups = legislative_period_stats[:parties].map { |_, group_stats| LegislativePeriodGroupReport.new(group_stats) }
end
end

class LegislativePeriodGroupReport
include GenderStats

attr_reader :stats

def initialize(stats)
@stats = stats
end

def name
stats[:name]
end
end

get '/reports/:country' do
redirect to('/login') unless current_user
@country = Everypolitician.country(slug: params[:country])
stats_raw = JSON.parse(open('https://everypolitician.github.io/gender-balance-country-stats/stats.json').read, symbolize_names: true)
stats = Hash[stats_raw.map { |c| [c[:slug], c] }]
@country_stats = stats[params[:country]]
@legislature_stats = Hash[@country_stats[:legislatures].map {|l| [l[:slug], l]}]
@legislatures = @country.legislatures.map { |l| LegislatureReport.new(l, @legislature_stats[l.slug]) }
@legislatures = @country.legislatures.map { |l| Reports::LegislatureReport.new(l, @legislature_stats[l.slug]) }
erb :report, :layout => :layout_page
end

Expand Down
142 changes: 142 additions & 0 deletions lib/reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
module Reports
# Require including class to define a `stats` method which returns a hash with
# :female, :male and :total keys.
module GenderStats
def female
@female ||= stats[:female].to_f
end

def male
@male ||= stats[:male].to_f
end

def total
@total ||= stats[:total].to_f
end

def female_percentage
@female_percentage ||= female / total * 100
end

def male_percentage
@male_percentage ||= male / total * 100
end
end

class LegislatureReport
extend Forwardable
include GenderStats

# Delegate 'name' and 'slug' method calls to the 'legislature' object.
def_delegators :legislature, :name, :slug

attr_reader :legislature
attr_reader :raw_stats

def initialize(legislature, raw_stats)
@legislature = legislature
@raw_stats = raw_stats
end

def stats
@stats ||= raw_stats[:totals][:overall]
end

def groups
@groups ||= raw_stats[:totals][:parties].map { |slug, group_stats| GroupReport.new(slug, group_stats, raw_stats, legislative_periods) }
end

def legislative_periods
@legislative_periods ||= legislature.legislative_periods.map { |lp| LegislativePeriodReport.new(lp, raw_stats) }
end
end

class GroupReport
include GenderStats

attr_reader :slug
attr_reader :stats
attr_reader :raw_stats

def initialize(slug, stats, raw_stats, legislative_periods)
@slug = slug
@stats = stats
@raw_stats = raw_stats
@legislative_periods = legislative_periods
end

def name
stats[:name]
end

def id_slug
@id_slug ||= slug.to_s.sub('/', '-')
end

def legislative_periods
@legislative_periods.map do |term|
term_stats = raw_stats[:terms]["term/#{term.slug}".to_sym]
group_stats = term_stats[:parties][slug]
GroupLegilativePeriodReport.new(term, group_stats) if group_stats
end.compact
end
end

class GroupLegilativePeriodReport
include GenderStats

attr_reader :term
attr_reader :stats

def initialize(term, stats)
@term = term
@stats = stats
end

def name
term.name
end
end

class LegislativePeriodReport
extend Forwardable
include GenderStats

# Delegate 'name' and 'slug' method calls to the 'legislative_period' object.
def_delegators :legislative_period, :name, :slug

attr_reader :legislative_period
attr_reader :raw_stats

def initialize(legislative_period, raw_stats)
@legislative_period = legislative_period
@raw_stats = raw_stats
end

def legislative_period_stats
@legislative_period_stats ||= raw_stats[:terms]["term/#{legislative_period.slug}".to_sym]
end

def stats
@stats ||= legislative_period_stats[:overall]
end

def groups
@groups = legislative_period_stats[:parties].map { |_, group_stats| LegislativePeriodGroupReport.new(group_stats) }
end
end

class LegislativePeriodGroupReport
include GenderStats

attr_reader :stats

def initialize(stats)
@stats = stats
end

def name
stats[:name]
end
end
end

0 comments on commit be09f86

Please sign in to comment.