Created
October 13, 2013 19:04
-
-
Save rosiehoyem/6966099 to your computer and use it in GitHub Desktop.
Mass-assignment of attributes at initialization, day 14.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attributes = { | |
:name => "Avi", | |
:birthday => "01/29/1984", | |
:hair_color => "brown", | |
:eye_color => "brown", | |
:height => "tall", | |
:weight => "good", | |
:handed => "lefty", | |
:complexion => "decent", | |
:t_shirt_size => "medium", | |
:wrist_size => "small", | |
:glove_size => "normal", | |
:pant_length => "32", | |
:pant_width => "32", | |
} | |
class Person | |
attr_reader :name, :birthday, :hair_color, :eye_color, :height, :handed, :complexion, | |
:t_shirt_size, :wrist_size, :glove_size, :pant_length, :pant_width, :weight | |
def initialize(attributes) | |
attributes.each do |key, value| | |
self.instance_variable_set("@#{key}", value) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative './person_class.rb' | |
# Create a Person class that can take an arbitrary amount of | |
# properties on initialization through a hash. | |
# Each key in the attributes hash will be a property | |
RSpec.configure do |config| | |
# Use color in STDOUT | |
config.color_enabled = true | |
# Use color not only in STDOUT but also in pagers and files | |
config.tty = true | |
# Use the specified formatter | |
config.formatter = :progress # :progress, :html, :textmate | |
end | |
describe "Person", "Mass Assignment" do | |
it 'will assign an arbitrary amount of properties on initialization' do | |
attributes = { | |
:name => "Avi", | |
:birthday => "01/29/1984", | |
:hair_color => "brown", | |
:eye_color => "brown", | |
:height => "tall", | |
:weight => "good", | |
:handed => "lefty", | |
:complexion => "decent", | |
:t_shirt_size => "medium", | |
:wrist_size => "small", | |
:glove_size => "normal", | |
:pant_length => "32", | |
:pant_width => "32", | |
} | |
p = Person.new(attributes) | |
p.name.should eq(attributes[:name]) | |
p.birthday.should eq(attributes[:birthday]) | |
p.hair_color.should eq(attributes[:hair_color]) | |
p.eye_color.should eq(attributes[:eye_color]) | |
p.height.should eq(attributes[:height]) | |
p.weight.should eq(attributes[:weight]) | |
p.handed.should eq(attributes[:handed]) | |
p.complexion.should eq(attribetes[:complexion]) | |
p.t_shirt_size.should eq(attributes[:t_shirt_size]) | |
p.wrist_size.should eq(attributes[:wrist_size]) | |
p.glove_size.should eq(attributes[:glove_size]) | |
p.pant_length.should eq(attributes[:pant_length]) | |
p.pant_width.should eq(attributes[:pant_width]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment