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.



26
27
28
29
30
31
32
33
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



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

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



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

def inspect
  "'#{to_s}'"
end

#last_slice?Boolean

Return the current slice is a last or not?

Returns:

  • (Boolean)


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

def last_slice?
  current_slice == slice_number
end

#load!Object

Process initializer



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
91
# 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

#resized?Boolean

True if any HTML tag has been resized

Returns:

  • (Boolean)


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

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

#slice!(slice = nil) ⇒ Object

General slicing method. Passing the argument changes the current_slice.

Raises:

  • (Exception)


99
100
101
102
103
104
105
106
107
108
109
# 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_numberObject

Return number of slices.



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

def slice_number
  sliced? ? slicing.slice_number : 1
end

#sliced?Boolean

True if any part of document has been sliced

Returns:

  • (Boolean)


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

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

#sourceObject

Getting source content



40
41
42
43
44
45
46
# 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) ⇒ Object



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

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