Class: HtmlSlicer::Interface

Inherits:
Object
  • Object
show all
Includes:
Mappers
Defined in:
lib/html_slicer/interface.rb

Overview

Interface code. Accepts slice number, store it into instance variable and provides resulted String.

Example:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, method_name, options = {}) ⇒ Interface

Returns a new instance of Interface.



25
26
27
28
29
30
31
32
# File 'lib/html_slicer/interface.rb', line 25

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



115
116
117
# File 'lib/html_slicer/interface.rb', line 115

def method_missing(*args, &block)
  to_s.send(*args, &block)
end

Instance Attribute Details

#cached_stuffObject (readonly)

Returns the value of attribute cached_stuff.



21
22
23
# File 'lib/html_slicer/interface.rb', line 21

def cached_stuff
  @cached_stuff
end

#current_sliceObject (readonly)

Returns the value of attribute current_slice.



21
22
23
# File 'lib/html_slicer/interface.rb', line 21

def current_slice
  @current_slice
end

#documentObject (readonly)

Returns the value of attribute document.



21
22
23
# File 'lib/html_slicer/interface.rb', line 21

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/html_slicer/interface.rb', line 21

def options
  @options
end

Instance Method Details

#inspectObject



34
35
36
# File 'lib/html_slicer/interface.rb', line 34

def inspect
  "'#{to_s}'"
end

#last_slice?Boolean

Return the current slice is a last or not?

Returns:

  • (Boolean)


130
131
132
# File 'lib/html_slicer/interface.rb', line 130

def last_slice?
  current_slice == slice_number
end

#load!Object

Process initializer



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/html_slicer/interface.rb', line 48

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

#resized?Boolean

True if any HTML tag has been resized

Returns:

  • (Boolean)


120
121
122
# File 'lib/html_slicer/interface.rb', line 120

def resized?
  resizing ? resizing.map.any? : false
end

#slice!(slice = nil) ⇒ Object

General slicing method. Passing the argument changes the current_slice.

Raises:

  • (Exception)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/html_slicer/interface.rb', line 98

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 Fixnum in (1..#{slice_number}). #{slice.inspect} passed.")
    end
  end
  self
end

#slice_numberObject

Return number of slices.



93
94
95
# File 'lib/html_slicer/interface.rb', line 93

def slice_number
  sliced? ? slicing.slice_number : 1
end

#sliced?Boolean

True if any part of document has been sliced

Returns:

  • (Boolean)


125
126
127
# File 'lib/html_slicer/interface.rb', line 125

def sliced?
  slicing ? slicing.map.any? : false
end

#sourceObject

Getting source content



39
40
41
42
43
44
45
# File 'lib/html_slicer/interface.rb', line 39

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) ⇒ Object



110
111
112
113
# File 'lib/html_slicer/interface.rb', line 110

def to_s(&block)
  load!
  view(document.root, @current_slice, &block)
end