Class: Amrita2::Core::RootSpec

Inherits:
DynamicElementSpec show all
Defined in:
lib/amrita2/core.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from DynamicElementSpec

#assigned_elements, #children, #cloned, #matcher, #option, #parent, #sym

Instance Method Summary collapse

Methods inherited from DynamicElementSpec

#check_double_id, #compile_element_main_code, #compile_element_sub_code, #dynamic_element, #get_dynamic_specs, #get_methods, #inspect, #make_default_template, #method_missing, #name_for, #output_stream

Constructor Details

#initialize(option = {}, &block) ⇒ RootSpec

Returns a new instance of RootSpec.



1017
1018
1019
1020
# File 'lib/amrita2/core.rb', line 1017

def initialize(option={}, &block)
  option[:key_attr_name] = :id unless option[:key_attr_name]
  super(nil, nil, option, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Amrita2::Core::DynamicElementSpec

Instance Method Details

#create_template_module(e, doc = nil) ⇒ Object



1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/amrita2/core.rb', line 1026

def create_template_module(e, doc=nil)
  m = Module.new
  result = create_template_module_text(e)
  @option[:debug_source].puts result  if @option[:debug_source]
  m.module_eval result
  if doc
    m.const_set(:XmlDecl, doc.xml_decl.to_s)
    m.const_set(:DocType, doc.doctype.to_s)
  end
  m
end

#create_template_module_text(e) ⇒ Object



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/amrita2/core.rb', line 1038

def create_template_module_text(e)
  specs = [self]

  traverse_and_search_match(specs, e)
  # not match check
  raise "SPEC #{specs.first.sym} was not matched" if specs.size > 0 

  cg = CodeGenerator.new
  cg.ie_hack = option[:ie_hack] 
  cg.eval_cdata_as_erb = option[:eval_cdata_as_erb] 
  cg.code("include Amrita2")
  cg.code("extend Amrita2")

   cg.define_method(:expand_template, :out, "&block") do
     define_expand_template(cg, e)
   end

  cg.define_module(module_name) do
    cg.code("Methods = #{get_methods.inspect}")
    cg.code("include Amrita2")
    cg.code("extend Amrita2")
    define_sub_modules(cg, e)
  end

  cg.define_method(:xml_decl) do
    cg.code("XmlDecl.dup")
  end

  cg.define_method(:doctype) do
    cg.code("DocType.dup")
  end

  cg.results.join("\n")
end

#define_expand_template(cg, e) ⇒ Object



1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/amrita2/core.rb', line 1073

def define_expand_template(cg, e)
  cg.code("new_context(out) do")
  cg.level_up do
    cg.code("block.call(#{module_name or 'nil'}) if block_given? ")
    e.amrita_compile_main_code(cg)
  end
  cg.code("current_context.mv_index.clear")
  cg.code("end")
end

#define_sub_modules(cg, e) ⇒ Object



1083
1084
1085
# File 'lib/amrita2/core.rb', line 1083

def define_sub_modules(cg, e)
  e.amrita_compile_sub_code(cg)
end

#generate_expander(mod, parent = nil) ⇒ Object



1130
1131
1132
# File 'lib/amrita2/core.rb', line 1130

def generate_expander(mod, parent=nil)
  Expander::Root.new(self, mod)
end

#generate_template(spec, element) ⇒ Object



1122
1123
1124
1125
1126
1127
1128
# File 'lib/amrita2/core.rb', line 1122

def generate_template(spec, element)
  src = spec.make_default_template
  doc = REXML::Document.new src
  doc.root.elements.each do |c|
    element.add(c)
  end
end

#module_nameObject



1022
1023
1024
# File 'lib/amrita2/core.rb', line 1022

def module_name
  "MainContext"
end

#traverse_and_search_match(child_specs, element) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/amrita2/core.rb', line 1087

def traverse_and_search_match(child_specs, element)
  spec = child_specs.first
  unless spec
    element.amrita_no_spec = true unless element.amrita_spec
    return false
  end
  if spec.matcher.match(element)
    element.amrita_no_spec = false
    element.amrita_spec = spec if spec.sym
    child_specs.shift unless spec.option[:match_many]
    specs = spec.children.collect do |s|
      s.get_dynamic_specs
    end.flatten
    if spec.option[:generate_template] and specs.size > 0 and element.elements.size == 0
      generate_template(spec, element)
    end
    traverse_and_search_match(specs, element)

    # not match check
    specs.each do |s|
      next if s.option[:match_many]
      next if s.option[:static_data]
      raise "SPEC #{s.sym} was not matched" 
    end
  else
    # check children has spec and set it to amrita_no_spec and return the result
    f = false                   
    element.elements.each do |ce|
      f |= traverse_and_search_match(child_specs, ce)
    end
    element.amrita_no_spec = (not f) 
    f
  end
end