Case classes in ruby [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Im trying to implement case_class in Ruby for academic reasons.
I have already read this question Redefining "class" keyword in Ruby
I'm having troubles to understand why def case_class is used inside a Module. Why are there two definitions of case_class?
The author of the answer says this "foo_immutable = Foo_immutable.new" works,
using the same code and irb I get
NameError: uninitialized constant Foo_inmutable
from (irb):3
from -e:1:in `load'
from -e:1:in `<main>'
Why does that happen? How should I initialize the constant?
Thanks!

First, a class is a instance of class Class, and Class inherited Module. For example:
class A
end
we defined a class A, it's class, so A is a instance of Class.
Since A is a instance of Class, it get all the instance methods in Class. And because Class inherited Module, and Module has this instance method "case_class", so, A get the method "case_class".
we can invoke like this: A.case_class. That why we defined "case_class" method in Module: in order to make all the classes have this method.
Second, these two method do not need to have the same name, the second invoke the first, by self.class.case_class(name, superclass, &blk).

Related

What is klass in rails? And how is it different from class? [duplicate]

This question already has answers here:
What is the difference between Class and Klass in ruby?
(4 answers)
Closed 4 years ago.
I saw klass in one of the existing rails project. Why is it used? and how come it's different from class in rails?
One is the name of a class and the other is just an undefined constant by default. And for the pair you're more likely to see, class and klass,
the former is a keyword for defining classes while the latter is just an identifier (like any other string of characters). It's used when you would like to write the word "class" but can't because it's a reserved keyword.
link hope it will help you.

Getting Null Exception at UserManager.GetRoles(Convert.ToInt32(userid)) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Exception: Object reference not set to an instance of an object.
{System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.HttpContextExtensions.GetOwinEnvironment(HttpContext context)
at System.Web.HttpContextExtensions.GetOwinContext(HttpContext context)
You did not instantiate UserManager, but trying to use it as a static class which it is not.
Use the new keyword (how do I create an instance of UserManager).

Where should helpful functions for my controller go in Rails? [duplicate]

This question already has answers here:
Where to put Ruby helper methods for Rails controllers?
(6 answers)
Closed 8 years ago.
I have a few helpful functions I want to use in my controller but I don't know where to put them. For instance I have a method to form a JSON structure which I need to use a few times.
I would probably end up putting it in some form of utilities class which would contain a bunch of useful functions. Is this the correct way to go about it?
The correct way to put helpers functions, is put them as private in the same controller or put in application controller if your helpers functions are used in most of your controllers.
EDITED
If your helpers functions have any function needed on the view, use helpers

Is it a code smell if an object knows a lot of its owner? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In our Delphi 2007 application we are using a lot of the following constructs
FdmBasic:=TdmBasicData(FindOwnerClass(AOwner,TdmBasicData));
The FindOwnerClass travels the Owner hierarchy of the current component upwards to find a specific class (in the example TdmBasicData). The resulting object is stored in the Field variable FdmBasic. We use this primarily to pass datamodules along.
Example:
When generating a report, the resulting data is compressed and stored in a Blob field of a table accessed through a datamodule TdmReportBaseData. In a separate module of our application, there is functionality to show the data from the report in a Paged form using ReportBuilder. The main code of this module (TdmRBReport), uses a class TRBTempdatabase to convert the compressed blob data into different tables that are usable in the Reportbuilder runtime reportdesigner.
TdmRBReport has access to TdmReportBaseData for all kinds of report-related data (type of report, report calculationsettings, etc). TRBTempDatabase is constructed in TdmRBReport but has to have access to TdmReportBasedata. So this is now done using the construction above:
constructor TRBTempDatabase.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FdmReportBaseData := TdmRBReport(FindOwnerClass(Owner, TdmRBReport)).dmReportBaseData;
end;{- .Create }
My feeling is that this means that TRBTempDatabase knows a lot of its owner, and I was wondering if this is some sort of code smell or Anti-pattern.
What are your thoughts about this? Is this a code smell? If so, what is a better way?
On the description presented here I regard this as mildly smelly. However, it seems easy to fix.
I'd be inclined to pass the dmReportBaseData object into the constructor of any component that needs it. This makes the contract clear at compile time rather than enforcing it at runtime as you currently do.
As it currently stands, the contract you enforce is stronger than it needs to be. Although TRBTempDatabase only requires a dmReportBaseData instance, it will only function if it can get that instance from a TdmRBReport report object.
Making this change would also allow TRBTempDatabase and TdmRBReport to have a divorce and still function successfully. And as #Lieven points out in the comments, this would likely make testing easier.
If all you're doing in a base class is maintaining a reference to a parent object then no, it's not code-smell, it's a perfectly legitimate use. You can explicitly design a base class to carry information about "something that might come later."
If the base class is relying on some characteristic of the derived class that isn't present in itself (i.e. the generalized class relies on a specialization of one of its children) then yeah, that might be a bit funky.

What are the ideal Unit Test Cases for different layers in repository pattern [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
can someone suggest the ideal Unit test cases that may fit in across each of the layers .
(which otherwise can be called as a standard).
for instance, in an ASP.NET MVC applictaion using a Repository pattern -
Controller - can assert for View names and format of the data returned to the views , from the controller action methods( i couldnt think of more , if u can please suggest).
Services Layer - ?? what can be written. because they in turn depend on the layers underneath.. ( can some one suggest a Unit Case with example for sevices layer)?.
One trivial question to finish off. Irrespective of the layers , the method being tested makes calls to other instance methods/static methods say,
public List<string> MethodUnderTest()
{
instance.SomeOtherMethod();
StaticMethod();
}
in each case it is neccesary to mock the methods calls by moving that to interfaces .? any thoughts on that . ( coz unit Testing by nomenclature should not depend on anything)
Can some
I recommend reading the Art of Unit Testing. It covers this stuff in detail.

Resources