mixins

What is the difference betwen including modules and embedding modules?

module Superpower # instance method def turn_invisible ... end # module method def Superpower.turn_into_toad ... end module Fly def flap_wings ... end end end Class Superman include Superpower ... def run_away # how to call flap_wings? # how to call tur...

Are there scala-like mixins for C++?

Scala Mixins ...

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

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... ...

Initializing a Module mixed in to a Model

Hi, I have this: class Bullet < ActiveRecord::Base include StagedVersionMethods ... end And this module StagedVersionMethods def initialize puts self.bullet_id end end When I create an instance of Bullet, the modules initialize method fires, but I get an ActiveRecord error: ...activerecord-2.2.2/lib/active_recor...

Overriding base class methods in a Controller

I'm trying to fool a very complex black box into displaying some floats differently (it's the Gruff graphing library, so this is being rendered to an image). In the console, I can paste this: logger = RAILS_DEFAULT_LOGGER logger.debug "Here's a float #{455.67.to_s}" eval %{class Float def to_s_with_time h = (self / 60...

How can I get nose to find class attributes defined on a base test class?

I'm getting some integration tests running against the database, and I'd like to have a structure that looks something like this: class OracleMixin(object): oracle = True # ... set up the oracle connection class SqlServerMixin(object): sql_server = True # ... set up the sql server connection class SomeTests(object): ...

Abusing .NET 4.0 Interface Code Contracts feature to achieve MixIns functionality

.NET 4.0 has that new Code Contracts feature. It works with interfaces too, as described here (scroll down to somewhere in the comments): http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx Now my question is, can we use and abuse this "Default Interface Implementation Feature" by putti...

VCL multiple inheritance

Hi, I'm trying to develop a set of controls which all have a number of common behaviours with respect to sizing. I think that this is an instance where multiple inheritance is required (although am fully willing to accept any advice to the contrary). What I would like to do is basically a mixin pattern class Sizable { ......

Is there a BDD-style framework that allows multiple inherited behaviours?

Many of our system tests are written in a BDD style, and we make decent use of inherited behaviours to minimise duplication, for example this might be a basic hierarchy for purchase tests. class BehavesLikeSuccessfulPurchase class BehavesLikePurchaseWithValidCreditCard : BehavesLikeSuccessfulPurchase In this case the BehavesLikeSucces...

Where to put common code found in multiple models?

I have two models that contain the same method: def foo # do something end Where should I put this? I know common code goes in the lib directory in a Rails app. But if I put it in a new class in lib called 'Foo', and I need to add its functionality to both of my ActiveRecord models, do I do that like this: class A < ActiveRecord:...

Refactoring ActiveRecord models with a base class versus a base module

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 li...

scala: mixins depending on type of arguments

I have a set of classes of models, and a set of algorithms that can be run on the models. Not all classes of models can perform all algorithms. I want model classes to be able to declare what algorithms they can perform. The algorithms a model can perform may depend on its arguments. Example: Say I have two algorithms, MCMC, and Importa...

[Rails] Cannot access with_scope from a mixin

I just stumbled over a weird problem, and I don't really understand what is causing this. In our rails app, let's have a mixin Mixin: module Mixin def foo with_scope :find => ... do ... end end end which is includeed into a model class elsewhere: class Model < ActiveRecord::Base include Mixin ... end calling ...

How to use Maven 3 mixins?

I was trying to figure out how mixins are defined in Maven 3, but couldn't find anything other than buzz. It is propagated as one of the big new features here and here. I am currently feeling the pain of the hierarchical structure and would like to give it a spin. Does anyone have a pointer to documentation or the source defining the syn...

call a class method from inside an instance method from a module mixin (rails)

Curious how one would go about calling a class method from inside an instance method of a module which is included by an active record class. For example I want both user and client models to share the nuts and bolts of password encryption. # app/models class User < ActiveRecord::Base include Encrypt end class Client < ActiveRecord::...

What are some languages that currently support mixins?

Well obviously Ruby and Sass, but what are some other ones? ...

What are the elegant ways to do MixIns in Python?

I need to find an elegant way to do 2 kinds of MixIns. First: class A(object): def method1(self): do_something() Now, a MixInClass should make method1 do this: do_other() -> A.method1() -> do_smth_else() - i.e. basically "wrap" the older function. I'm pretty sure there must exist a good solution to this. Second: class B...

Help creating a JavaScript mixin in Tapestry5?

I am creating a mixin which renders a javascript file when a textfield gains focus. I am new to the idea of mixins in Tapestry, and I am unsure of where to place my original javascript file which i wish to run when the textfield gains focus. The following is an example of my code: The Java mixin class: package asc.mixins; import org....

Ruby Module Inclusion in Methods

In class Foo I'd like to include method Bar under certain conditions: module Bar def some_method "orly" end end class Foo def initialize(some_condition) if !some_condition "bar" else class << self; include Bar; end end end end Is there any cleaner (and clearer) way to achieve the incl...

Single Responsibility and Mixins

Given that Mixins generally introduce new behaviour into a class, this generally implies that a class would have more than one behaviour. If a class has a single responsibility this is defined as the class having only one reason for change. So, I can see this from two different perspectives The class only has one reason for change....