Ruby gem design with PORO and no ORM

232 views
Skip to first unread message

theCrab

unread,
Jun 21, 2013, 10:32:46 AM6/21/13
to objects-...@googlegroups.com
Hi yall,
I am trying to achieve a feat with Ruby that I have not ventured before. I read a blog by Jose Mota about entitled "Extract your business" and in the article he asks "Why isn't your business a gem of its own?" 

So I embarked on a simple lesson to see if I could write the business logic into a gem and forget relying on Frameworks that are constantly changing and breaking the business model of an organisation.

So my quest is simple. I want to write a gem I called my_tree which details "My Family Tree". With just pure Ruby and Rspec for tests without ORM's or Persistence. My quest is to understand Inheritance and how to write better code and use Ruby Structures/Modules etc to create Associations/Validations like AR in Rails.

.
├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
├── TODO.txt
├── lib
│   ├── parent
│   │   ├── father.rb
│   │   ├── mother.rb
│   │   ├── child.rb
│   └── my_tree.rb
├── spec
└── my_treei.gemspec

It looks simple, maybe it is but I want to see how a family of 3 generations with each child having 3 offsprings(children) will come about.

Any hints?

Dave Newton

unread,
Jun 21, 2013, 10:40:39 AM6/21/13
to objects-...@googlegroups.com
I'm not entirely clear on the question.

My view of a "family tree" data model would have a single class representing a person; having separate classes for "father", "mother", and "child" seems redundant and counter-OOP. People have properties, and that's it.

Similarly, the "union" of two people have properties, namely, a collection of offspring, and the offspring are *also* people, and so on.

This is nothing more than a tree structure, one of the most common data structures.

Dave



--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Benjamin Fleischer

unread,
Jun 21, 2013, 10:47:26 AM6/21/13
to objects-...@googlegroups.com

On Fri, Jun 21, 2013 at 9:40 AM, Dave Newton <davel...@gmail.com> wrote:
My view of a "family tree" data model would have a single class representing a person; having separate classes for "father", "mother", and "child" seems redundant and counter-OOP. People have properties, and that's it.

for one thing see https://en.wikipedia.org/wiki/GEDCOM#GEDCOM_model (learn from the past!)

I agree with Dave in terms of modeling.  In your proposal, I would be modeled as a father, husband, son, grandson, great-grandson, uncle, etc.  I am still the same person. The only thing changing in the previous list is the relationship.  So, I am a node, with different relationships to other nodes. And those relationships have data associated with them.  

-Benjamin

Kerri Miller

unread,
Jun 21, 2013, 11:06:19 AM6/21/13
to objects-...@googlegroups.com
Exactly what I was thinking, too.  Extracting the business rules into a gem seems to me to mean that you're gathering up the code that describes  the messages nodes send to each other, and how they respond, given the nature of their relationship.  The description of those responses feel an awful lot like a Role or module.. "Oh, in __this__ relationship you're a Crazy Spinster Aunt? Here, have use this code to interpret ```#purchase_gift```"

-k-
 

Hugo Estrada

unread,
Jun 21, 2013, 11:27:54 AM6/21/13
to objects-...@googlegroups.com
Your goal is pretty cool. I have done something similar in the .NET world, but not as well defined as you describe it. I do like it how the gem concept makes it even tidier. In one .NET project my lead and I just gave up on a relational persistence and just saved the objects themselves.

If your quest is to understand inheritance, then this project may not be the best one. As others said, you essentially have one object, a person, that has relational links to others.

Inheritance in OOP is more about sharing common data and methods between two different but similar objects. It is hard to come up with a good exercise for it. Try finding some domain, such as a racing game with different cars with different attributes, or simulating IC chips, or model an RPG character and its different classes.

In my personal experience inheritance for business applications seems to be rarely needed, but  if you do have the need for it, then it is a powerful concept.  

--

Johnneylee Rollins

unread,
Jun 21, 2013, 11:44:32 AM6/21/13
to objects-...@googlegroups.com


