class HtmlSlicer::Interface
Interface
code. Accepts slice number, store it into instance variable and provides resulted String.
Example:¶ ↑
@article = Article.find(1) @article_paged = @article.paged.slice!(params)
Attributes
cached_stuff[R]
current_slice[R]
document[R]
options[R]
Public Class Methods
new(env, method_name, options = {})
click to toggle source
# File lib/html_slicer/interface.rb, line 26 def initialize(env, method_name, options = {}) @env, @method_name = env, method_name @options = options @resizing_options = ResizingOptions.new(options[:resize]) if options[:resize] @slicing_options = SlicingOptions.new(options[:slice]) if options[:slice] @current_slice = 1 load! end
Public Instance Methods
inspect()
click to toggle source
# File lib/html_slicer/interface.rb, line 35 def inspect "'#{to_s}'" end
last_slice?()
click to toggle source
Return the current slice is a last or not?
# File lib/html_slicer/interface.rb, line 131 def last_slice? current_slice == slice_number end
load!()
click to toggle source
Process
initializer
# File lib/html_slicer/interface.rb, line 49 def load! text = source||"" @cached_stuff ||= begin if options[:cache_to] # Getting recorded hash dump Marshal.load(Base64.decode64(@env.send(options[:cache_to]))).tap do |cached_stuff| if cached_stuff.time < Date.new(2012,7,25) #### CACHE OUT OF DATE #### warn "WARNING: html_slicer's cached stuff for #{@env.class.name} records has become unacceptable because of code changes. Update each record again. Visit http://vkvon.ru/projects/html_slicer for further details." raise Exception end end else raise Exception end rescue Exception, TypeError, NoMethodError # New cache object otherwise CachedStuff.new end if @document.blank? || !@cached_stuff.valid_text?(text) # Initialize new @document if not exist or content has been changed @document = ::HTML::Document.new(text) @cached_stuff.hexdigest_for = text end if @cached_stuff.changed? || !@cached_stuff.valid_resizing_options?(@resizing_options) # Initialize new resizing process if the content or options has been changed if @resizing_options @cached_stuff.resizing = Resizing.new(@document, @resizing_options) else @cached_stuff.resizing = nil end end if @cached_stuff.changed? || !@cached_stuff.valid_slicing_options?(@slicing_options) # Initialize new slicing process if the content or options has been changed if @slicing_options @cached_stuff.slicing = Slicing.new(@document, @slicing_options) else @cached_stuff.slicing = nil end end if @cached_stuff.changed? # Serialize and dump the cache if any changes provided @cached_stuff.changed = false if options[:cache_to] @env.send("#{options[:cache_to]}=", @cached_stuff.to_dump) end end end
method_missing(*args, &block)
click to toggle source
# File lib/html_slicer/interface.rb, line 116 def method_missing(*args, &block) to_s.send(*args, &block) end
resized?()
click to toggle source
True if any HTML tag has been resized
# File lib/html_slicer/interface.rb, line 121 def resized? resizing ? resizing.map.any? : false end
slice!(slice = nil)
click to toggle source
General slicing method. Passing the argument changes the current_slice
.
# File lib/html_slicer/interface.rb, line 99 def slice!(slice = nil) raise(Exception, "Slicing unavailable!") unless sliced? if slice.present? if slice.to_i.in?(1..slice_number) @current_slice = slice.to_i else raise(ArgumentError, "Slice number must be Integer in (1..#{slice_number}). #{slice.inspect} passed.") end end self end
slice_number()
click to toggle source
Return number of slices.
# File lib/html_slicer/interface.rb, line 94 def slice_number sliced? ? slicing.slice_number : 1 end
sliced?()
click to toggle source
True if any part of document has been sliced
# File lib/html_slicer/interface.rb, line 126 def sliced? slicing ? slicing.map.any? : false end
source()
click to toggle source
Getting source content
# File lib/html_slicer/interface.rb, line 40 def source case options[:processors].present? when true then HtmlSlicer::Process.iterate(@env.send(@method_name), options[:processors]) else @env.send(@method_name) end end
to_s(&block)
click to toggle source
# File lib/html_slicer/interface.rb, line 111 def to_s(&block) load! view(document.root, @current_slice, &block) end
Private Instance Methods
view(node, slice) { |self, export| ... }
click to toggle source
Return a textual representation of the node including all children.
# File lib/html_slicer/interface.rb, line 138 def view(node, slice, &block) slice = slice.to_i case node when ::HTML::Tag then children_view = node.children.map {|child| view(child, slice, &block)}.compact.join if resized? resizing.resize_node(node) end if sliced? if slicing.map.get(node, slice) || children_view.present? if node.closing == :close "</#{node.name}>" else s = "<#{node.name}" node.attributes.each do |k,v| s << " #{k}" s << "=\"#{v}\"" if String === v end s << " /" if node.closing == :self s << ">" s += children_view s << "</#{node.name}>" if node.closing != :self && !node.children.empty? s end end else node.to_s end when ::HTML::Text then if sliced? if range = slicing.map.get(node, slice) (range.is_a?(Array) ? node.content[Range.new(*range)] : node.content).tap do |export| unless range == true || (range.is_a?(Array) && range.last == -1) # broken text export << slicing.options.text_break if slicing.options.text_break if block_given? yield self, export end end end end else node.to_s end when ::HTML::CDATA then node.to_s when ::HTML::Node then node.children.map {|child| view(child, slice, &block)}.compact.join end end