poodr | Practical Object-Oriented Design in Ruby | Web Framework library
kandi X-RAY | poodr Summary
kandi X-RAY | poodr Summary
Practical Object-Oriented Design in Ruby.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Convenience method to handle the wheel
poodr Key Features
poodr Examples and Code Snippets
Community Discussions
Trending Discussions on poodr
QUESTION
Background: I'm trying to refactor my code after reading Practical Object Oriented Design in Ruby (it's awesome), and in doing so, I want to introduce some more models that encapsulate responsibility, rather than have a single large file with logic (and case statements, for that).
Problem: To simplify the problem statement, I have a model Rule
that "has many" RuleConditions
. However, there is only one table in the database for rules. In it, I have a column for conditions that's of type jsonb
(based on the complications of a RuleCondition
). But I can't seem to accomplish this. Specifically I can't figure out how to instantiate a model with a nested model, and expect ActiveRecord to know how to convert the model to jsonb, and perhaps from the table back to a nested model. I also don't know if I can define a has_many
relationship without a table backing it using ActiveRecord.
What I expect:
I expect that that there should be some flow (defined by a mix of ActiveRecord and ActiveModel) that would make this flow possible
- Get params for a
Rule
. - Create a new array of
RuleConditions
from a subset of the params for a rule. - Do
Rule.new(rule)
where rule contains :conditions =>RuleCondition
- Do
rule.save!
- At some point later, fetch the rule from the table, and expect it to rebuild a
Rule
with the nestedRuleConditions
model from theconditions
attribute.
What I've tried:
What I thought would get me halfway there was the serialize, :conditions, JSON
, but it struggles to serialize my object. After that, I really don't know. I've played around with ActiveModel::Conversion as well. So I just need some guidance.
And, to be perfectly clear, calling as_json
on my RuleCondition
works like I expect it to (prints out the same JSON that used to be stored in the Rule
model and the database before attempting a refactor). So it's maybe that I don't understand serialize
(since it's supposed to YAML unless otherwise, I think the encoding is different than just "match my column type")
Edit:
Currently I have something like (barebones, 0 validations / associations)
...ANSWER
Answered 2018-Dec-16 at 13:42Keep in mind that database adapters handle certain serialization tasks for you. For instance: json and jsonb types in PostgreSQL will be converted between JSON object/array syntax and Ruby Hash or Array objects transparently. There is no need to use serialize in this case.
- api.rubyonrails.org
Don't use serialize
with native JSON/JSONB columns. Its meant to be used with string columns as a poor-mans alternative.
What you are trying to do is really outside the scope of what ActiveRecord does - AR is built around a relational model where models correspond to tables. And you cannot expect that AR will have any provisions to unmarshal a JSONB column into anything but basic scalar types. And I would consider if what you are doing is really worth the effort vs actually creating a separate table for the relation.
You are on the right track with ActiveModel::Model which will give your model the same behaviour as a regular model, but you should take a look at how ActiveRecord handles nested attributes:
QUESTION
In POODR, author Sandi Metz gives an example a Trip class, that needs to be prepared.
The Trip class defines a method prepare
, which from the outside appears to say it can prepare it self,
but in fact it does so by internally asking some other object to prepare it, like bellow
ANSWER
Answered 2018-Jan-07 at 11:09If I send you a letter saying "please, compute 2+2 for me", and I get a letter back which says "4", I have no idea how you come up with that answer, you may have memorized it, you may have looked it up, you may have computed it by hand, you may have used a calculator, you may have sent a letter to someone else asking them to compute 2+2, you may have sent several letters to several other people asking them to compute sub-problems of 2+2, you may have read the letter and decided based on its contents to pass it on to a specialist, you may have not read the letter and just blindly passed it on to someone else without even opening it, or your secretary may have opened it and answered on your behalf without you even knowing about it.
That's the fundamental nature of messaging: I cannot, should not, and must not know how you were able to answer. The only thing I can observe is the answer. As long as the answer is correct, it doesn't matter to me how the answer was obtained. Maybe it was printed out, mailed to China, computed by hand in an office, then mailed back and scanned. I don't know and I don't care.
- Is this line of thinking correct?
Yes.
- Is this what it means by intent and implementation separation?
I am not familiar with that phrase. Is that something from the book? If yes, you should explain what it means in your question.
- Or the analogy doesn't fall it line at all.
No, the messaging metaphor is a good one. It is the fundamental idea of OO. Classes are irrelevant, even objects are irrelevant. Messaging is what it's all about.
QUESTION
I am implementing BST in ruby and writing insert method using recursion.
I am trying to use attr_accessor for setting and getting root but it doesn't work. Can anyone help out?
...ANSWER
Answered 2017-Dec-26 at 03:38It's actually totally OK to use instance variables within instance methods. In fact, that's what they're for! Setters and getters allow things outside the instance to access variables inside the instance. They (basically) define instance methods for the class like this:
QUESTION
I was recently working on a project where I faced a dilemma of choosing between two ways of getting same results. Here is the class structure:
...ANSWER
Answered 2017-Nov-27 at 08:571) Obviously not, it performs JOIN of books & authors tables. What you've made requires 2 queries, instead of 1 join
you'll have book.find(id)
and author.find(book.author_id)
.
2) JOIN should be faster.
3) Since last_name
is a public interface, it absolutely doesn't violate design principles. It would violate principles if you were accessing author's last name from outside like that: Book.find(1).author.last_name
- that's a bad thing. Correct is: Book.find(1).authors_last_name
- and accessing author's name inside Model class.
Your provided example seems to be overcomplicated to me.
According to the example you shared, you only want to get full name of the book's author. So, the idea of splitting responsibility is correct, but in Author
class should be simple instance method full_name
, like:
QUESTION
Given a class:
...ANSWER
Answered 2017-Jan-14 at 21:19Ruby seems to place unusual emphasis on the intent of names and the implications of them. For example, unwrap_spiral
implies that it might do the operation in-place unless there was a compantion method like unwrap_spiral!
that made it clear it didn't.
unwrapped_spiral
may be a shade too verbose. It's not clear why spiral
factors into this so much when unwrapped
might suffice.
Another thing to consider is organizing methods that operate on spiral
under the same alphabetic ordering: spiral_unwrap
or spiral_unwrapped
.
QUESTION
I'm reading POODR and trying to wrap my head around Sandi's examples.
I'm especially interested in trying to understand how her UML diagrams work.
For example Figure 4.8 on page 75:
Looking at the image some questions come to mind:
moe Customer - This represents an instance of the
Customer
class, perhaps with an attribute name equal tomoe
, or perhaps it's an instance stored in a variable namedmoe
, either way the diagram makes it look like this box represents "an object that is and instance of the Customer class"?a TripFinder - This is a little confusing because it also seems like it represents "an object that is an instance of the TripFinder class"?
class Trip and class Bicycle - Now we see the
class
keyword, so does is Sandi trying to say that those boxes represent "a Trip Class object" or "a Bicycle Class object"? Both of which are "objects that are instances of the Class class"?An arrow seems to go from the "sender" to the "receiver" of a "message". In code that translates to "calling a method on the receiver object"? The fact that the arrow is coming from moe means that somewhere inside the
Customer
class there is a method that calls theTripFinder#suitable_trips
instance method. So the arrow going from the "sender" to the "receiver" tells us the receiver's method name (aka method signature?) but not the sender's method name right?What do the response arrows mean? Are they only return statements? What is the significance of a dotted vs. a solid line? I know in some diagrams they represent dependencies but now it seems like they are representing sending a message and getting a return statement. Is that the same thing as a dependency?
Why is the last line a solid line? Is it just a typo?
ANSWER
Answered 2017-Jan-11 at 10:30I assume, this has been created with a painting tool, not an UML CASE tool.
- (up to 3.) This naming is wrong. The lifeline is either described with just the instance
name
,:classifier
or asname:classifier
. See p. 570 17.3.4.1 Lifeline in the spec:
A Lifeline is shown using a symbol that consists of a rectangle forming its “head” followed by a vertical line (which may be dashed) that represents the lifetime of the participant. Information identifying the lifeline is displayed inside the rectangle in the following format:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install poodr
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page