Class: RSS::Element

Inherits:
Object
  • Object
show all
Extended by:
BaseModel, Utils::InheritedReader
Includes:
SetupMaker, Utils
Defined in:
lib/rss/rss.rb

Constant Summary collapse

INDENT =
"  "
MUST_CALL_VALIDATORS =
{}
MODELS =
[]
GET_ATTRIBUTES =
[]
HAVE_CHILDREN_ELEMENTS =
[]
TO_ELEMENT_METHODS =
[]
NEED_INITIALIZE_VARIABLES =
[]
PLURAL_FORMS =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModel

install_date_element, install_have_child_element, install_have_children_element, install_text_element

Methods included from Utils

element_initialize_arguments?, get_file_and_line_from_caller, html_escape, new_with_value_if_need, to_class_name

Methods included from Utils::InheritedReader

inherited_array_reader, inherited_hash_reader, inherited_reader

Methods included from SetupMaker

#setup_maker

Constructor Details

#initialize(do_validate = true, attrs = nil) ⇒ Element

Returns a new instance of Element.



856
857
858
859
860
861
862
863
864
# File 'lib/rss/rss.rb', line 856

def initialize(do_validate=true, attrs=nil)
  @parent = nil
  @converter = nil
  if attrs.nil? and (do_validate.is_a?(Hash) or do_validate.is_a?(Array))
    do_validate, attrs = true, do_validate
  end
  @do_validate = do_validate
  initialize_variables(attrs || {})
end

Instance Attribute Details

#do_validateObject

Returns the value of attribute do_validate.



854
855
856
# File 'lib/rss/rss.rb', line 854

def do_validate
  @do_validate
end

#parentObject

Returns the value of attribute parent.



854
855
856
# File 'lib/rss/rss.rb', line 854

def parent
  @parent
end

Class Method Details

.add_have_children_element(variable_name, plural_name) ⇒ Object



814
815
816
# File 'lib/rss/rss.rb', line 814

def add_have_children_element(variable_name, plural_name)
  self::HAVE_CHILDREN_ELEMENTS << [variable_name, plural_name]
end

.add_need_initialize_variable(variable_name) ⇒ Object



822
823
824
# File 'lib/rss/rss.rb', line 822

def add_need_initialize_variable(variable_name)
  self::NEED_INITIALIZE_VARIABLES << variable_name
end

.add_plural_form(singular, plural) ⇒ Object



826
827
828
# File 'lib/rss/rss.rb', line 826

def add_plural_form(singular, plural)
  self::PLURAL_FORMS[singular] = plural
end

.add_to_element_method(method_name) ⇒ Object



818
819
820
# File 'lib/rss/rss.rb', line 818

def add_to_element_method(method_name)
  self::TO_ELEMENT_METHODS << method_name
end

.content_setup(type = nil, disp_name = nil) ⇒ Object



803
804
805
806
807
808
# File 'lib/rss/rss.rb', line 803

def content_setup(type=nil, disp_name=nil)
  writer_type, reader_type = type
  def_corresponded_attr_writer :content, writer_type, disp_name
  def_corresponded_attr_reader :content, reader_type
  @have_content = true
end

.def_corresponded_attr_reader(name, type = nil) ⇒ Object



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# File 'lib/rss/rss.rb', line 784

def def_corresponded_attr_reader(name, type=nil)
  case type
  when :inherit
    inherit_convert_attr_reader name
  when :uri
    uri_convert_attr_reader name
  when :yes_clean_other
    yes_clean_other_attr_reader name
  when :yes_other
    yes_other_attr_reader name
  when :csv
    csv_attr_reader name
  when :csv_integer
    csv_attr_reader name, :separator => ","
  else
    convert_attr_reader name
  end
end

.def_corresponded_attr_writer(name, type = nil, disp_name = nil) ⇒ Object



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/rss/rss.rb', line 756

def def_corresponded_attr_writer(name, type=nil, disp_name=nil)
  disp_name ||= name
  case type
  when :integer
    integer_writer name, disp_name
  when :positive_integer
    positive_integer_writer name, disp_name
  when :boolean
    boolean_writer name, disp_name
  when :w3cdtf, :rfc822, :rfc2822
    date_writer name, type, disp_name
  when :text_type
    text_type_writer name, disp_name
  when :content
    content_writer name, disp_name
  when :yes_clean_other
    yes_clean_other_writer name, disp_name
  when :yes_other
    yes_other_writer name, disp_name
  when :csv
    csv_writer name
  when :csv_integer
    csv_integer_writer name
  else
    attr_writer name
  end
end

.get_attributesObject



693
694
695
# File 'lib/rss/rss.rb', line 693

def get_attributes
  inherited_array_reader("GET_ATTRIBUTES")
end

.have_children_elementsObject



696
697
698
# File 'lib/rss/rss.rb', line 696

def have_children_elements
  inherited_array_reader("HAVE_CHILDREN_ELEMENTS")
end

.have_content?Boolean

Returns:

  • (Boolean)


810
811
812
# File 'lib/rss/rss.rb', line 810

def have_content?
  @have_content
end

.inherited(klass) ⇒ Object



713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/rss/rss.rb', line 713

def inherited(klass)
  klass.const_set(:MUST_CALL_VALIDATORS, {})
  klass.const_set(:MODELS, [])
  klass.const_set(:GET_ATTRIBUTES, [])
  klass.const_set(:HAVE_CHILDREN_ELEMENTS, [])
  klass.const_set(:TO_ELEMENT_METHODS, [])
  klass.const_set(:NEED_INITIALIZE_VARIABLES, [])
  klass.const_set(:PLURAL_FORMS, {})

  tag_name = klass.name.split(/::/).last
  tag_name[0, 1] = tag_name[0, 1].downcase
  klass.instance_variable_set(:@tag_name, tag_name)
  klass.instance_variable_set(:@have_content, false)
end

.inherited_baseObject



709
710
711
# File 'lib/rss/rss.rb', line 709

def inherited_base
  ::RSS::Element
end

.install_get_attribute(name, uri, required = true, type = nil, disp_name = nil, element_name = nil) ⇒ Object



741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/rss/rss.rb', line 741

def install_get_attribute(name, uri, required=true,
                          type=nil, disp_name=nil,
                          element_name=nil)
  disp_name ||= name
  element_name ||= name
  writer_type, reader_type = type
  def_corresponded_attr_writer name, writer_type, disp_name
  def_corresponded_attr_reader name, reader_type
  if type == :boolean and /^is/ =~ name
    alias_method "#{$POSTMATCH}?", name
  end
  self::GET_ATTRIBUTES << [name, uri, required, element_name]
  add_need_initialize_variable(disp_name)
end

.install_model(tag, uri, occurs = nil, getter = nil, plural = false) ⇒ Object



732
733
734
735
736
737
738
739
# File 'lib/rss/rss.rb', line 732

def install_model(tag, uri, occurs=nil, getter=nil, plural=false)
  getter ||= tag
  if m = self::MODELS.find {|t, u, o, g, p| t == tag and u == uri}
    m[2] = occurs
  else
    self::MODELS << [tag, uri, occurs, getter, plural]
  end
end

.install_must_call_validator(prefix, uri) ⇒ Object



728
729
730
# File 'lib/rss/rss.rb', line 728

def install_must_call_validator(prefix, uri)
  self::MUST_CALL_VALIDATORS[uri] = prefix
end

.install_ns(prefix, uri) ⇒ Object



842
843
844
845
846
847
# File 'lib/rss/rss.rb', line 842

def install_ns(prefix, uri)
  if self::NSPOOL.has_key?(prefix)
    raise OverlappedPrefixError.new(prefix)
  end
  self::NSPOOL[prefix] = uri
end

.modelsObject



690
691
692
# File 'lib/rss/rss.rb', line 690

def models
  inherited_array_reader("MODELS")
end

.must_call_validatorsObject



687
688
689
# File 'lib/rss/rss.rb', line 687

def must_call_validators
  inherited_hash_reader("MUST_CALL_VALIDATORS")
end

.need_initialize_variablesObject



702
703
704
# File 'lib/rss/rss.rb', line 702

def need_initialize_variables
  inherited_array_reader("NEED_INITIALIZE_VARIABLES")
end

.need_parent?Boolean

Returns:

  • (Boolean)


838
839
840
# File 'lib/rss/rss.rb', line 838

def need_parent?
  false
end

.plural_formsObject



705
706
707
# File 'lib/rss/rss.rb', line 705

def plural_forms
  inherited_hash_reader("PLURAL_FORMS")
end

.required_prefixObject



830
831
832
# File 'lib/rss/rss.rb', line 830

def required_prefix
  nil
end

.required_uriObject



834
835
836
# File 'lib/rss/rss.rb', line 834

def required_uri
  ""
end

.tag_nameObject



849
850
851
# File 'lib/rss/rss.rb', line 849

def tag_name
  @tag_name
end

.to_element_methodsObject



699
700
701
# File 'lib/rss/rss.rb', line 699

def to_element_methods
  inherited_array_reader("TO_ELEMENT_METHODS")
end

Instance Method Details

#convert(value) ⇒ Object



885
886
887
888
889
890
891
# File 'lib/rss/rss.rb', line 885

def convert(value)
  if @converter
    @converter.convert(value)
  else
    value
  end
end

#converter=(converter) ⇒ Object



874
875
876
877
878
879
880
881
882
883
# File 'lib/rss/rss.rb', line 874

def converter=(converter)
  @converter = converter
  targets = children.dup
  self.class.have_children_elements.each do |variable_name, plural_name|
    targets.concat(__send__(plural_name))
  end
  targets.each do |target|
    target.converter = converter unless target.nil?
  end
end

#full_nameObject



870
871
872
# File 'lib/rss/rss.rb', line 870

def full_name
  tag_name
end

#have_xml_content?Boolean

Returns:

  • (Boolean)


935
936
937
# File 'lib/rss/rss.rb', line 935

def have_xml_content?
  false
end

#need_base64_encode?Boolean

Returns:

  • (Boolean)


939
940
941
# File 'lib/rss/rss.rb', line 939

def need_base64_encode?
  false
end

#set_next_element(tag_name, next_element) ⇒ Object



943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/rss/rss.rb', line 943

def set_next_element(tag_name, next_element)
  klass = next_element.class
  prefix = ""
  prefix << "#{klass.required_prefix}_" if klass.required_prefix
  key = "#{prefix}#{tag_name.gsub(/-/, '_')}"
  if self.class.plural_forms.has_key?(key)
    ary = __send__("#{self.class.plural_forms[key]}")
    ary << next_element
  else
    __send__("#{key}=", next_element)
  end
end

#tag_nameObject



866
867
868
# File 'lib/rss/rss.rb', line 866

def tag_name
  self.class.tag_name
end

#to_s(need_convert = true, indent = '') ⇒ Object



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'lib/rss/rss.rb', line 914

def to_s(need_convert=true, indent='')
  if self.class.have_content?
    return "" if !empty_content? and !content_is_set?
    rv = tag(indent) do |next_indent|
      if empty_content?
        ""
      else
        xmled_content
      end
    end
  else
    rv = tag(indent) do |next_indent|
      self.class.to_element_methods.collect do |method_name|
        __send__(method_name, false, next_indent)
      end
    end
  end
  rv = convert(rv) if need_convert
  rv
end

#valid?(ignore_unknown_element = true) ⇒ Boolean

Returns:

  • (Boolean)


893
894
895
896
897
898
# File 'lib/rss/rss.rb', line 893

def valid?(ignore_unknown_element=true)
  validate(ignore_unknown_element)
  true
rescue RSS::Error
  false
end

#validate(ignore_unknown_element = true) ⇒ Object



900
901
902
903
904
905
906
907
# File 'lib/rss/rss.rb', line 900

def validate(ignore_unknown_element=true)
  do_validate = @do_validate
  @do_validate = true
  validate_attribute
  __validate(ignore_unknown_element)
ensure
  @do_validate = do_validate
end

#validate_for_stream(tags, ignore_unknown_element = true) ⇒ Object



909
910
911
912
# File 'lib/rss/rss.rb', line 909

def validate_for_stream(tags, ignore_unknown_element=true)
  validate_attribute
  __validate(ignore_unknown_element, tags, false)
end