Module: Engine2::Model
- Included in:
- E2Files
- Defined in:
- lib/engine2/model.rb
Instance Attribute Summary collapse
-
#after_destroy_processors ⇒ Object
readonly
Returns the value of attribute after_destroy_processors.
-
#after_save_processors ⇒ Object
readonly
Returns the value of attribute after_save_processors.
-
#before_destroy_processors ⇒ Object
readonly
Returns the value of attribute before_destroy_processors.
-
#before_save_processors ⇒ Object
readonly
Returns the value of attribute before_save_processors.
-
#dummies ⇒ Object
readonly
Returns the value of attribute dummies.
-
#many_to_many_associations ⇒ Object
readonly
, :one_to_one_associations.
-
#many_to_one_associations ⇒ Object
readonly
, :one_to_one_associations.
-
#one_to_many_associations ⇒ Object
readonly
, :one_to_one_associations.
-
#scheme_args ⇒ Object
readonly
Returns the value of attribute scheme_args.
-
#scheme_name ⇒ Object
readonly
Returns the value of attribute scheme_name.
-
#validation_in_transaction ⇒ Object
readonly
Returns the value of attribute validation_in_transaction.
Class Method Summary collapse
Instance Method Summary collapse
- #install_processors(processors) ⇒ Object
- #resolve_dependencies ⇒ Object
- #resolve_dependency(name, resolved, seen = []) ⇒ Object
- #scheme(s_name = :default, opts = nil, &blk) ⇒ Object
- #setup_schema ⇒ Object
- #synchronize_type_info ⇒ Object
- #type_info(&blk) ⇒ Object
Instance Attribute Details
#after_destroy_processors ⇒ Object (readonly)
Returns the value of attribute after_destroy_processors.
7 8 9 |
# File 'lib/engine2/model.rb', line 7 def after_destroy_processors @after_destroy_processors end |
#after_save_processors ⇒ Object (readonly)
Returns the value of attribute after_save_processors.
7 8 9 |
# File 'lib/engine2/model.rb', line 7 def after_save_processors @after_save_processors end |
#before_destroy_processors ⇒ Object (readonly)
Returns the value of attribute before_destroy_processors.
7 8 9 |
# File 'lib/engine2/model.rb', line 7 def before_destroy_processors @before_destroy_processors end |
#before_save_processors ⇒ Object (readonly)
Returns the value of attribute before_save_processors.
7 8 9 |
# File 'lib/engine2/model.rb', line 7 def before_save_processors @before_save_processors end |
#dummies ⇒ Object (readonly)
Returns the value of attribute dummies.
5 6 7 |
# File 'lib/engine2/model.rb', line 5 def dummies @dummies end |
#many_to_many_associations ⇒ Object (readonly)
, :one_to_one_associations
6 7 8 |
# File 'lib/engine2/model.rb', line 6 def many_to_many_associations @many_to_many_associations end |
#many_to_one_associations ⇒ Object (readonly)
, :one_to_one_associations
6 7 8 |
# File 'lib/engine2/model.rb', line 6 def many_to_one_associations @many_to_one_associations end |
#one_to_many_associations ⇒ Object (readonly)
, :one_to_one_associations
6 7 8 |
# File 'lib/engine2/model.rb', line 6 def one_to_many_associations @one_to_many_associations end |
#scheme_args ⇒ Object (readonly)
Returns the value of attribute scheme_args.
143 144 145 |
# File 'lib/engine2/model.rb', line 143 def scheme_args @scheme_args end |
#scheme_name ⇒ Object (readonly)
Returns the value of attribute scheme_name.
143 144 145 |
# File 'lib/engine2/model.rb', line 143 def scheme_name @scheme_name end |
#validation_in_transaction ⇒ Object (readonly)
Returns the value of attribute validation_in_transaction.
8 9 10 |
# File 'lib/engine2/model.rb', line 8 def validation_in_transaction @validation_in_transaction end |
Class Method Details
.extended(cls) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/engine2/model.rb', line 11 def extended cls # cls.dataset.row_proc = nil models = cls.db.models ||= {} raise E2Error.new("Model '#{cls.name}' already defined") if models[cls.name.to_sym] models[cls.name.to_sym] = cls cls.instance_eval do @many_to_one_associations = association_reflections.select{|n, a| a[:type] == :many_to_one} @one_to_many_associations = association_reflections.select{|n, a| a[:type] == :one_to_many} @many_to_many_associations = association_reflections.select{|n, a| a[:type] == :many_to_many} # @one_to_one_associations = association_reflections.select{|n, a| a[:type] == :one_to_one} @validation_in_transaction = nil @before_save_processors = nil @after_save_processors = nil @around_save_processors = nil @before_destroy_processors = nil @after_destroy_processors = nil @type_info_synchronized = nil end cls.setup_schema end |
Instance Method Details
#install_processors(processors) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/engine2/model.rb', line 34 def install_processors processors hash = {} type_info.each_pair do |name, info| proc = processors[info[:type]] hash[name] = proc if proc end hash.empty? ? nil : hash end |
#resolve_dependencies ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/engine2/model.rb', line 122 def resolve_dependencies resolved = {} @type_info.each_pair do |name, info| @validation_in_transaction ||= info[:transaction] resolve_dependency(name, resolved) end @type_info = resolved end |
#resolve_dependency(name, resolved, seen = []) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/engine2/model.rb', line 131 def resolve_dependency name, resolved, seen = [] seen << name deps = @type_info[name][:depends] deps.each do |e| if !resolved[e] raise "Circular dependency for field '#{name}' in model '#{self}'" if seen.include?(e) resolve_dependency(e, resolved, seen) end end if deps resolved[name] = @type_info[name] end |
#scheme(s_name = :default, opts = nil, &blk) ⇒ Object
145 146 147 148 149 |
# File 'lib/engine2/model.rb', line 145 def scheme s_name = :default, opts = nil, &blk @scheme_name = s_name @scheme_args = [name.to_sym, self, opts] SCHEMES::define_scheme name.to_sym, &blk end |
#setup_schema ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/engine2/model.rb', line 43 def setup_schema @type_info = {} @dummies = [] type_info do schema = @model.db_schema @model.primary_keys.each{|pk| (schema[pk]||={})[:primary_key] = true} if @model.primary_key schema.each_pair do |name, db_info| @info[name] = {otype: db_info[:type]} case db_info[:type] when :integer integer_field name when :string if db_info[:db_type] == 'text' string_field name, 10 else string_field name, Integer(db_info[:column_size] || db_info[:db_type][/\((\d+)\)/, 1]) end when :time time_field name, LOCS[:default_time_format], LOCS[:default_time_model_format] when :date date_field name, LOCS[:default_date_format], LOCS[:default_date_model_format] when :datetime datetime_field name, LOCS[:default_date_format], LOCS[:default_time_format], LOCS[:default_date_model_format], LOCS[:default_time_model_format] when :decimal size, scale = db_info[:column_size], db_info[:scale].to_i unless size && scale db_info[:db_type] =~ /decimal\((\d+),(\d+)\)/i size, scale = $1.to_i, $2.to_i raise E2Error.new("Cannot parse decimal type for #{db_info}") unless size || scale end decimal_field name, size, scale when :blob blob_field name, 100000 when nil # ignore nil type else p db_info raise E2Error.new("Unknown column type: #{db_info[:type].inspect} for #{name}") end required name if !db_info[:allow_null] primary_key name if db_info[:primary_key] sequence name, "SEQ_#{@model.table_name}.nextVal" if db_info[:primary_key] && !db_info[:allow_null] && !db_info[:auto_increment] && !@model.natural_key default name, db_info[:ruby_default] if db_info[:ruby_default] end unique *@model.primary_keys if @model.natural_key && @model.db.adapter_scheme # uri ? @model.many_to_one_associations.each do |aname, assoc| many_to_one_field aname decode assoc[:keys].first end end end |
#synchronize_type_info ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/engine2/model.rb', line 112 def synchronize_type_info resolve_dependencies @before_save_processors = install_processors(BeforeSaveProcessors) @after_save_processors = install_processors(AfterSaveProcessors) @around_save_processors = {} @before_destroy_processors = install_processors(BeforeDestroyProcessors) @after_destroy_processors = install_processors(AfterDestroyProcessors) @type_info_synchronized = true end |