Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/22bb320f9a0e07faed99 to your computer and use it in GitHub Desktop.
Save mike-neck/22bb320f9a0e07faed99 to your computer and use it in GitHub Desktop.
Datomicのチュートリアルのデータスキーマのデータを準備するDSL
[
{:db/id #db/id[:db.part/user -3436826128599827928],
:district/name "Koo",
:district/region :region/ne}
{:db/id #db/id[:db.part/user -4827320198634435321],
:neighborhood/name "Hoo",
:neighborhood/district #db/id[:db.part/user -3436826128599827928]}
{:db/id #db/id[:db.part/user -1316594657738555491],
:community/name "Foo",
:community/url "http://localhost:8000/foo",
:community/category ["test"],
:community/orgtype :community.orgtype/commercial,
:community/type :community.type/twitter,
:community/neighborhood #db/id[:db.part/user -4827320198634435321]}
{:db/id #db/id[:db.part/user -7620519237280347981],
:district/name "Kansai",
:district/region :region/n}
{:db/id #db/id[:db.part/user -4648290718618480935],
:neighborhood/name "Kan Java",
:neighborhood/district #db/id[:db.part/user -7620519237280347981]}
{:db/id #db/id[:db.part/user -1718337805748871334],
:community/name "Hoge",
:community/url "http://localhost:8000/hoge",
:community/category ["test" "kudoh"],
:community/orgtype :community.orgtype/commercial,
:community/type :community.type/facebook-page,
:community/neighborhood #db/id[:db.part/user -4648290718618480935]}
{:db/id #db/id[:db.part/user -2528577332355840606],
:district/name "Value",
:district/region :region/ne}
{:db/id #db/id[:db.part/user -5749241506115709116],
:neighborhood/name "Val",
:neighborhood/district #db/id[:db.part/user -2528577332355840606]}
{:db/id #db/id[:db.part/user -5683305002900775255],
:community/name "Bar",
:community/url "http://localhost:8000/bar",
:community/category ["test" "drink" "alcohol"],
:community/orgtype :community.orgtype/commercial,
:community/type :community.type/facebook-page,
:community/neighborhood #db/id[:db.part/user -5749241506115709116]}
]
enum Region {
NE(':region/ne'), SW(':region/sw'), N(':region/n'), S(':region/s')
private final String v
Region(String v) {
this.v = v
}
String asKey() {v}
static Region random() {
def r = values()
return r.collect {it}[new Random().nextInt(r.size())]
}
}
enum CommunityType {
Twitter('twitter'), Facebook('facebook-page'), Wiki('wiki')
final String type
CommunityType(String type) {
this.type = type
}
String asKey(){":community.type/${type}"}
static CommunityType random() {
def c = values()
return c.collect{it}[new Random().nextInt(c.size())]
}
}
enum OrgType {
Community, Commercial
String asKey() {":community.orgtype/${this.toString().toLowerCase()}"}
static OrgType random() {
def o = values()
return o.collect {it}[new Random().nextInt(o.size())]
}
}
assert Region.random() in Region.values()
assert CommunityType.random() in CommunityType.values()
assert OrgType.random() in OrgType.values()
abstract class Entity {
static String DB_ID = ':db/id'
final long dbId = {
def id = new Random().nextLong()
id < 0 ? id : -id
}()
String toDbId() {
"#db/id[:db.part/user ${dbId}]"
}
}
class District extends Entity {
static String ENTITY = ':district'
String name
Region region
@Override String toString() {
def dq = '"'
$/{${DB_ID} ${toDbId()},
${ENTITY}/name $dq$name$dq,
${ENTITY}/region ${region.asKey()}}/$
}
}
class Neighborhood extends Entity {
static String ENTITY = ':neighborhood'
String name
District district
@Override String toString() {
def dq = '"'
$/{${DB_ID} ${toDbId()},
${ENTITY}/name $dq$name$dq,
${ENTITY}/district ${district.toDbId()}}/$
}
}
class Community extends Entity {
static String ENTITY = ':community'
static String DQ = '"'
String name
String url
List<String> categories
String category() {
"[${categories.collect {"$DQ$it$DQ"}.join(' ')}]"
}
OrgType orgType
CommunityType type
Neighborhood neighborhood
@Override String toString() {
$/{$DB_ID ${toDbId()},
$ENTITY/name $DQ$name$DQ,
$ENTITY/url $DQ$url$DQ,
$ENTITY/category ${category()},
$ENTITY/orgtype ${orgType.asKey()},
$ENTITY/type ${type.asKey()},
$ENTITY/neighborhood ${neighborhood.toDbId()}}/$
}
}
class Data {
def list = []
Data self = this
def community = {String cn ->
[url: {String cu ->
[category: {List<String> cc ->
[orgtype: {OrgType co ->
[type: {CommunityType ct ->
[neighbor: {String nn ->
[district: {String dn ->
[region: {Region rr ->
def district = new District(name: dn, region: rr)
def neighborhood = new Neighborhood(name: nn, district: district)
def community = new Community(name: cn, url: cu, categories: cc, orgType: co, type: ct, neighborhood: neighborhood)
list << [district: district, neighborhood: neighborhood, community: community]
}]
}]
}]
}]
}]
}]
}]
}
@Override String toString() {
$/[
${list.collect{
it.collect {
it.value.toString()
}
}.flatten().join('\n')}
]/$
}
}
def data = new Data()
data.community ('Foo') url ('http://localhost:8000/foo') category (['test']) orgtype (OrgType.Community) type (CommunityType.Twitter) neighbor ('Hoo') district ('Koo') region (Region.NE)
println data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment