Module: Tanuki::MetaModelBehavior
- Included in:
- Tanuki_MetaModel
- Defined in:
- lib/tanuki/behavior/meta_model_behavior.rb
Overview
Tanuki::MetaModelBehavior contains all methods for the meta-model. In is included in the meta-model class.
Instance Method Summary collapse
-
#class_name_for(class_type) ⇒ Object
Returns class name for a given class type.
-
#initialize(namespace, name, data, models) ⇒ Object
Creates new meta-model
nameinnamespace. -
#key ⇒ Object
Returns an array of code for alias-column name pair.
-
#process! ⇒ Object
Prepares data for template generation.
- #process_joins! ⇒ Object
- #process_key! ⇒ Object
-
#process_relations! ⇒ Object
Prepares data for template generation.
-
#process_source! ⇒ Object
Extracts the model firts-source information form the YAML @data and performs.
-
#qualified_name(field_name) ⇒ Object
Returns code for alias-column name pair for field
field_name.
Instance Method Details
#class_name_for(class_type) ⇒ Object
Returns class name for a given class type.
18 19 20 21 22 23 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 18 def class_name_for(class_type) case class_type when :model, :model_base then "#{@namespace}_Model_#{@name}" when :manager, :manager_base then "#{@namespace}_Manager_#{@name}" end end |
#initialize(namespace, name, data, models) ⇒ Object
Creates new meta-model name in namespace. Model schema is passed as data. Stucture models contains all models being generated.
10 11 12 13 14 15 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 10 def initialize(namespace, name, data, models) @namespace = namespace @name = name @data = data @models = models end |
#key ⇒ Object
Returns an array of code for alias-column name pair.
26 27 28 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 26 def key @key.inspect end |
#process! ⇒ Object
Prepares data for template generation. Processes own keys, fields, etc.
32 33 34 35 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 32 def process! process_source! process_key! end |
#process_joins! ⇒ Object
63 64 65 66 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 63 def process_joins! @joins = {} @joins[@first_source] = nil end |
#process_key! ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 37 def process_key! @key = @source['key'] || 'id' @key = [@key] if @key.is_a? String raise 'invalid key' unless @key.is_a? Array @key.map! do |k| parts = k.split('.').map {|p| p.to_sym } raise "invalid key field #{k}" if parts.count > 2 if parts.count == 2 raise 'all key fields should belong to the first-source' if parts[0] != @first_source.to_s parts else [@first_source, parts[0]] end end end |
#process_relations! ⇒ Object
Prepares data for template generation. Processes foreign keys, fields, etc.
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 101 102 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 70 def process_relations! joins = @source['joins'] || {} joins = [joins] if joins.is_a? String joins = Hash[*joins.collect {|v| [v, nil] }.flatten] if joins.is_a? Array if joins.is_a? Hash joins.each_pair do |table_alias, join| table_alias = table_alias.to_sym raise "#{table_alias} is already in use" if @joins.include? table_alias if join if join['on'].is_a Hash table_name = join['table'] || table_alias on = join['on'] else on = join table_name = table_alias end else on = nil table_name = table_alias end if on else on = {} @key.each do |k| on[[table_alias, @first_source.to_s.singularize.to_sym]] = [] # TODO choose a right priciple end end end else raise "`joins' should be either nil or string or array or hash" end end |
#process_source! ⇒ Object
Extracts the model firts-source information form the YAML @data and performs
55 56 57 58 59 60 61 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 55 def process_source! guess_table = @name.pluralize @data ||= {} @source = @data['source'] || guess_table @source = {'table' => @source} if @source.is_a? String @first_source = (@source['table'] || guess_table).downcase.to_sym end |
#qualified_name(field_name) ⇒ Object
Returns code for alias-column name pair for field field_name.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tanuki/behavior/meta_model_behavior.rb', line 105 def qualified_name(field_name) parts = field_name.split('.') if parts.length == 1 "%w{:#{@first_source} :#{field_name}}" elsif parts.length == 2 "%w{:#{parts[0]} :#{parts[1]}}" else raise "field name for model #{@namespace}.#{@name} is invalid" end end |