Skip to content

Instantly share code, notes, and snippets.

@galaydaroman
Last active June 8, 2016 21:23
Show Gist options
  • Save galaydaroman/e6917f79229a6524c3a1ec0c03572a81 to your computer and use it in GitHub Desktop.
Save galaydaroman/e6917f79229a6524c3a1ec0c03572a81 to your computer and use it in GitHub Desktop.
require 'active_support'
require 'active_model'
require 'pry'
require 'awesome_print'
AwesomePrint.pry!
class Ages
extend ActiveModel::Naming
include ActiveModel::Validations
attr_reader :m1, :m2, :m3
attr_reader :f1, :f2, :f3
PAIRS = {
m1: :f3,
m2: :f1,
m3: :f2
}
validate :ages_constraints
def initialize
@m1, @m2, @m3 = [5, 5, 5]
@f1, @f2, @f3 = [0, 0, 0]
end
def set hash
changes = []
hash.each do |key, value|
next unless /[mf][1-3]/ === key.to_s
instance_variable_set "@#{key}", value.to_i
if /m/ === key.to_s
opposite_key = PAIRS[key]
instance_variable_set "@#{opposite_key}", send(key) - 5
changes << key.to_sym
else
opposite_key = PAIRS.invert[key]
instance_variable_set "@#{opposite_key}", send(key) + 5
changes << opposite_key
end
end
unchanged = PAIRS.keys - changes.uniq
if unchanged.size == 1
sum = changes.uniq.map(&method(:send)).reduce &:+
set unchanged[0] => (83 - sum)
end
self
end
def ages_constraints
errors.add :base, 'male_should_be_older_5' unless male_older_5?
errors.add :base, 'm1 == f2 + 1' unless constraint_1?
errors.add :base, 'm1 + f1 == 48' unless constraint_2?
errors.add :base, 'm3 + f1 == 52' unless constraint_3?
errors.add :base, 'Males ages should be 83' unless males_equal?
errors.add :base, 'Females ages should be 68' unless females_equal?
end
def male_older_5?
PAIRS.all? do |male, female|
send(male) == send(female) + 5
end
end
def constraint_1?
m1 == f2 + 1
end
def constraint_2?
m1 + f1 == 48
end
def constraint_3?
m3 + f1 == 52
end
def males_equal?
PAIRS.keys.map(&method(:send)).reduce(&:+) == 83
end
def females_equal?
PAIRS.values.map(&method(:send)).reduce(&:+) == 68
end
end
q = Ages.new
50.times do |m1|
50.times do |m2|
print '.'
q.set m1: m1, m2: m2
break if q.valid?
end
break if q.valid?
end
puts
puts q.valid?
puts q.errors.full_messages
puts q.inspect
__END__
OUTPUT:
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
true
#<Ages:0x007f9cca1dabf0 @m3=30, @m2=27, @m1=26, @f1=22, @f2=25, @f3=21, @validation_context=nil, @errors=#<ActiveModel::Errors:0x007f9cca2f9130 @base=#<Ages:0x007f9cca1dabf0 ...>, @messages={}>>
[Finished in 0.3s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment