New puppet user, trying to wrap my head around name spaces and so forth.
I am currently working on a module called user. It has a unixuser class and a unixgroup class in unixuser.pp and unixgroup.pp. I'll limit my question to the creation of users.
init.pp currently only contains:
class user {
class {'user::unixuser': }
class {'user::unixgroup': }
# Create groups before creating users
Class['user::unixgroup'] -> ['user::unixuser']
}
In unixuser.pp and unixgroup.pp I only want to define the virtual resources. The functions that do this should be in functions.pp for readability.
Question 1: Should functions.pp should only contain definitions or should the definitions sit inside a class? So in code, should functions.pp contain this:
class user::functions {
define login ($uid, $gid) {
user { $title:
ensure => present,
uid => $uid,
gid => $gid,
}
}
}
Or should define login not be in the user::functions class? If so, should the define line be define user::login or something else?
Going further, unixuser.pp will then contain something similar to the following (incomplete) stanza:
class user::unixuser {
#something { 'admin':
uid => 1000,
gid => 1000,
}
}
If all of this is finished then I will realize the users from nodes.pp with something like realize User::Something['admin'].
Question 2: How should the class user::unixuser be made aware of the login function? Is this done by having class user::unixuser inherits user::functions or should I have include user::functions or class {'user::functions': } or...?
Question 3: Based on question 2, what comes in the place of #something when the user::unixuser class plays nicely with functions.pp?
I tried to formulate the question to the best of my abilities but if it is still a bit chaotic I apologize. :)
First off if a user is assigned to a group that group is auto required, so you shouldn't need the following,:
# Create groups before creating users
Class['user::unixgroup'] -> ['user::unixuser']
functions.pp looks fine try the following in init.pp:
class user {
include user::functions
include user::unixgroup
include user::unixuser
}
and unixuser.pp:
class user::unixuser {
login { 'admin':
uid => 1000,
gid => 1000,
}
...
}
Related
By importing classes at two places do I create 2 different instances?
* content of "MyClass.js"
class MyClass {
constructor() {}
isAuthenticated() {}
}
const cls = new MyClass();
export default cls;
--------------------------------
* content of "router.js"
import auth from "./MyClass";
Vue.use(Router)
--------------------------------
content of "./plugins/MyPlugin.js"
import clsInstance from "./MyClass";
export default {
install(Vue) {
Vue.prototype.$auth = clsInstance;
}
}
--------------------------------
* content of main.js
import myFirstPlugin from "./plugins/MyPlugin.js";
Vue.use(myFirstPlugin);
router.beforeEach((to, from, next) => {
if( auth.isAuthenticated() ){}
}
new Vue({
router
})
--------------------------------
* content of someComponent.vue
methods: {
logOut() {
this.$auth.isAuthenticated()
}
}
Is "auth.isAuthenticated" inside of "router.beforeEach" in "main.js"
identical
with
this.$auth.isAuthenticated() inside of "logOut" in "someComponent.vue"
or there are actually two different instances of "MyClass"created?
import is much the same as require. The code in MyClass.js will only be run once, creating a single instance of MyClass. Both calls to import will be pulling in the same instance.
You can confirm this by:
Putting some console logging in MyClass.js. Note that it only gets run once no matter how many times you import it.
Add a property to the object you import in one file (e.g. set auth.myFlag = true) and then check whether that flag is also present in the other file (i.e. check clsInstance.myFlag). This isn't totally conclusive but it's a pretty good way to verify that it's the same object rather than two separate instances.
If you wanted separate instances you might want to try exporting the class itself so that each file can create its own instance.
From the code you've posted I believe auth.isAuthenticated() and this.$auth.isAuthenticated() are calling the same method on the same object and (depending on what isAuthenticated does) should give the same result.
Let's say I'm writing a swift module, and I want to name a type with a name which already exists. If the name is from another module, it's easy. I can just use the module name as a namespace:
import Foundation
class MyClass {
class Notification : Foundation.Notification { ... }
}
My question is, is there any way to do the same with types in the same module? For example, I would like to be able to do something like this:
class Notification { ... }
class MyClass {
class Notification : Module.Notification { ... }
}
Where Module.Notification is a reference to the type declared above. Is such a thing possible?
You need to use the actual name of your module:
class Notification { ... }
class MyClass {
class Notification : MyAmazingTwitterApp.Notification { ... }
}
If you're working in Xcode, this defaults to your target name. There's a build setting "Product Module Name", under "Packaging" that lets you change this.
If you're using the Swift build system, this is of course specified in your manifest file, via the PackageDescription.
What does it mean in Coffeescript when a variable name begins with an "#" sign?
For example, I've been looking through the hubot source code and just in the first few lines I've looked at, I found
class Brain extends EventEmitter
# Represents somewhat persistent storage for the robot. Extend this.
#
# Returns a new Brain with no external storage.
constructor: (robot) ->
#data =
users: { }
_private: { }
#autoSave = true
robot.on "running", =>
#resetSaveInterval 5
I've seen it several other places, but I haven't been able to guess what it means.
The # symbol is a shorcut for this as you can see in Operators and Aliases.
As a shortcut for this.property, you can use #property.
It basically means that the “#” variables are instance variables of the class, that is, class members. Which souldn't be confused with class variables, that you can compare to static members.
Also, you can think of #variables as the this or self operators of OOP languages, but it's not the exact same thing as the old javascript this. That javascript this refer to the current scope, which causes some problems when your are trying to refer to the class scope inside a callback for example, that's why coffescript have introduced the #variables, to solve this kind of problem.
For example, consider the following code:
Brain.prototype = new EventEmitter();
function Brain(robot){
// Represents somewhat persistent storage for the robot. Extend this.
//
// Returns a new Brain with no external storage.
this.data = {
users: { },
_private: { }
};
this.autoSave = true;
var self = this;
robot.on('running', fucntion myCallback() {
// here is the problem, if you try to call `this` here
// it will refer to the `myCallback` instead of the parent
// this.resetSaveInterval(5);
// therefore you have to use the cached `self` way
// which coffeescript solved using #variables
self.resetSaveInterval(5);
});
}
Final thought, the # these days means that you are referring to the class instance (i.e., this or self). So, #data basically means this.data, so, without the #, it would refer to any visible variable data on scope.
How to give namespace for Block\Catalog\Product\List in magento 2
namespace Lesson\Chapter\Block\Catalog\Product\List;
class Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
protected $tglssearchHelper;
public function __construct(
\Lesson\Chapter\Helper\Data $tglssearchHelper
) {
$this->tglssearchHelper = $tglssearchHelper;
}
}
1) Since List is a keyword in php im unable to create a namespace.
2) Also how to give the class path of List Toolbar since Magento 2
does not have this Block/Product/List/Toolbar class.
i get syntax error, unexpected 'List' (T_LIST), expecting identifier
(T_STRING) in \Block\Catalog\Product\List\Toolbar.php on line 2
List is a restricted PHP word.
You cannot use any of the following words as constants, class names, function or method names.
http://www.php.net/manual/en/reserved.keywords.php
To answer your question, you will have to change your list class name to something else. MyList, CarList, Listing, etc.
I'd like to be able to parametrize my exports not only with types (as in, generic exports), but also with values.
Something like:
class Greeter
{
readonly string _format;
public Greeter( string format ) { _format = format; }
public string Greet( string name ) { return string.Format( _format, name ); }
}
// ...
var e = new ExportProvider();
e.ExportParametrized<Greeter>( args: new[] { "Hi, {0}!" } );
e.ExportParametrized<Greeter>( args: new[] { "¡Hola, {0}!" } );
// And then:
[ImportMany] IEnumerable<Greeter> Greeters { get; set; }
foreach( var g in Greeters ) Console.WriteLine( g.Greet( "John" ) );
// Should print out:
// Hello, John!
// ¡Hola, John!
One might ask: why don't I simply export the value new Greeter( "Hello, {0}!" ) using ComposablePartExportProvider and CompositionBatch?
While this approach would work in this particular case, it has an important flaw: if the Greeter class had any imports of its own, they would not be satisfied.
The usual way I would go about this is to declare two classes - EnglishGreeter and SpanishGreeter, inherit them both from Greeter, and then provide the appropriate arguments in the call to base constructor.
But this doesn't work for two reasons:
This is a lot of noise to write. Not only do I have to type the whole shebang, I also have to come up with names for those classes, and it doesn't always make sense to have names. Not to mention the DRY principle. But even besides the noise...
Sometimes I don't know the parameters upfront. Say, for example, my greeting formats were coming from some kind of config file.
Here is another thought, to somewhat clarify what I'm looking for.
This problem is almost solved in the TypeCatalog. See, the TypeCatalog knows about the type and it calls the type's constructor to create the part on demand.
One can think of this process from another standpoint: the catalog has a factory function; using that function, it creates the part, then satisfies its non-prerequisite imports, and then returns the part back to the requestor.
Now, in the particular case of TypeCatalog, the factory function just happens to be the type's own constructor. If only I could hook in and replace the factory function with my own, but still leverage the rest of the machinery, that would be exactly what I'm looking for.
You can achieve this by using property exports. You could define a class specifically for those kinds of exports, and it will look like this:
class MyParameterizedExports
{
[Export(typeof(Greeter))]
private Greeter EnglishGreeter
{
get
{
Greeter g = new Greeter("Hi, {0}!");
container.SatisfyImportsOnce(g);
return g;
}
}
[Export(typeof(Greeter))]
private Greeter SpanishGreeter
{
get
{
Greeter g = new Greeter("¡Hola, {0}!");
container.SatisfyImportsOnce(g);
return g;
}
}
}
Here you export two separate Greeter instances without having to define a new class for each type of Greeter.