Class: Chunk::Abstract

Inherits:
Object
  • Object
show all
Defined in:
app/models/chunks/chunk.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match_data, content) ⇒ Abstract

Returns a new instance of Abstract.



36
37
38
39
40
# File 'app/models/chunks/chunk.rb', line 36

def initialize(match_data, content) 
  @text = match_data[0] 
  @content = content
  @unmask_mode = :normal
end

Class Attribute Details

.derivativesObject (readonly)

Returns the value of attribute derivatives.



16
17
18
# File 'app/models/chunks/chunk.rb', line 16

def derivatives
  @derivatives
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



34
35
36
# File 'app/models/chunks/chunk.rb', line 34

def text
  @text
end

#unmask_modeObject (readonly)

Returns the value of attribute unmask_mode.



34
35
36
# File 'app/models/chunks/chunk.rb', line 34

def unmask_mode
  @unmask_mode
end

#unmask_textObject (readonly)

Returns the value of attribute unmask_text.



34
35
36
# File 'app/models/chunks/chunk.rb', line 34

def unmask_text
  @unmask_text
end

Class Method Details

.apply_to(content) ⇒ Object

Find all the chunks of the given type in content Each time the pattern is matched, create a new chunk for it, and replace the occurance of the chunk in this content with its mask.



46
47
48
49
50
51
52
# File 'app/models/chunks/chunk.rb', line 46

def self.apply_to(content)
	  content.gsub!( self.pattern ) do |match|	
    new_chunk = self.new($~, content)
    content.add_chunk(new_chunk)
    new_chunk.mask
  end
end

.inherited(klass) ⇒ Object



19
20
21
# File 'app/models/chunks/chunk.rb', line 19

def self::inherited( klass ) 
  Abstract::derivatives << klass 
end

.mask_re(chunk_types) ⇒ Object

a regexp that matches all chunk_types masks



29
30
31
32
# File 'app/models/chunks/chunk.rb', line 29

def Abstract::mask_re(chunk_types)
  tmp = chunk_types.map{|klass| klass.mask_string}.join("|")
  Regexp.new("chunk([0-9a-f]+n\\d+)(#{tmp})chunk")
end

.mask_stringObject

the class name part of the mask strings



24
25
26
# File 'app/models/chunks/chunk.rb', line 24

def self.mask_string
  self.to_s.delete(':').downcase
end

Instance Method Details

#escaped?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/chunks/chunk.rb', line 74

def escaped?
  @unmask_mode == :escape
end

#idObject

We should not use object_id because object_id is not guarantied to be unique when we restart the wiki (new object ids can equal old ones that were restored from madeleine storage)



62
63
64
# File 'app/models/chunks/chunk.rb', line 62

def id
  @id ||= "#{@content.page_id}n#{@content.chunk_id}"
end

#maskObject

should contain only [a-z0-9]



55
56
57
# File 'app/models/chunks/chunk.rb', line 55

def mask
  @mask ||="chunk#{@id}#{self.class.mask_string}chunk"
end

#rendered?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/chunks/chunk.rb', line 70

def rendered?
  @unmask_mode == :normal
end

#revertObject



78
79
80
81
82
# File 'app/models/chunks/chunk.rb', line 78

def revert
  @content.sub!(mask, @text)
  # unregister
  @content.delete_chunk(self)
end

#unmaskObject



66
67
68
# File 'app/models/chunks/chunk.rb', line 66

def unmask
  @content.sub!(mask, @unmask_text)
end