Skip to content

Instantly share code, notes, and snippets.

@markglenfletcher
Created October 9, 2015 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markglenfletcher/65e446dced55762e25b8 to your computer and use it in GitHub Desktop.
Save markglenfletcher/65e446dced55762e25b8 to your computer and use it in GitHub Desktop.
DSL example
require 'active_support'
require 'active_support/core_ext'
class Program
class << self
def define(&block)
Program.new.tap { |p| p.instance_eval(&block) }
end
end
attr_accessor :program_name, :program_acronym, :program_length
attr_reader :routines
def initialize
@routines = []
end
alias_method :name, :program_name=
alias_method :acronym, :program_acronym=
alias_method :suggested_length, :program_length=
def routine(&block)
@routines << Routine.new.tap do |new_routine|
new_routine.instance_eval(&block)
end
end
end
class Routine
attr_accessor :routine_name
attr_reader :exercises
def initialize
@exercises = {}
end
alias_method :name, :routine_name=
def exercise(reference, &block)
@exercises[reference] ||= Exercise.new.tap do |new_exercise|
if block
new_exercise.instance_eval(&block)
end
end
end
end
class Exercise
attr_accessor :exercise_name, :exercise_acronym
attr_reader :exercise_sets, :increment_ranges
def initialize
@exercise_sets = []
@increment_ranges = []
end
alias_method :name, :exercise_name=
alias_method :acronym, :exercise_acronym=
def sets(quantity, &block)
quantity.times do
@exercise_sets << ExerciseSet.new.tap do |set|
set.instance_eval(&block)
end
end
end
def increment(attr={},&block)
@increment_ranges << IncrementRange.new(attr[:range], attr[:unit]).tap do |range|
range.instance_eval(&block)
end
end
end
class ExerciseSet
attr_accessor :set_reps
alias_method :reps, :set_reps=
end
class IncrementRange
attr_accessor :increment
attr_reader :range, :unit
def initialize(range, unit)
@range = range
@unit = unit
end
alias_method :increase, :increment=
end
Program.define do
name 'StrongLifts 5x5'
acronym 'SL5x5'
suggested_length 3.months
routine do
name 'Routine A'
exercise :squat do
name 'Squat'
acronym 'SQ'
sets 5 do
reps 5
end
increment :range => (0..9999), :unit => :lbs do
increase 5
end
increment :range => (0..9999), :unit => :kg do
increase 2.5
end
end
exercise :bench_press do
name 'Bench Press'
acronym 'BP'
sets 5 do
reps 5
end
increment :range => (0..9999), :unit => :lbs do
increase 5
end
increment :range => (0..9999), :unit => :kgs do
increase 2.5
end
end
exercise :barbell_row do
name 'Barbell Row'
acronym 'BR'
sets 5 do
reps 5
end
increment :range => (0..9999), :unit => :lbs do
increase 5
end
increment :range => (0..9999), :unit => :kgs do
increase 2.5
end
end
end
routine do
name 'Routine B'
exercise :squat
exercise :overhead_press do
name 'Overhead Press'
acronym 'OHP'
sets 5 do
reps 5
end
increment :range => (0..9999), :unit => :lbs do
increase 5
end
increment :range => (0..9999), :unit => :kgs do
increase 2.5
end
end
exercise :deadlift do
name 'Deadlift'
acronym 'DL'
sets 1 do
reps 5
end
increment :range => (0..225), :unit => :lbs do
increase 5
end
increment :range => (255..9999), :unit => :lbs do
increase 2.5
end
increment :range => (0..100), :unit => :kgs do
increase 2.5
end
increment :range => (100..9999), :unit => :kgs do
increase 1.25
end
end
end
end
source 'https://rubygems.org'
gem 'activesupport'
gem 'pry'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment