mixins

Refactoring ActiveRecord models with a base class versus a base module

邮差的信 提交于 2019-12-03 00:44:25
Class A and B are identical: class A < ActiveRecord::Base def foo puts "foo" end end class B < ActiveRecord::Base def foo puts "foo" end end What's the difference between refactoring like this with a base class : class Base < ActiveRecord::Base def foo puts "foo" end end class A < Base end class B < Base end versus like this using a base module : module Base def foo puts "foo" end end class A < ActiveRecord::Base include Base end class B < ActiveRecord::Base include Base end Is one way preferable over another? There's a fundamental difference between those two methods that all the other

Mixins vs composition in scala

一个人想着一个人 提交于 2019-12-03 00:16:28
问题 In java world (more precisely if you have no multiple inheritance/mixins) the rule of thumb is quite simple: "Favor object composition over class inheritance". I'd like to know if/how it is changed if you also consider mixins, especially in scala? Are mixins considered a way of multiple inheritance, or more class composition? Is there also a "Favor object composition over class composition" (or the other way around) guideline? I've seen quite some examples when people use (or abuse) mixins

Abstract classes vs. interfaces vs. mixins

浪子不回头ぞ 提交于 2019-12-03 00:14:21
问题 Could someone please explain to me the differences between abstract classes , interfaces , and mixins ? I've used each before in my code but I don't know the technical differences. 回答1: Abstract Class An abstract class is a class that is not designed to be instantiated. Abstract classes can have no implementation, some implementation, or all implementation. Abstract classes are designed to allow its subclasses share a common (default) implementation. A (pseudocoded) example of an abstract

What is the difference between an Abstract Class and a Mixin?

混江龙づ霸主 提交于 2019-12-02 23:05:26
I just found an article on a framework in Java that apparently allows it to support Mixins and something called Composite Oriented Programming (which for all I know might even be the same thing...) I've also heard of/worked with AOP, and I'm not sure how it differs from this either... At a language-agnostic level, a mixin just adds functionality to a class, and is more for programmer convenience and to avoid code duplication. An abstract (base) class forms an is-a relationship and allows for polymorphism. One reason why inheritance is overused is that it's an easy way to implement mixins

Jackson serialize property with dynamically different names using mixins

橙三吉。 提交于 2019-12-02 19:14:47
问题 I use different NoSQL databases and depending on the database I need to name the "id" different. So for example in OrientDB the id is named "@rid" @JsonProperty("@rid") private String id; And for MongoDB the id is named "_id" @JsonProperty("@_id") private String id; I do not know what is wrong with the modern DB developers not just naming the id field "id" ^^. But now I have a problem. How can I dynamically serialize/deserialize the id field in some case as "@rid" and in another case as "_id"

Can I simulate traits/mixins in Swift?

不羁岁月 提交于 2019-12-02 18:59:40
Does Swift have a way of mixing in traits, a la Scala? The section of the Swift book on using extensions to add protocols to existing classes comes tantalizingly close. However, since protocols can't contain an implementation, this can't be used to mix code into a class. Is there another way? As of Swift 2.0, yes! Providing Default Implementations You can use protocol extensions to provide a default implementation to any method or property requirement of that protocol. If a conforming type provides its own implementation of a required method or property, that implementation will be used

How can I use mixins or modules in my controllers in Rails 3?

大城市里の小女人 提交于 2019-12-02 18:57:42
I have some behavior in my controller that I pulled out into a module in order to test better and re-use it in a few places. Two questions about this: Where is a good place to put my modules? They need to run in order to be available to the controllers, so I was thinking the config/initializers/ directory. That seems a little suspect to me though. lib/ ? How do I ensure the code gets run so the modules are available to include in my controllers? Thank you kindly sirs. lib/ is an excellent place for modules; much better than config/initializers/ --at least in my opinion. If it's several modules

Abstract classes vs. interfaces vs. mixins

自作多情 提交于 2019-12-02 13:57:35
Could someone please explain to me the differences between abstract classes , interfaces , and mixins ? I've used each before in my code but I don't know the technical differences. Eva Abstract Class An abstract class is a class that is not designed to be instantiated. Abstract classes can have no implementation, some implementation, or all implementation. Abstract classes are designed to allow its subclasses share a common (default) implementation. A (pseudocoded) example of an abstract class would be something like this abstract class Shape { def abstract area(); // abstract (unimplemented

Sass - Converting Hex to RGBa for background opacity

早过忘川 提交于 2019-12-02 13:51:44
I have the following Sass mixin, which is a half complete modification of an RGBa example : @mixin background-opacity($color, $opacity: .3) { background: rgb(200, 54, 54); /* The Fallback */ background: rgba(200, 54, 54, $opacity); } I have applied $opacity ok, but now I am a stuck with the $color part. The colors I will be sending into the mixin will be HEX not RGB. My example use will be: element { @include background-opacity(#333, .5); } How can I use HEX values within this mixin? hopper The rgba() function can accept a single hex color as well decimal RGB values. For example, this would

Common beforeInsert and beforeUpdate methods from Mixin for common domain columns

对着背影说爱祢 提交于 2019-12-02 13:14:42
问题 Most of the domain objects our company uses will have some common properties. These represent the user that created the object, the user that last updated the object, and the program they used to do it. In the interest of DRYing out my domain classes, I want to find some way to add the same beforeInsert and beforeUpdate logic to all domain classes that have these columns without interfering with those that don't. How I'd like to do it is using a Mixin with its own beforeInsert and