Class: Atom::CoreElement

Inherits:
RootElement show all
Defined in:
lib/atomutil.rb

Direct Known Subclasses

Entry, Feed

Instance Attribute Summary

Attributes inherited from Element

#elem

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#add, element_attr_accessor, element_attr_accessors, element_datetime_accessor, element_datetime_accessors, element_name, element_ns, element_object_list_accessor, element_text_accessor, element_text_accessors, element_text_list_accessor, #get, #get_attr, #get_object, #get_objects, #getlist, new, ns, #set, #set_attr, #to_s

Constructor Details

#initialize(params = {}) ⇒ CoreElement

Returns a new instance of CoreElement.



906
907
908
909
910
911
912
913
914
# File 'lib/atomutil.rb', line 906

def initialize(params={})
  if params.has_key?(:uri) || params.has_key?(:file)
    target = params.has_key?(:uri)         ? URI.parse(params.delete(:uri)) \
           : params[:file].is_a?(Pathname) ? params.delete(:file) \
           :                                 Pathname.new(params.delete(:file))
    params[:stream] = target.open { |f| f.read }
  end
  super(params)
end

Class Method Details



849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/atomutil.rb', line 849

def self.element_link_accessor(type)
  type = type.to_s
  meth_name = [type.tr('-','_'), 'link'].join('_')
  class_eval("\n    def \#{meth_name}\n      selected = links.select{ |l| l.rel == '\#{type}' }\n      selected.empty? ? nil : selected.first.href\n    end\n\n    def \#{meth_name}s\n      links.select{ |l| l.rel == '\#{type}' }.collect{ |l| l.href }\n    end\n\n    def add_\#{meth_name}(href)\n      l = Link.new\n      l.href = href\n      l.rel = '\#{type}'\n      add_link l\n    end\n\n    def \#{meth_name}=(href)\n      xpath = child_xpath(Namespace::ATOM, 'link', { :rel => '\#{type}' })\n      @elem.elements.delete_all(xpath)\n      add_\#{meth_name}(href)\n    end\n  EOS\nend\n", __FILE__, __LINE__)


878
879
880
# File 'lib/atomutil.rb', line 878

def self.element_link_accessors(*types)
  types.flatten.each{ |type| element_link_accessor(type) }
end

Instance Method Details



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

def add_alternate_link(href)
  l = Link.new
  l.href = href
  l.rel = 'alternate'
  add_link l
end


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

def alternate_link
  alternates = links.select{ |l| l.rel.nil? || l.rel == 'alternate' }
  alternates.empty? ? nil : alternates.first.href
end

#alternate_link=(href) ⇒ Object



900
901
902
903
904
# File 'lib/atomutil.rb', line 900

def alternate_link=(href)
  xpath = child_xpath(Namespace::ATOM, 'link', { :rel => 'alternate' })
  @elem.elements.delete_all(xpath)
  add_alternate_link(href)
end


884
885
886
# File 'lib/atomutil.rb', line 884

def alternate_links
  links.select{ |l| l.rel.nil? || l.rel == 'alternate' }.collect{ |l| l.href }
end