<div dir="auto">Hey Tim</div><div dir="auto"><br></div><div dir="auto">I’m curious about the motivation for modelling these structures as POROs in the first place. It’s not clear to me what use they have without some kinda persistence?</div><div dir="auto"><br></div><div dir="auto">Identifying the boundaries of collaborating entities is always a bit contentious but for straightforward projects, the multifaceted Rails style model approach is perfectly functional imho. I also quite like the repository style abstraction used in Elixir/Ecto for example. </div><div dir="auto"><br></div><div dir="auto">I can see the appeal of building out simple entity structures and only adding what you need when required but I wonder, if you just took the “sensible defaults” of a framework like Rails, would this still be confusing? </div><div dir="auto"><br></div><div dir="auto">I guess you could still wrap up data access and separate business logic functions into some collaborating Module or Class if you really wanna keep things separate.</div><div dir="auto"><br></div><div dir="auto">✌️ </div><div dir="auto"><br></div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 22 Jun 2021 at 17:05, Andrew Donovan <<a href="mailto:andrew_donovan@hotmail.com">andrew_donovan@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">
<div lang="EN-GB" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="m_2808940914996974406WordSection1">
<p class="MsoNormal"><span>Hi Tim,<u></u><u></u></span></p>
<p class="MsoNormal"><span>Your db or permanent storage is your source of truth , if you’re using Rails then use all the facilities it offers such as ActiveRecord. If Product is somehow defining prices and other behaviour
for your digital items then it needs to be modelled in the db. If you don’t have a db schema I’d draw out a simply entity relationship diagram which should help in identifying where things are missing or don’t tie up. From the gist of the details below it
sounds like you have access to a database. I’d be wary of using code for defining product strategy, amongst other things how do you record product changes over time ?
<u></u><u></u></span></p>
<p class="MsoNormal"><span>Regards<u></u><u></u></span></p>
<p class="MsoNormal"><span>Andrew</span><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span>Regards</span></p></div></div><div lang="EN-GB" link="blue" vlink="purple" style="word-wrap:break-word"><div class="m_2808940914996974406WordSection1"><p class="MsoNormal"><span><u></u><u></u></span></p>
<p class="MsoNormal"><span>Andrew<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<div style="border-style:solid none none;border-top-width:1pt;padding:3pt 0cm 0cm;border-top-color:rgb(225,225,225)">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Chat <<a href="mailto:chat-bounces@lists.lrug.org" target="_blank">chat-bounces@lists.lrug.org</a>>
<b>On Behalf Of </b>Tim Cowlishaw<br>
<b>Sent:</b> 22 June 2021 15:43<br>
<b>To:</b> Ruby Group <<a href="mailto:chat@lists.lrug.org" target="_blank">chat@lists.lrug.org</a>><br>
<b>Subject:</b> [LRUG] Objects that are neither classes nor records b.ut something in between<u></u><u></u></span></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Hi there folks! Hope you're all doing well.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I've just run into a problem / conundrum / anti-pattern / code smell that seems to recur fairly frequently in projects I've worked on of late, and which i'm not really sure how to name in order to search for an existing solution, so i figured
I'd post it here in case anyone has some sensible advice, or in the hope that it provokes some interesting discussion, at least.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The problem basically arises when there's some 'object' in my system (using this word in the vaguest possible sense for now) which has both *data* (so fields that can be updated through an API or webapp, and which should probably be stored
in a database row), but also has a one-to-one correspondence with a specific bit of behaviour, expressed as code.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">An example:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I'm currently working on a project which is a digital library. It contains lots of Books:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">class Book < Struct.new(:id, :title, :author, :content); end</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Users can buy access to these books either individually (all books are the same price), or by buying a subscription to the whole library. Forgetting the DB / ORM side of this for a sec, i'd model a toy implementation of what i want to do
something like the following:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">class User</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def initialize(purchases=[])</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> @purchases = purchases</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> attr_reader :purchases</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def has_access_to?(book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> purchases.any? { |p| p.grants_access_to?(book) }</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">class Purchase < Struct.new(:product)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def grants_access_to?(book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> product.grants_access_to?(book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end </span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">class Product</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def self.price</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> raise NotImplementedError</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def </span><a href="http://self.name" target="_blank"><span style="font-family:"Courier New"">self.name</span></a><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> raise NotImplementedError</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def grants_access_to?(book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> raise NotImplementedError</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">class OneOffPurchase < Purchase</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def initialize(book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> @book = book</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> attr_reader :book</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def self.price</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> 500</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def </span><a href="http://self.name" target="_blank"><span style="font-family:"Courier New"">self.name</span></a><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> "A single book"</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def grants_access_to?(other_book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> </span><a href="http://book.id" target="_blank"><span style="font-family:"Courier New"">book.id</span></a><span style="font-family:"Courier New""> ==
</span><a href="http://other_book.id" target="_blank"><span style="font-family:"Courier New"">other_book.id</span></a><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">def Subscription < Purchase</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def self.price</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> 500</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def </span><a href="http://self.name" target="_blank"><span style="font-family:"Courier New"">self.name</span></a><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> "All inclusive special subscription"</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def grants_access_to?(book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> true</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal">end<u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">This all to me looks reasonably sensible so far, but i also want those product names and prices to be editable by site admins through our 'backoffice' webapp, so they need to be stored in the DB, and this is where I have trouble finding
a solution i'm happy with. Naively I could have an ActiveRecord type object and a fairly simple dispatch mechanism to the code that works out the permissions, but this has some fairly obvious flaws:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">class Product < Struct.new(:unique_key, :name, :price, :book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> def grants_accesss_to?(other_book)</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> if unique_key = :one_off && book.present?</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> </span><a href="http://book.id" target="_blank"><span style="font-family:"Courier New"">book.id</span></a><span style="font-family:"Courier New""> ==
</span><a href="http://other_book.id" target="_blank"><span style="font-family:"Courier New"">other_book.id</span></a><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> elsif unique_key == :one_off</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> raise "oops! we're in a totally inconsistent state</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> elsif unique_key == :subscription && book.present?</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> raise "yep, this makes absolutely no sense either"</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> elsif unique_key == :subscription</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> true</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> else</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> raise "this isn't even a real product type. hopeless."</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New""> end</span><u></u><u></u></p>
</div>
<div style="margin-left:30pt">
<p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">aside from all the brittleness and smells that you can see above, this is also kinda useless in a bunch of respects - we can't add new product types without making a change to the codebase, we have to do a bunch of convoluted validation
to avoid all the various possible inconsistent states we can get into above, and our codebase is tightly coupled to the value of that unique_key database field, which i'm very suspicious of?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Therefore, anyone got any smart ideas about how to do this better? The requirements I'm looking to fulfil, in summary:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">1) A product has a specific 'strategy' for granting access to a book for a user, expressed as ruby code<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">2) A product has a price and name that is editable as data through our web app's admin interface<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">And, in general, i'd be interested to know - is there a name for this type of (anti-)pattern - Types of things that refuse to sensibly sit in the domain of software classes or Database fields? Does any of this even make any sense? I'm
not so sure myself anymore.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Any thoughts gratefully received!<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Cheers,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Tim<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
</div>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</blockquote></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Sent from Gmail Mobile</div>