On Jun 21, 2013 11:27 AM, "Hugo Estrada" <hugo...@gmail.com> wrote:
>
> Your goal is pretty cool. I have done something similar in the .NET world, but not as well defined as you describe it. I do like it how the gem concept makes it even tidier. In one .NET project my lead and I just gave up on a relational persistence and just saved the objects themselves.
>
> If your quest is to understand inheritance, then this project may not be the best one. As others said, you essentially have one object, a person, that has relational links to others.
>
> Inheritance in OOP is more about sharing common data and methods between two different but similar objects. It is hard to come up with a good exercise for it. Try finding some domain, such as a racing game with different cars with different attributes, or simulating IC chips, or model an RPG character and its different classes.

I believe that you could also split factory objects and the human relation behavior by having a person class for your factory and use modules for each side of a relation.

module Aunt
  attr_accessor :nieces, :nephews
  ... I'd add code for event listeners and emitters here to handle linking family members to the rest of their tree here.
end

This does suck in the way that it'd assume you want to mix this module into the person instance rather than the class. I'd also consider if this was worth doing, questioning whether there are many Aunt-specific behavior, if there isn't, I'd also posit that multiple classes would also not be a good way to compose your domain objects.


>
> In my personal experience inheritance for business applications seems to be rarely needed, but  if you do have the need for it, then it is a powerful concept.

I like to treat classes as factory objects primarily and not as a means of describing my object inheritance, but I also prefer modules, so the bias is fairly clear.  

~Johnneylee

Hugo Estrada

unread,
Jun 21, 2013, 12:28:55 PM6/21/13
to objects-...@googlegroups.com
Hi, Jonneylee,

May I ask you why you prefer modules? I personally don't like them that much, but I am curious to hear what benefits they bring from someone who does.

Hugo  

--

theCrab

unread,
Jun 21, 2013, 12:40:50 PM6/21/13
to objects-...@googlegroups.com
I have reviewed my exercise and found that I might have to change the domain. I also think FactoryGirl spoils the mix as am not looking for persistence rather messages shared across Classes.

Say I have rent out cars.

class Company
  include Address, Owner, Email, PhoneNumber
  attr_accessor :name, :address, :phone_number, :email, :owner

  def initialize(name)
    @name = name
    @address ||= address #new company address
    ...
  end
end

class Address
  attr_accessor :line_one, :line_two, :city, :state, :post_code, :company, :owner
end

class Owner
  include Address, Company, Email, PhoneNumber
  attr_accessor :first_name, :last_name, :address, :email, :phone_number

  def initialize(first_name, last_name)
    @first_name, @last_name = first_name, last_name
  end
end
..... you get the drift ;)

My question is, how is this message passing working? I don't really understand how to pass the messages around. Maybe am just tired

theCrab

unread,
Jun 21, 2013, 12:42:42 PM6/21/13
to objects-...@googlegroups.com
+1
I have no prereference but I'd like to know why modules over classes 

Thanks

Dave Newton

unread,
Jun 21, 2013, 12:46:25 PM6/21/13
to objects-...@googlegroups.com
On Fri, Jun 21, 2013 at 12:40 PM, theCrab <nke...@gmail.com> wrote:
My question is, how is this message passing working?

Which message passing?

Dave

theCrab

unread,
Jun 21, 2013, 12:58:26 PM6/21/13
to objects-...@googlegroups.com
Passing attributes around. I heard someone call it "passing messages around" in Ruby. Not so sure why but it could be say Company.new('Google Inc.').should respond_to :name 

So #respond_to checks that Company understands the message :name. Is that even right? :(

Here's what is going on in my head https://gist.github.com/theCrab/91787faf5179a63c4199

Hate that this Google editor has IDE style.

theCrab

Dave Newton

unread,
Jun 21, 2013, 1:02:08 PM6/21/13
to objects-...@googlegroups.com
On Fri, Jun 21, 2013 at 12:58 PM, theCrab <nke...@gmail.com> wrote:
Passing attributes around. I heard someone call it "passing messages around" in Ruby. Not so sure why but it could be say Company.new('Google Inc.').should respond_to :name 

So #respond_to checks that Company understands the message :name.

Correct, `respond_to` checks if an object responds to a given message.

Dave

Benjamin Fleischer

unread,
Jun 21, 2013, 1:11:42 PM6/21/13
to objects-on-rails

On Fri, Jun 21, 2013 at 10:27 AM, Hugo Estrada <hugo...@gmail.com> wrote:
Inheritance in OOP is more about sharing common data and methods between two different but similar objects. It is hard to come up with a good exercise for it.

