Module: Caracal::Core::Styles

Included in:
Document
Defined in:
lib/caracal/core/styles.rb

Overview

This module encapsulates all the functionality related to defining paragraph styles.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/caracal/core/styles.rb', line 12

def self.included(base)
  base.class_eval do
    
    #-------------------------------------------------------------
    # Class Methods
    #-------------------------------------------------------------
    
    def self.default_styles
      [
        { id: 'Normal',   name: 'normal',    font: 'Arial',    size: 20, line: 320, color: '333333' },
        { id: 'Heading1', name: 'heading 1', font: 'Palatino', size: 36, bottom: 120 },
        { id: 'Heading2', name: 'heading 2', font: 'Arial',    size: 26, top: 120, bottom: 160, bold: true },
        { id: 'Heading3', name: 'heading 3', font: 'Arial',    size: 24, top: 120, bottom: 160, bold: true, italic: true, color: '666666' },
        { id: 'Heading4', name: 'heading 4', font: 'Palatino', size: 24, top: 120, bottom: 120, bold: true },
        { id: 'Heading5', name: 'heading 5', font: 'Arial',    size: 22, top: 120, bottom: 120, bold: true },
        { id: 'Heading6', name: 'heading 6', font: 'Arial',    size: 22, top: 120, bottom: 120, underline: true, italic: true, color: '666666' },
        { id: 'Title',    name: 'title',     font: 'Palatino', size: 60 },
        { id: 'Subtitle', name: 'subtitle',  font: 'Arial',    size: 28, top: 60 }
      ]           
    end
    
    
    #-------------------------------------------------------------
    # Public Methods
    #-------------------------------------------------------------
    
    #============== ATTRIBUTES ==========================
    
    def style(options={}, &block)
      model = Caracal::Core::Models::StyleModel.new(options, &block)
      
      if model.valid?
        register_style(model)
      else
        raise Caracal::Errors::InvalidModelError, 'style must define an :id and :name.'
      end
      model
    end
    
    
    #============== GETTERS =============================
    
    def styles
      @styles ||= []
    end
    
    def default_style
      styles.find { |s| s.style_default }
    end
    
    def find_style(id)
      styles.find { |s| s.matches?(id) }
    end
    
    
    #============== REGISTRATION ========================
    
    def register_style(model)
      unregister_style(model.style_id)
      styles << model
      model
    end
    
    def unregister_style(id)
      if s = find_style(id)
        styles.delete(s)
      end
    end
    
  end
end