Class: Amrita2::Core::DynamicElement

Inherits:
ParentNode show all
Includes:
Util, OptionSupport
Defined in:
lib/amrita2/gettext.rb,
lib/amrita2/template.rb

Overview

:nodoc: all

Direct Known Subclasses

RootElement

Constant Summary

Constants included from Util

Util::AMP_WITHOUT_REFRENCE, Util::DefaultAllowedScheme, Util::NAME, Util::NAMECHAR, Util::NOT_REFERENCE, Util::UrlInvalidChar

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

sanitize_attribute_value, sanitize_text, sanitize_url

Methods inherited from ParentNode

#each, #has_dynamic?

Methods inherited from BaseNode

#root

Constructor Details

#initialize(parent, name, element, filters = []) ⇒ DynamicElement

Returns a new instance of DynamicElement.



907
908
909
910
911
912
913
914
915
916
917
918
# File 'lib/amrita2/template.rb', line 907

def initialize(parent, name, element, filters=[])
  @parent,@name,@element = parent, name, element
  parse_opt(root.opt)
  @attrs = {}
  delete_am_attrs
  setup_filter(filters)
  @element = filter_element(@element)
  @renderers = []
  super(parent, element)

  setup_type_renderer
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def attrs
  @attrs
end

#childrenObject (readonly)

Returns the value of attribute children.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def children
  @children
end

#elementObject (readonly)

Returns the value of attribute element.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def element
  @element
end

#filterObject (readonly)

Returns the value of attribute filter.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def filter
  @filter
end

#nameObject (readonly)

Returns the value of attribute name.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def parent
  @parent
end

#renderersObject (readonly)

Returns the value of attribute renderers.



894
895
896
# File 'lib/amrita2/template.rb', line 894

def renderers
  @renderers
end

Class Method Details

.delegate_to_filter(name) ⇒ Object



895
896
897
898
899
900
901
# File 'lib/amrita2/template.rb', line 895

def self.delegate_to_filter(name)
  module_eval <<-END
  def #{name}(*args,&block)
    @filter.#{name}(self, *args,&block)
  end
  END
end

Instance Method Details

#[](key) ⇒ Object



920
921
922
# File 'lib/amrita2/template.rb', line 920

def [](key)
  @attrs[key]
end

#am_for_valueObject



992
993
994
# File 'lib/amrita2/template.rb', line 992

def am_for_value
  @attrs[am_for.intern]
end

#am_skipif_valueObject



996
997
998
# File 'lib/amrita2/template.rb', line 996

def am_skipif_value
  @attrs[am_skipif.intern]
end

#am_v_valueObject



1000
1001
1002
# File 'lib/amrita2/template.rb', line 1000

def am_v_value
  @attrs[am_v.intern]
end

#class_nameObject



952
953
954
955
956
957
958
# File 'lib/amrita2/template.rb', line 952

def class_name
  if name
    "XX" + name.capitalize
  else
    "XX%d" % [object_id.abs]
  end
end

#compile(cg = CodeGenerator.new) ⇒ Object



964
965
966
967
968
969
970
# File 'lib/amrita2/template.rb', line 964

def compile(cg = CodeGenerator.new)
  define_element_method(cg) do
    cg.code("$_ = value")
    loop_check_code(cg)
    method_body_code(cg)
  end
end

#dynamic?Boolean

Returns:

  • (Boolean)


928
929
930
# File 'lib/amrita2/template.rb', line 928

def dynamic?
  true
end

#get_erb_sourceObject



21
22
23
24
25
# File 'lib/amrita2/gettext.rb', line 21

def get_erb_source
  @children.collect do |c|
    c.get_erb_source
  end.join("\n")
end

#has_ruby?Boolean

Returns:

  • (Boolean)


932
933
934
# File 'lib/amrita2/template.rb', line 932

def has_ruby?
  super or am_for_value or am_skipif_value or am_v_value
end

#instance_nameObject



960
961
962
# File 'lib/amrita2/template.rb', line 960

def instance_name
  class_name + "Instance"
end

#module_src(cg) ⇒ Object



980
981
982
983
984
985
986
987
988
989
990
# File 'lib/amrita2/template.rb', line 980

def module_src(cg)
  cg.define_class(class_name) do
    cg.code <<-END
    include Amrita2
    include Amrita2::Runtime
  END
    compile(cg)
    super
  end
  cg.code("#{instance_name} = #{class_name}.new")
end

#parent_deObject



924
925
926
# File 'lib/amrita2/template.rb', line 924

def parent_de
  self
end

#render_me(cg, n = name) ⇒ Object



972
973
974
975
976
977
978
# File 'lib/amrita2/template.rb', line 972

def render_me(cg, n = name)
  if (n)
    cg.put_string_expression("#{instance_name}.render_with($_.amrita_value(:#{n}), __binding__)")
  else
    cg.put_string_expression("#{instance_name}.render_with($_, __binding__)")
  end
end

#setup_filter(filters) ⇒ Object



936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/amrita2/template.rb', line 936

def setup_filter(filters)
  #default_filter = @parent ? @parent.filter : DefaultFilter.new
  default_filter =  DefaultFilter.new
  default_filter.parse_opt(opt)
  if filters.size > 0
    @filter = current = filters.first
    for f in filters do
      current.next_ = f
      current = f
    end
    current.next_ = default_filter
  else
    @filter = default_filter
  end
end