Module: Rie::Model::ClassMethods
- Defined in:
- lib/rie/model.rb,
lib/rie/schema.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#base_attributes ⇒ Object
readonly
Returns the value of attribute base_attributes.
-
#datomic_name ⇒ Object
readonly
Returns the value of attribute datomic_name.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#partition ⇒ Object
readonly
Returns the value of attribute partition.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
- #attribute(attr, datomic_key = nil, opts = {}) ⇒ Object
- #base_attribute(key, val) ⇒ Object
- #changers(&b) ⇒ Object
- #connection ⇒ Object
- #create(&b) ⇒ Object
- #create!(&b) ⇒ Object
- #datomic_type ⇒ Object
- #datomic_type_key ⇒ Object
- #define_attribute(key, datomic_key, opts = {}) ⇒ Object
- #finder(db, constraints = []) ⇒ Object
- #finders(&b) ⇒ Object
- #get_attribute(key) ⇒ Object
- #install_base! ⇒ Object
- #install_schema! ⇒ Object
- #schema(name = nil, opts = {}, &b) ⇒ Object
- #transact(edn) ⇒ Object
- #uri(arg = nil) ⇒ Object
- #validation(&b) ⇒ Object
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
57 58 59 |
# File 'lib/rie/model.rb', line 57 def attributes @attributes end |
#base_attributes ⇒ Object (readonly)
Returns the value of attribute base_attributes.
63 64 65 |
# File 'lib/rie/model.rb', line 63 def base_attributes @base_attributes end |
#datomic_name ⇒ Object (readonly)
Returns the value of attribute datomic_name.
60 61 62 |
# File 'lib/rie/model.rb', line 60 def datomic_name @datomic_name end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
58 59 60 |
# File 'lib/rie/model.rb', line 58 def defaults @defaults end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
61 62 63 |
# File 'lib/rie/model.rb', line 61 def namespace @namespace end |
#partition ⇒ Object (readonly)
Returns the value of attribute partition.
62 63 64 |
# File 'lib/rie/model.rb', line 62 def partition @partition end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
59 60 61 |
# File 'lib/rie/model.rb', line 59 def validator @validator end |
Instance Method Details
#attribute(attr, datomic_key = nil, opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/rie/model.rb', line 91 def attribute(attr, datomic_key=nil, opts={}) if datomic_key.is_a? Hash opts = datomic_key datomic_key = nil end datomic_key ||= "#{self.namespace}.#{self.datomic_name}/#{attr.to_s.tr('_', '-')}" define_attribute(attr, datomic_key, opts) end |
#base_attribute(key, val) ⇒ Object
70 71 72 |
# File 'lib/rie/model.rb', line 70 def base_attribute(key, val) @base_attributes.merge!(key => val) end |
#changers(&b) ⇒ Object
127 128 129 |
# File 'lib/rie/model.rb', line 127 def changers(&b) self::Changer.class_eval(&b) end |
#connection ⇒ Object
79 80 81 |
# File 'lib/rie/model.rb', line 79 def connection Dalton::Connection.connect(uri) end |
#create(&b) ⇒ Object
143 144 145 |
# File 'lib/rie/model.rb', line 143 def create(&b) self::Changer.new(Dalton::Utility.tempid(partition), defaults).change(&b) end |
#create!(&b) ⇒ Object
139 140 141 |
# File 'lib/rie/model.rb', line 139 def create!(&b) self::Changer.new(Dalton::Utility.tempid(partition), defaults).change!(&b) end |
#datomic_type ⇒ Object
83 84 85 |
# File 'lib/rie/model.rb', line 83 def datomic_type :"#{namespace}.type/#{datomic_name}" end |
#datomic_type_key ⇒ Object
87 88 89 |
# File 'lib/rie/model.rb', line 87 def datomic_type_key :"#{namespace}/type" end |
#define_attribute(key, datomic_key, opts = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rie/model.rb', line 101 def define_attribute(key, datomic_key, opts={}) @attributes[key] = Attribute.new(self, key, opts.merge(datomic_attribute: datomic_key)) @defaults[key] = opts[:default] define_method(key) { self[key] } self::Finder::AttributeMethods.class_eval do define_method("by_#{key}") { |v| where(key => v) } end self::Changer::AttributeMethods.class_eval do define_method(key) { self[key] } define_method("#{key}=") { |v| self[key] = v } end end |
#finder(db, constraints = []) ⇒ Object
135 136 137 |
# File 'lib/rie/model.rb', line 135 def finder(db, constraints=[]) self::Finder.new(db).where(constraints) end |
#finders(&b) ⇒ Object
123 124 125 |
# File 'lib/rie/model.rb', line 123 def finders(&b) self::Finder.class_eval(&b) end |
#get_attribute(key) ⇒ Object
117 118 119 120 121 |
# File 'lib/rie/model.rb', line 117 def get_attribute(key) @attributes.fetch(key) do raise ArgumentError, "Undefined attribute #{key} for #{self}" end end |
#install_base! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rie/schema.rb', line 35 def install_base! transact <<-EDN [{:db/id #db/id[:db.part/db] :db/ident :#{partition} :db.install/_partition :db.part/db} {:db/id #db/id[:db.part/db] :db/ident :#{namespace}/type :db/valueType :db.type/ref :db/cardinality :db.cardinality/one :db/doc "A model's type" :db.install/_attribute :db.part/db}] EDN end |
#install_schema! ⇒ Object
30 31 32 33 |
# File 'lib/rie/schema.rb', line 30 def install_schema! raise ArgumentError.new("no schema defined for #{self}!") unless schema schema.install! end |
#schema(name = nil, opts = {}, &b) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rie/schema.rb', line 4 def schema(name=nil, opts={}, &b) return @schema unless block_given? if name.is_a? Hash opts = name name = nil end @datomic_name = name @datomic_name ||= self.name .gsub(/[^[:alpha:]]+/, '-') .gsub(/(?<=[[:lower:]])(?=[[:upper:]])/, '-') .downcase @namespace = opts.fetch(:namespace) { Model.namespace } \ or raise ArgumentError.new("no namespace configured for #{self} or globally") @partition = opts.fetch(:partition) { Model.partition } \ or raise ArgumentError.new("no partition configured for #{self} or globally") @partition = :"db.part/#{partition}" unless partition.to_s.start_with?('db.part/') Model.registry[datomic_type.to_s] = self base_attribute datomic_type_key, datomic_type @schema = Schema.new(self, &b) end |
#transact(edn) ⇒ Object
65 66 67 68 |
# File 'lib/rie/model.rb', line 65 def transact(edn) Model.logger.info("datomic.transact #{Dalton::Connection.convert_datoms(edn).to_edn}") connection.transact(edn) end |
#uri(arg = nil) ⇒ Object
74 75 76 77 |
# File 'lib/rie/model.rb', line 74 def uri(arg=nil) @uri = arg if arg @uri or Model.uri or raise "you must specify a datomic uri for #{self}" end |
#validation(&b) ⇒ Object
131 132 133 |
# File 'lib/rie/model.rb', line 131 def validation(&b) @validator.specify(&b) end |