mixins

How to include a module in a factory_girl factory?

我们两清 提交于 2019-12-03 05:17:40
I'm trying to reuse a helper method in all my factories, however I cannot get it to work. Here's my setup: Helper module (in spec/support/test_helpers.rb) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = ['A'..'Z', 'a'..'z', '0'..'9'].map{|r|r.to_a}.flatten (0...length).map{ chars[rand(chars.size)] }.join end end end A factory (in spec/factories/users.rb) FactoryGirl.define do factory :user do sequence(:username) { |n| "username-#{n}" } password random_string password_confirmation { |u| u.password } end end If I

“The Ruby way” (mixins and class reopening) vs. dependency injection

故事扮演 提交于 2019-12-03 05:01:14
In studying mixins vs. dependency injection, I often hear the phrase "the Ruby way." Often developers say something along the lines of Ruby lets you reopen classes and redefine methods means that you can easily "inject" new references into your code at test-time. (see #6 at http://weblog.jamisbuck.org/2007/7/29/net-ssh-revisited ) But testing is not my main concern; my concern is class reuse. I want classes I can reuse in multiple enterprise-scale Rails applications. So what happened to REUSING classes? Using mixins and reopening classes does not seem to provide a way to write classes in such

Mixing multiple traits in Scala

a 夏天 提交于 2019-12-03 04:04:31
问题 Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types . Suppose I have the traits Student, Worker, Underpaid, and Young. How could I declare a class ( not instance ), CollegeStudent, with all these traits? Note: I am aware of the simplests cases, such as CollegeStudent with one or two Traits: class CollegeStudent extends Student with Worker 回答1: It is easy, when declaring a class you just use the "with" keyword as often as you want class CollegeStudent

Initializing instance variables in Mixins

旧城冷巷雨未停 提交于 2019-12-03 03:32:52
Is there any clean way to initialize instance variables in a Module intended to be used as Mixin? For example, I have the following: module Example def on(...) @handlers ||= {} # do something with @handlers end def all(...) @all_handlers ||= [] # do something with @all_handlers end def unhandled(...) @unhandled ||= [] # do something with unhandled end def do_something(..) @handlers ||= {} @unhandled ||= [] @all_handlers ||= [] # potentially do something with any of the 3 above end end Notice that I have to check again and again if each @member has been properly initialized in each function --

Dynamically mixin a base class to an instance in Python

三世轮回 提交于 2019-12-03 03:04:56
问题 Is it possible to add a base class to an object instance (not a class!) at runtime? Something along the lines of how Object#extend works in Ruby: class Gentleman(object): def introduce_self(self): return "Hello, my name is %s" % self.name class Person(object): def __init__(self, name): self.name = name p = Person("John") # how to implement this method? extend(p, Gentleman) p.introduce_self() # => "Hello, my name is John" 回答1: This dynamically defines a new class GentlePerson , and reassigns p

LESS mix-in for nth-child?

 ̄綄美尐妖づ 提交于 2019-12-03 03:00:45
I'm trying to make a LESS mixin that will give me this output: .resource:nth-child(8n+1) { clear: left; } I've got this so far: .wrap-every(@n) { &:nth-child(@n + "n+1") { // parse error on this line clear: left; } } .resource { .wrap-every(8); } But it's giving a parse error on the indicated line ParseError: Unrecognised input Is there a way to do this? Less >= 1.4 you could do something like this: .wrap-every(@n) { &:nth-child(@{n}n + 1) { clear: left; } } this should have the desired output. Without any hacks needed. in older versins of Less you can try simple string interpolation : .wrap

EventEmitter and Subscriber ES6 Syntax with React Native

本秂侑毒 提交于 2019-12-03 02:57:37
I am trying to implement an EventEmitter/Subscriber relationship between two components in a react native class. I have seen referenced the following materials: React Native - Event Emitters by Colin Ramsay React Native - Call Function of child from NavigatorIOS These solutions are adequate for what I am trying to accomplish, however, they bother require the use of mixins: [Subscribable.Mixin] on the receiving component to work properly with Subscriber . Unfortunately, I am using ES6 and extending my classes from Component so I can not use this mixin syntax. My question is: How can I implement

Sass mixin for background transparency back to IE8

一世执手 提交于 2019-12-03 02:31:12
问题 I'm new to Sass and struggling with this. I can't get the color to render in both hex (for IE) and rgba . Every little piece is frustrating me because I haven't mastered the syntax yet, and Google results for Sass are still sparse. Here's the mixin: @mixin transparent($hex, $a){ /* for IEGR8 */ background: transparent; filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#{$a}#{$hex},endColorstr=#{$a}#{$hex}); zoom: 1; /* for modern browsers */ background-color: rgba(#{$hex},.#{$a

Sass - Converting Hex to RGBa for background opacity

好久不见. 提交于 2019-12-03 01:34:23
问题 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? 回答1: The

Abstract class + mixin + multiple inheritance in python

你。 提交于 2019-12-03 01:24:06
So, I think the code probably explains what I'm trying to do better than I can in words, so here goes: import abc class foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def bar(self): pass class bar_for_foo_mixin(object): def bar(self): print "This should satisfy the abstract method requirement" class myfoo(foo, bar_for_foo_mixin): def __init__(self): print "myfoo __init__ called" self.bar() obj = myfoo() The result: TypeError: Can't instantiate abstract class myfoo with abstract methods bar I'm trying to get the mixin class to satisfy the requirements of the abstract/interface