Skip to content

Commit

Permalink
adding struct initializer for reuse later
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Brundage committed Nov 18, 2016
1 parent 6515a1b commit 339d88e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
15 changes: 8 additions & 7 deletions lib/thermostat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

class Thermostat

autoload :Config, File.join('thermostat', 'config')
autoload :ConfigLoader, File.join('thermostat', 'config_loader')
autoload :Fan, File.join('thermostat', 'fan')
autoload :HeatPump, File.join('thermostat', 'heat_pump')
autoload :InvalidSetpoint, File.join('thermostat', 'invalid_setpoint')
autoload :Sensor, File.join('thermostat', 'sensor')
autoload :Zone, File.join('thermostat', 'zone')
autoload :Config, File.join('thermostat', 'config')
autoload :ConfigLoader, File.join('thermostat', 'config_loader')
autoload :Fan, File.join('thermostat', 'fan')
autoload :HeatPump, File.join('thermostat', 'heat_pump')
autoload :InvalidSetpoint, File.join('thermostat', 'invalid_setpoint')
autoload :Sensor, File.join('thermostat', 'sensor')
autoload :StructInitializer, File.join('thermostat', 'struct_initializer')
autoload :Zone, File.join('thermostat', 'zone')

attr_reader :default_config

Expand Down
12 changes: 2 additions & 10 deletions lib/thermostat/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Thermostat
class Config < Struct.new(:cooldown_seconds, :initial_set_point, :max_set_point, :min_set_point)

include StructInitializer

COOLDOWN_SECONDS = 120
INITIAL_SET_POINT = 23.9
MAX_SET_POINT = 26.7
Expand All @@ -11,15 +13,5 @@ def self.default_initial_set_point; INITIAL_SET_POINT; end
def self.default_max_set_point; MAX_SET_POINT; end
def self.default_min_set_point; MIN_SET_POINT; end


def initialize(*args)
opts = args.last.is_a?(Hash) ? args.pop : Hash.new
super
members.each do |k|
next if self.send(k)
self.send "#{k}=", (opts[k] || self.class.send("default_#{k}") )
end
end

end
end
21 changes: 21 additions & 0 deletions lib/thermostat/struct_initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Thermostat
module StructInitializer

def initialize(*args)
opts = args.last.is_a?(Hash) ? args.pop : Hash.new
super
members.each do |k|
next if self.send(k)
self.send "#{k}=", (opts[k] || default_value_for(k))
end
end

private

def default_value_for(key)
m = "default_#{key}".intern
self.class.respond_to?(m) ? self.class.send(m) : nil
end

end
end
25 changes: 25 additions & 0 deletions spec/cases/thermostat/struct_initializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Llama < Struct.new(:length)
include Thermostat::StructInitializer
def self.default_length; 1; end
end
describe Thermostat::StructInitializer do

let(:default_length) { Llama.default_length }
let(:subject) { Llama.new length }

context 'when not initialized' do
let(:length) { nil }
it 'has a default length' do
expect( subject.length ).to eq(default_length)
end
end


context 'when initialized' do
let(:length) { 2 }
it 'overrides the default length' do
expect( subject.length ).to eq(length)
end
end

end

0 comments on commit 339d88e

Please sign in to comment.