Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created March 3, 2012 16:15
Show Gist options
  • Save jnthn/1966845 to your computer and use it in GitHub Desktop.
Save jnthn/1966845 to your computer and use it in GitHub Desktop.
[
{
"name": "FlightBookedEvent",
"values": [ "flight_code", "passenger_name", "cost" ]
},
{
"name": "FlightCancelledEvent",
"values": [ "flight_code", "passenger_name" ]
}
]
module Events;
use JSON::Tiny;
package EXPORT::DEFAULT { }
my @events = @(from-json(slurp("events.json")));
for @events -> (:$name, :@values) {
my $type := Metamodel::ClassHOW.new_type(:$name);
for @values -> $attr_name {
$type.HOW.add_attribute($type, Attribute.new(
:name('$!' ~ $attr_name), :type(Mu),
:has_accessor(1), :package($type)
));
}
$type.HOW.compose($type);
EXPORT::DEFAULT.WHO{$name} := $type;
}
use Events;
my $e1 = FlightBookedEvent.new(
flight_code => 'AB123',
passenger_name => 'jnthn',
cost => 100);
say $e1.perl;
my $e2 = FlightCancelledEvent.new(
flight_code => 'AB123',
passenger_name => 'jnthn');
say $e2.flight_code;
say $e2.passenger_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment