123456789_123456789_123456789_123456789_123456789_

Module: RSS::Utils

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Extended In:
Atom::Entry, Atom::Feed, Atom::Feed::Author, Atom::Feed::Category, Atom::Feed::Contributor, Atom::Feed::Entry, Atom::Feed::Entry::Content, Atom::Feed::Entry::Published, Atom::Feed::Entry::Source, Atom::Feed::Entry::Summary, Atom::Feed::Generator, Atom::Feed::Icon, Atom::Feed::Id, Atom::Feed::Link, Atom::Feed::Logo, Atom::Feed::Rights, Atom::Feed::Subtitle, Atom::Feed::Title, Atom::Feed::Updated, Atom::PersonConstruct::Email, Atom::PersonConstruct::Name, Atom::PersonConstruct::Uri, BaseListener, ContentModel, DublinCoreModel, Element, ITunesBaseModel, ITunesChannelModel, ITunesChannelModel::ITunesCategory, ITunesChannelModel::ITunesImage, ITunesChannelModel::ITunesOwner, ITunesItemModel, ITunesItemModel::ITunesDuration, ImageFaviconModel, ImageFaviconModel::ImageFavicon, ImageItemModel, ImageItemModel::ImageItem, RDF, RDF::Bag, RDF::Channel, RDF::Channel::Image, RDF::Channel::Items, RDF::Channel::Textinput, RDF::Image, RDF::Item, RDF::Li, RDF::Seq, RDF::Textinput, REXMLListener, Rss, Rss::Channel, Rss::Channel::Cloud, Rss::Channel::Image, Rss::Channel::Item, Rss::Channel::Item::Category, Rss::Channel::Item::Enclosure, Rss::Channel::Item::Guid, Rss::Channel::Item::Source, Rss::Channel::SkipDays, Rss::Channel::SkipDays::Day, Rss::Channel::SkipHours, Rss::Channel::SkipHours::Hour, Rss::Channel::TextInput, SlashModel, SyndicationModel, TaxonomyTopicModel, TaxonomyTopicModel::TaxonomyTopic, TaxonomyTopicsModel, TaxonomyTopicsModel::TaxonomyTopics, TrackBackModel10, TrackBackModel10::TrackBackAbout, TrackBackModel10::TrackBackPing, TrackBackModel20, TrackBackModel20::TrackBackAbout, TrackBackModel20::TrackBackPing, XMLParserListener, XMLScanListener
Included In:
Defined in: lib/rss/utils.rb

Overview

Utils is a module that holds various utility functions that are used across many parts of the rest of the ::RSS library. Like most modules named some variant of ‘util’, its methods are probably not particularly useful to those who aren’t developing the library itself.

Class Method Summary

Class Method Details

.element_initialize_arguments?(args) ⇒ Boolean (mod_func)

This method is used inside of several different objects to determine if special behavior is needed in the constructor.

Special behavior is needed if the array passed in as args has true or false as its value, and if the second element of args is a hash.

[ GitHub ]

  
# File 'lib/rss/utils.rb', line 124

def element_initialize_arguments?(args)
  [true, false].include?(args[0]) and args[1].is_a?(Hash)
end

.get_file_and_line_from_caller(i = 0) (mod_func)

Returns an array of two elements: the filename where the calling method is located, and the line number where it is defined.

Takes an optional argument i, which specifies how many callers up the stack to look.

Examples:

require 'rss/utils'

def foo
  p RSS::Utils.get_file_and_line_from_caller
  p RSS::Utils.get_file_and_line_from_caller(1)
end

def bar
  foo
end

def baz
  bar
end

baz
# => ["test.rb", 5]
# => ["test.rb", 9]

If i is not given, or is the default value of 0, it attempts to figure out the correct value. This is useful when in combination with instance_eval. For example:

require 'rss/utils'

def foo
  p RSS::Utils.get_file_and_line_from_caller(1)
end

def bar
  foo
end

instance_eval <<-RUBY, *RSS::Utils.get_file_and_line_from_caller
def baz
  bar
end
RUBY

baz

# => ["test.rb", 8]
[ GitHub ]

  
# File 'lib/rss/utils.rb', line 83

def get_file_and_line_from_caller(i=0)
  file, line, = caller[i].split(':')
  line = line.to_i
  line += 1 if i.zero?
  [file, line]
end

.h(s) (mod_func)

Alias for #html_escape.

[ GitHub ]

  
# File 'lib/rss/utils.rb', line 106

alias h html_escape

.html_escape(s) (mod_func) Also known as: .h

Takes a string s with some HTML in it, and escapes ‘&’, ‘“’, ‘<’ and ‘>’, by replacing them with the appropriate entities.

This method is also aliased to h, for convenience.

Examples:

require 'rss/utils'

RSS::Utils.html_escape("Dungeons & Dragons")
# => "Dungeons &amp; Dragons"
RSS::Utils.h(">_>")
# => "&gt;_&gt;"
[ GitHub ]

  
# File 'lib/rss/utils.rb', line 103

def html_escape(s)
  s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
end

.new_with_value_if_need(klass, value) (mod_func)

If value is an instance of class klass, return it, else create a new instance of klass with value value.

[ GitHub ]

  
# File 'lib/rss/utils.rb', line 110

def new_with_value_if_need(klass, value)
  if value.is_a?(klass)
    value
  else
    klass.new(value)
  end
end

.to_class_name(name) (mod_func)

Given a name in a name_with_underscores or a name-with-dashes format, returns the CamelCase version of name.

If the name is already CamelCased, nothing happens.

Examples:

require 'rss/utils'

RSS::Utils.to_class_name("sample_name")
# => "SampleName"
RSS::Utils.to_class_name("with-dashes")
# => "WithDashes"
RSS::Utils.to_class_name("CamelCase")
# => "CamelCase"
[ GitHub ]

  
# File 'lib/rss/utils.rb', line 27

def to_class_name(name)
  name.split(/[_\-]/).collect do |part|
    "#{part[0, 1].upcase}#{part[1..-1]}"
  end.join("")
end