general rule 'prefer composition over inheritance'.   --> looser coupling, more defined object boundaries. e.g. if Rails models/db object didn't all inherit from ActiveRecord::Base, which has a very wide public api/boundary, it might not be so tempting to mix db stuff and business logic in your model and throughout the application

obligatory groaner:


tenderlove
I met a guy who really preferred inheritance over composition. He was a super man.
6/14/13 1:59 PM

theCrab

unread,
Jun 21, 2013, 1:18:15 PM6/21/13
to objects-...@googlegroups.com
Throw me some code example please?

Shane Mingins

unread,
Jun 21, 2013, 8:07:43 PM6/21/13
to objects-...@googlegroups.com
Just skimming through this thread it would seem you are trying to solve two problems ... How to model a family tree domain ... And how to extract your business logic into a gem.  Given your goal seemed to be the latter, it might be better to take an existing app you have or one where you already have modeled the domain and use that.  Just my 2 cents worth :-)

Good luck
Shane 

Sent from my iPhone
--

Kris Leech

unread,
Jun 22, 2013, 11:19:27 AM6/22/13
to objects-...@googlegroups.com
Have you got a git repo so we can follow along?

theCrab wrote:
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
===========================================================
I welcome VSRE emails. Learn more at http://vsre.info
===========================================================

Hugo Estrada

unread,
Jun 22, 2013, 12:10:12 PM6/22/13
to objects-...@googlegroups.com
I agree with you, Benjamin. Yet, theCrab wants to learn the concept of inheritance. :)

Also, sometimes there are legitimate uses for inheritance. The use of inheritance by the Ruby libraries is a good example. It just happens that if you are writing business applications it is rare to justify inheritance.  The rule was an attempt to dial down on unneeded inheritance that we all engaged in back when we first learned OOP.

There are some techniques and patterns that are not used all the time, but once we run into a good use for them, they are a godsend. I particularly hate the singleton pattern, but I learned it anyway. And when I had the need for it, it was good that that I had it handy.



--

Matteo Vaccari

unread,
Jun 23, 2013, 11:58:17 PM6/23/13
to objects-...@googlegroups.com
On Fri, Jun 21, 2013 at 4:32 PM, theCrab <nke...@gmail.com> wrote:
Hi yall,
I am trying to achieve a feat with Ruby that I have not ventured before. I read a blog by Jose Mota about entitled "Extract your business" and in the article he asks "Why isn't your business a gem of its own?" 

So I embarked on a simple lesson to see if I could write the business logic into a gem and forget relying on Frameworks that are constantly changing and breaking the business model of an organisation.

So my quest is simple. I want to write a gem I called my_tree which details "My Family Tree". With just pure Ruby and Rspec for tests without ORM's or Persistence. My quest is to understand Inheritance and how to write better code and use Ruby Structures/Modules etc to create Associations/Validations like AR in Rails.

When we talk about "business" usually we talk about business *logic*, that is, we assume that code *does* something beyond defining data that represents something. Do you have specific behaviours in mind?  Use cases?  User stories?  

Matteo

theCrab

unread,
Jun 24, 2013, 8:21:16 AM6/24/13
to objects-...@googlegroups.com, vac...@pobox.com
I have been trying to find a real use case for this exercise. While my brain is bogged down with work, I can only think of some problems I had many years ago when i was learning RSpec and Ruby TDD at the same time. With BDD I just seam to end up rolling out a Rails app and forget it when am done with it, only to rush when @Homakov finds vulnerabilities.

So I think it's best to put something up and see where it leads. I am getting the github repo ready. I'll share it shortly. 

Brian Knapp

unread,
Aug 20, 2013, 11:17:59 PM8/20/13
to objects-...@googlegroups.com
If you want to use POROs and no ORM but still have validation, check out Obvious and the Obvious Status example app. In there you'll find clean PORO entities and some other fun stuff.

-Brian

Kelem

unread,
Aug 21, 2013, 4:03:31 AM8/21/13
to objects-...@googlegroups.com

Hey Brian.
Thank you for this.

--
You received this message because you are subscribed to a topic in the Google Groups "Objects on Rails" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/objects-on-rails/3KU8rN_jZFg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to objects-on-rai...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages