Module: Ditto::Entity
- Defined in:
- lib/ditto/entity.rb
Constant Summary collapse
- PROPS =
[ :mandatory, :unique, :related ].freeze
Class Method Summary collapse
-
.check_definitions(verbose = 0) ⇒ Object
Check that the entities are well defined return true/false.
-
.define_entity(name, details) ⇒ Object
Maps to the ‘entity’ DSL keyword.
-
.dep_list(name, list, dephash) ⇒ Object
Compute dependencies recursively.
- .entity_count ⇒ Object
- .instance_count ⇒ Object
- .load_instance(name, instance) ⇒ Object
- .set_version(version) ⇒ Object
-
.store_all ⇒ Object
Before storing, sort the instances by dependency.
- .validate(name, instance) ⇒ Object
- .validate_instances(verbose = 0) ⇒ Object
Class Method Details
.check_definitions(verbose = 0) ⇒ Object
Check that the entities are well defined return true/false
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ditto/entity.rb', line 36 def self.check_definitions verbose = 0 return true if @entities.size == 0 nrel = 0 nkey = 0 puts "checking entities..." if verbose > 0 @entities.each do |name,fields| puts name if verbose > 0 @deps[name] = [] fields.each do |field, props| symprops = Array(props).select{|p| p.is_a? Symbol} duffprops = symprops - PROPS unless duffprops.empty? error("unknown properties: '#{duffprops.join(' ,')}'", name, @definitions[name]) end nkey += symprops.count(:unique) if symprops.include? :related nrel += 1 if @entities.has_key? field @deps[name] << field else error("unknown relationship to #{field}", name, @definitions[name]) end end end end puts "checked #{@entities.size} entities, #{nrel} relationships, #{@errors} errors" return error? end |
.define_entity(name, details) ⇒ Object
Maps to the ‘entity’ DSL keyword
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ditto/entity.rb', line 20 def self.define_entity name, details src = Thread.current.backtrace[2].split(':')[0..1] src[0] = File.basename(src[0]) unless Ditto::Options.instance.debug error("entity details must be a hash!", name, src) unless details.kind_of? Hash if @entities.has_key?(name.to_sym) error "duplicate definition of entity", name, src, "(was previously defined at #{@definitions[name.to_sym].join(':')})" end @entities[name.to_sym] = Ditto.symbolize_keys details @definitions[name.to_sym] = src end |
.dep_list(name, list, dephash) ⇒ Object
Compute dependencies recursively
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ditto/entity.rb', line 90 def self.dep_list(name, list, dephash) if deps = dephash[name] dephash.delete name deps.each do |dep| self.dep_list dep, list, dephash end list << name unless list.include? name else return if list.include? name # we did it earlier raise "Missing or circular dependency on #{name}" end end |
.entity_count ⇒ Object
126 127 128 |
# File 'lib/ditto/entity.rb', line 126 def self.entity_count return @entities.keys.size end |
.instance_count ⇒ Object
122 123 124 |
# File 'lib/ditto/entity.rb', line 122 def self.instance_count return @instances.keys.size end |
.load_instance(name, instance) ⇒ Object
65 66 67 |
# File 'lib/ditto/entity.rb', line 65 def self.load_instance name, instance @instances[name] << instance end |
.set_version(version) ⇒ Object
14 15 16 |
# File 'lib/ditto/entity.rb', line 14 def self.set_version version @version = version end |
.store_all ⇒ Object
Before storing, sort the instances by dependency
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ditto/entity.rb', line 105 def self.store_all seq = [] deps = @deps.dup # shallow copy as deleting entries... @deps.each_key do |entity| self.dep_list entity, seq, deps end return false if Ditto::Map.check_maps(seq, @definitions) > 0 n = Ditto::Map.add_all seq, @instances puts "stored #{@instances.keys.size} entities, #{n} instances" return true end |
.validate(name, instance) ⇒ Object
117 118 119 120 |
# File 'lib/ditto/entity.rb', line 117 def self.validate name, instance return true return false unless @entities.has_key?(name) end |
.validate_instances(verbose = 0) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ditto/entity.rb', line 69 def self.validate_instances verbose = 0 return true if instance_count == 0 puts "checking instances..." if verbose > 0 ninst = nerr = 0 @instances.each do |entity_name,entities| puts entity_name if verbose > 1 entities.each do |e| puts e.inspect if verbose > 1 if Ditto::Entity.validate(entity_name, e) ninst += 1 else nerr += 1 end end end puts "validated #{@instances.keys.size} entities, #{ninst} instances" return (nerr == 0) end |