Class: Rie::Schema

Inherits:
Object
  • Object
show all
Includes:
Dalton::Utility
Defined in:
lib/rie/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, &block) ⇒ Schema

Returns a new instance of Schema.



56
57
58
59
60
61
# File 'lib/rie/schema.rb', line 56

def initialize(model, &block)
  @model = model
  @transactions = []
  declare_type
  instance_exec(&block)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



55
56
57
# File 'lib/rie/schema.rb', line 55

def model
  @model
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



55
56
57
# File 'lib/rie/schema.rb', line 55

def transactions
  @transactions
end

Instance Method Details

#attribute(attr_key, opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rie/schema.rb', line 91

def attribute(attr_key, opts={})
  config = {
    :'db/id' => opts.fetch(:id) { Peer.tempid(kw('db.part/db')) },
    :'db/ident' => kw(opts.fetch(:ident) { key(model.datomic_name, attr_key) }),
    :'db/valueType' => :"db.type/#{opts.fetch(:value_type)}",
    :'db/cardinality' => :"db.cardinality/#{opts.fetch(:cardinality, :one)}",
    :'db/doc' => opts.fetch(:doc) { "The #{attr_key} attribute" },
    :'db.install/_attribute' => :'db.part/db',
  }

  config[:'db/unique'] = :"db.unique/#{opts[:unique]}" if opts[:unique]

  edn(config)
end

#declare_typeObject



83
84
85
# File 'lib/rie/schema.rb', line 83

def declare_type
  edn [:'db/add', Peer.tempid(kw(partition)), :'db/ident', key(:type, name)]
end

#edn(edn) ⇒ Object



87
88
89
# File 'lib/rie/schema.rb', line 87

def edn(edn)
  @transactions << edn
end

#install!Object



106
107
108
109
110
# File 'lib/rie/schema.rb', line 106

def install!
  transactions.each do |t|
    model.transact([t])
  end
end

#key(key, subkey = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rie/schema.rb', line 75

def key(key, subkey=nil)
  if subkey
    :"#{namespace}.#{key}/#{subkey}"
  else
    :"#{namespace}/#{key}"
  end
end

#nameObject



63
64
65
# File 'lib/rie/schema.rb', line 63

def name
  model.datomic_name
end

#namespaceObject



71
72
73
# File 'lib/rie/schema.rb', line 71

def namespace
  model.namespace
end

#partitionObject



67
68
69
# File 'lib/rie/schema.rb', line 67

def partition
  model.partition
end