/SOA-Flip-Flap

Service Architecture Homework on the use of Mixins and Modules

Primary LanguageRuby

FlipFlap HW

Overview

This homework assignment asks you to create two files (a module and a class) that are needed to pass all tests.

Installing Assignment Files

1. Setting up Github

If you are new to git, please complete the following:

2. Getting the assignment code from Github

Now, clone the files needed to start this homework assignment in a new directory:

$ git clone git@github.com:ISS-SOA/FlipFlap_HW.git

This will create a directory called FlipFlap_HW/ in your current directory:

DO NOT FORK THE CLASS REPO because you will not submit your solution as pull requests!

If you are new to git and Github, please read the submission instructions at the end, very carefully. To submit your assignment, you will later create your own personal repository on Github and push to it.

Your Assignment

You are given two application files: tsv_to_yml.rb, yml_to_tsv.rb. DO NOT EDIT THESE FILES. These two command line applications will be able convert between TSV and Yaml data formats using the FlipFlap class, once you complete the assignment.

The FlipFlap class is in flip_flap.rb, which you may modify. However, to complete FlipFlap, you must first create two modules called TsvBuddy (in file tsv_buddy.rb) and YamlBuddy (in file yaml_buddy.rb)

1. tsv_buddy.rb

You must fill out two methods in TsvBuddy:

take_tsv

def take_tsv(tsv) : This method should take a String called tsv and convert it into a data structure called @data. For example, you may want @data to be an Array of Hashes that looks something like this:

[{"date"=>"9/12/2014 20:20:55",
  "student_id"=>"3452",
  "languages"=>"Java, C , HTML , JavaScript , ",
  "best_language"=>"Java",
  "app_experience"=>"Native GUI applications, Database Driven Applications",
  "tech_experience"=>
   "Unix-based OS, Database"},

      <more hashes here>

]

to_tsv

def to_tsv : This method should take any data in @data and return a String in TSV format.

Note that modules cannot be made into object instances (i.e., you cannot do mymod = TsvBuddy.new. So to test this module in the beginning, you can do something like this:

  class Tester
    include TsvBuddy
  end

  t = Tester.new.take_tsv(File.read('data/survey.tsv'))

That should show you if take_tsv is creating the right structure in @data, for example. Of course, please run the full tests (described later) as your development progresses.

2. yaml_buddy.rb

This file should contain a module called YamlBuddy with two methods:

def take_yaml(yaml) : takes a yaml string and converts it into a data structure in @data.

def to_yaml : converts any data in @data into a yaml string.

Feel free to use the 'yaml' library in R to implement both methods.

3. flip_flap.rb

This file's FlipFlap class should mix in TsvBuddy and YamlBuddy modules (using include). Mixing these modules in will give class FlipFlap all of TsvBuddy's and YamlBuddy's methods.

And that's it!

Test Your Solution

Test your solution as you code. You should be able to convert TSV files into Yaml, and back into TSV with no change in information.

Test your code out by running the spec (test) that is provided:

$ bundle install
(only need to run this once; bundle should report success)

$ bundle exec ruby flip_flap_spec.rb

Also, take a peek into flip_flap_spec.rb to see how it works -- you will soon start writing your own specs for your projects!

Submission

This part is for those of you who are new to using git and Github.

1. Add your work to your local repository

Your working folder contains a hidden folder called .git/ where you can save snapshots (commits) of your work to share with others. Once you've finished the assignment, save a snapshot of your final work:

$ git add .
$ git commit -m "Solved the homework and all tests pass"

2. Push your local repository to Github

We will now copy the commit of your final work to a new repository on Github.

Go to the Github website and make sure you are signed in. Then, create a new Github repository:

  • Create a new repository in your Github account using the '+' button (call it SOA_flip_flap for example)
  • In your new Github repo, click the green 'Clone or download' button and copy the SSH URL of your Github repo (it should look like git@github.com:username/your_repo.git -- if you see an HTTP URL, click the 'Use SSH' link in the popup)

In your local FlipFlap_HW/ folder, set your new Github repo as the remote repo where you will be pushing your work:

$ git remote set-url origin <SSH URL of Github repo>

Now, push your local repo to the Github repo you created.

$ git push origin master

Please submit the HTTP URL of your Github repo (e.g., https://github.com/<username>/FlipFlap_HW -- not the SSH URL you used before).

What Did We Learn?

This assignment shows you the power of mixins in Ruby's object model. You have created a class (FlipFlap) that can in the future incorporate code to parse or create any kind of data format (CSV, Excel, etc.). To keep FlipFlap short and uncomplicated, we can make modules (like TsvBuddy and YamlBuddy) for other formats (CsvBuddy, ExcelBuddy, etc.) and simply mix them into FlipFlap. Using mixins cleanly separates our code by keeping the parsing logic away from other code needed by applications to handle the data. And we can do this all without inheritance.

Feel free to ask more questions on our Canvas board, and we will of course discuss this in class!