Class: Amrita2::Core::DynamicElementSpec

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

Overview

:nodoc:

Direct Known Subclasses

RootSpec

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, parent, option = {}, &block) ⇒ DynamicElementSpec

Returns a new instance of DynamicElementSpec.



835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# File 'lib/amrita2/core.rb', line 835

def initialize(sym, parent, option={}, &block)
  case sym
  when Symbol
    @sym = sym
  when nil
    @sym = nil
  else
    @sym = sym.to_s.intern
  end
  @parent = parent
  @option, @children = option, []

  SpecOptionMeta::check_options(self)
  @direct_define = @option[:direct_define]
  setup_matcher
  setup_compiler
  if block_given?
    @in_setup = true
    self.instance_eval(&block) 
    @in_setup = false
  end

  check_double_id 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



893
894
895
896
897
898
899
900
901
902
903
904
905
906
# File 'lib/amrita2/core.rb', line 893

def method_missing(*args, &block)
  name = nil
  name = args.shift if args.first.kind_of?(Symbol)
  return super unless @in_setup
  if @direct_define 
    params = args[-1] || {}
    dynamic_element(name, params, &block)
  else
    sym = nil
    sym = args.shift if args.first.kind_of?(Symbol)
    SpecOptionMeta::define_element_ext(self, name , sym, args.first, &block) or 
      super
  end
end

Instance Attribute Details

#assigned_elementsObject (readonly)

Returns the value of attribute assigned_elements.



833
834
835
# File 'lib/amrita2/core.rb', line 833

def assigned_elements
  @assigned_elements
end

#childrenObject (readonly)

Returns the value of attribute children.



833
834
835
# File 'lib/amrita2/core.rb', line 833

def children
  @children
end

#clonedObject

Returns the value of attribute cloned.



832
833
834
# File 'lib/amrita2/core.rb', line 832

def cloned
  @cloned
end

#matcherObject (readonly)

Returns the value of attribute matcher.



833
834
835
# File 'lib/amrita2/core.rb', line 833

def matcher
  @matcher
end

#optionObject (readonly)

Returns the value of attribute option.



833
834
835
# File 'lib/amrita2/core.rb', line 833

def option
  @option
end

#parentObject

Returns the value of attribute parent.



832
833
834
# File 'lib/amrita2/core.rb', line 832

def parent
  @parent
end

#symObject (readonly)

Returns the value of attribute sym.



833
834
835
# File 'lib/amrita2/core.rb', line 833

def sym
  @sym
end

Instance Method Details

#check_double_idObject



987
988
989
990
991
992
993
994
# File 'lib/amrita2/core.rb', line 987

def check_double_id
  h = {}
  children.each do |c|
    next unless c.sym
    raise "dobule ID #{c.sym} in #{sym || 'ROOT'}" if h[c.sym] and not c.option[:dummy_element]
    h[c.sym] = true
  end
end

#compile_element_main_code(element, cg, &block) ⇒ Object



883
884
885
886
# File 'lib/amrita2/core.rb', line 883

def compile_element_main_code(element, cg, &block)
  return if @option[:dummy_element]
  @compiler.compile_main(element, self, cg, &block)
end

#compile_element_sub_code(element, cg, &block) ⇒ Object



888
889
890
891
# File 'lib/amrita2/core.rb', line 888

def compile_element_sub_code(element, cg, &block)
  return if @option[:dummy_element]
  @compiler.compile_sub(element, self, cg, &block)
end

#dynamic_element(name, opt = {}, cls = DynamicElementSpec, &block) ⇒ Object



875
876
877
878
879
880
# File 'lib/amrita2/core.rb', line 875

def dynamic_element(name, opt={}, cls=DynamicElementSpec, &block)
  SpecOptionMeta::inherit_option(self, opt)
  new_spec = cls.new(name, self, opt, &block)
  @children <<  new_spec
  new_spec
end

#generate_expander(mod, parent = nil) ⇒ Object



973
974
975
# File 'lib/amrita2/core.rb', line 973

def generate_expander(mod, parent=nil)
  @compiler.generate_expander(self, mod, parent)
end

#get_dynamic_specsObject



977
978
979
980
981
982
983
984
985
# File 'lib/amrita2/core.rb', line 977

def get_dynamic_specs
  if sym
    [self]
  else
    children.collect do |c|
      c.get_dynamic_specs
    end.flatten
  end
end

#get_methodsObject



916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/amrita2/core.rb', line 916

def get_methods
  h = {}
  ret = children.find_all do |c|
    ret = h[c.sym]
    h[c.sym] = true
    ret != true and c.sym
  end.collect {|c| c.sym }
  if ret.size == 0
    children.each do |c|
      ret.concat c.get_methods
    end
  end
  ret
end

#inspect(lvl = 0) ⇒ Object



860
861
862
863
864
865
866
867
868
869
# File 'lib/amrita2/core.rb', line 860

def inspect(lvl=0)
  spc = "  " * lvl
  if children.size > 0
    "#{spc}dynamic_element(#{sym.inspect}, #{@option.inspect}, #{self.class.name}) do\n" +
      children.collect {|c| c.inspect(lvl+1)} .join +
      "#{spc}end\n" 
  else
    "#{spc}dynamic_element(#{sym.inspect}, #{@option.inspect}, #{self.class.name})\n"
  end
end

#make_default_templateObject



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/amrita2/core.rb', line 931

def make_default_template
  tag = @option[:html_tag] 
  tag = 'div' if @option[:div] 
  unless sym or tag
    ret = children.collect do |c|
      c.make_default_template
    end.join('')
    return ret
  end

  attr = @option[:html_attr]
  a = nil
  if attr
    a =  attr.collect do |k,v| 
      "#{k}='#{v}'"
    end.join(' ')
  end
  if @option[:static_data]
    tag_and_attr = tag.to_s
    tag_and_attr += ' ' + a if attr and attr.size > 0
    "<#{tag_and_attr}>#{@option[:static_data]}</#{tag}>"
  else
    tag ||= 'span' 
    tag_and_attr = tag.to_s
    tag_and_attr += ' ' + a if attr and attr.size > 0
    if sym or tag
      idparam = ""
      idparam = " #{@option[:key_attr_name] }='#{sym}'" if sym
      tag = 'span' unless tag
      if children.size > 0
        "<#{tag_and_attr}#{idparam}>" +
          children.collect do |c|
          c.make_default_template
        end.join('') + "</#{tag}>"
      else
        "<#{tag_and_attr} #{idparam} />" 
      end
    else
    end
  end
end

#module_nameObject



908
909
910
# File 'lib/amrita2/core.rb', line 908

def module_name
  sym.to_s.capitalize  
end

#name_for(element) ⇒ Object



871
872
873
# File 'lib/amrita2/core.rb', line 871

def name_for(element)
  @sym
end

#output_streamObject



912
913
914
# File 'lib/amrita2/core.rb', line 912

def output_stream
  @option[:output_stream] or sym
end