Class: HtmlSlicer::Interface
- Inherits:
-
Object
- Object
- HtmlSlicer::Interface
- 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
-
#cached_stuff ⇒ Object
readonly
Returns the value of attribute cached_stuff.
-
#current_slice ⇒ Object
readonly
Returns the value of attribute current_slice.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(env, method_name, options = {}) ⇒ Interface
constructor
A new instance of Interface.
-
#last_slice? ⇒ Boolean
Return the current slice is a last or not?.
-
#load! ⇒ Object
Process initializer.
- #method_missing(*args, &block) ⇒ Object
-
#resized? ⇒ Boolean
True if any HTML tag has been resized.
-
#slice!(slice = nil) ⇒ Object
General slicing method.
-
#slice_number ⇒ Object
Return number of slices.
-
#sliced? ⇒ Boolean
True if any part of document has been sliced.
-
#source ⇒ Object
Getting source content.
- #to_s(&block) ⇒ Object
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, = {}) @env, @method_name = env, method_name = = ResizingOptions.new([:resize]) if [:resize] = SlicingOptions.new([:slice]) if [:slice] @current_slice = 1 load! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
111 112 113 |
# File 'lib/html_slicer/interface.rb', line 111 def method_missing(*args, &block) to_s.send(*args, &block) end |
Instance Attribute Details
#cached_stuff ⇒ Object (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_slice ⇒ Object (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 |
#document ⇒ Object (readonly)
Returns the value of attribute document.
21 22 23 |
# File 'lib/html_slicer/interface.rb', line 21 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/html_slicer/interface.rb', line 21 def end |
Instance Method Details
#last_slice? ⇒ Boolean
Return the current slice is a last or not?
126 127 128 |
# File 'lib/html_slicer/interface.rb', line 126 def last_slice? current_slice == slice_number end |
#load! ⇒ Object
Process initializer
44 45 46 47 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 |
# File 'lib/html_slicer/interface.rb', line 44 def load! text = source||"" @cached_stuff ||= begin if [:cache_to] # Getting recorded hash dump Marshal.load(Base64.decode64(@env.send([: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.() # Initialize new resizing process if the content or options has been changed if @cached_stuff.resizing = Resizing.new(@document, ) else @cached_stuff.resizing = nil end end if @cached_stuff.changed? || !@cached_stuff.() # Initialize new slicing process if the content or options has been changed if @cached_stuff.slicing = Slicing.new(@document, ) 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 [:cache_to] @env.send("#{options[:cache_to]}=", @cached_stuff.to_dump) end end end |
#resized? ⇒ Boolean
True if any HTML tag has been resized
116 117 118 |
# File 'lib/html_slicer/interface.rb', line 116 def resized? resizing ? resizing.map.any? : false end |
#slice!(slice = nil) ⇒ Object
General slicing method. Passing the argument changes the current_slice.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/html_slicer/interface.rb', line 94 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_number ⇒ Object
Return number of slices.
89 90 91 |
# File 'lib/html_slicer/interface.rb', line 89 def slice_number sliced? ? slicing.slice_number : 1 end |
#sliced? ⇒ Boolean
True if any part of document has been sliced
121 122 123 |
# File 'lib/html_slicer/interface.rb', line 121 def sliced? slicing ? slicing.map.any? : false end |
#source ⇒ Object
Getting source content
35 36 37 38 39 40 41 |
# File 'lib/html_slicer/interface.rb', line 35 def source case [:processors].present? when true then HtmlSlicer::Process.iterate(@env.send(@method_name), [:processors]) else @env.send(@method_name) end end |
#to_s(&block) ⇒ Object
106 107 108 109 |
# File 'lib/html_slicer/interface.rb', line 106 def to_s(&block) load! view(document.root, @current_slice, &block) end |