Regina Imhoff

Software Engineer in Austin, Texas

Category: Primer

Model-View-Controller

In modern web and mobile development there’s a concept of Model-View-Controller (MVC) which is simply a design pattern in which every instance belongs to one of three layers -> Model Layer, View Layer, or Controller Layer.  Layer is just a fancy term for objects that fulfill a role.

  • Model Layer
    • holds data
    • has no info about User Interface (UI)
    • called things like you would call them IRL
      • For Crapper Keeper I had models for:
        • Users
        • Containers
        • Items
  • View Layer
    • UI primarily
      • things users can see
    • Things they can interact with go here
      • buttons, text fields, etc.
    • Sends message to controller
  • Controller Layer
    • Management for the app
      • configure the views that the users can see and when they can see it
      • the directions for how the app should work/flow
    • Takes data from model objects that its views want information on
    • Updates model objects
    • Updates view with changes in model objects

Models do not interact directly with views – the controller layer does all of the talking between these layers, receiving and dispatching requests.

 

Ruby’s Method Lookup

In many programming languages you are able to lookup methods without much trouble – just start at the receiver and work up the chain until you find the method you want.  However, in Ruby, you are able to mix in modules and singleton classes, so it gets weird fast.

There are just a few steps on how Ruby looks up methods:

  1.  Look within singleton class
    • A singleton method is a method that is defined on a instance vs. to a class where the method would be available on all instances
  2.  Look within modules that extend singleton classes
    • If you can’t find the method on the singleton class, look at modules that extend the singleton class
    • If there are multiple extend modules, the later modules are more important and take precedence
  3.  Look within methods prepended to the method and methods defined on a class
  4.  Look within modules that were mixed in when class was defined
  5.  Look up the ancestor chain
  6.  Start again checking method_missing

© 2017 Regina Imhoff

Theme by Anders NorenUp ↑