require 'caracal/core/models/base_model' module Caracal module Core module Models # This class encapsulates the logic needed to store and manipulate # paragraph style data. # class StyleModel < BaseModel #------------------------------------------------------------- # Configuration #------------------------------------------------------------- # constants const_set(:DEFAULT_STYLE_COLOR, '333333') const_set(:DEFAULT_STYLE_SIZE, 20) const_set(:DEFAULT_STYLE_BOLD, false) const_set(:DEFAULT_STYLE_ITALIC, false) const_set(:DEFAULT_STYLE_UNDERLINE, false) const_set(:DEFAULT_STYLE_ALIGN, :left) const_set(:DEFAULT_STYLE_LINE, 360) # 0.25in in twips const_set(:DEFAULT_STYLE_TOP, 0) # 0.0in in twips const_set(:DEFAULT_STYLE_BOTTOM, 0) # 0.0in in twips const_set(:DEFAULT_STYLE_BASE, 'Normal') const_set(:DEFAULT_STYLE_NEXT, 'Normal') # accessors attr_reader :style_default attr_reader :style_id attr_reader :style_name attr_reader :style_color attr_reader :style_font attr_reader :style_size attr_reader :style_bold attr_reader :style_italic attr_reader :style_underline attr_reader :style_align attr_reader :style_top attr_reader :style_bottom attr_reader :style_line attr_reader :style_base attr_reader :style_next # initialization def initialize(**options, &block) @style_default = false @style_base = DEFAULT_STYLE_BASE @style_next = DEFAULT_STYLE_NEXT super options, &block if (style_id == DEFAULT_STYLE_BASE) @style_default ||= true @style_color ||= DEFAULT_STYLE_COLOR @style_size ||= DEFAULT_STYLE_SIZE @style_bold ||= DEFAULT_STYLE_BOLD @style_italic ||= DEFAULT_STYLE_ITALIC @style_underline ||= DEFAULT_STYLE_UNDERLINE @style_align ||= DEFAULT_STYLE_ALIGN @style_top ||= DEFAULT_STYLE_TOP @style_bottom ||= DEFAULT_STYLE_BOTTOM @style_line ||= DEFAULT_STYLE_LINE end end #------------------------------------------------------------- # Public Instance Methods #------------------------------------------------------------- #=============== SETTERS ============================== # booleans [:bold, :italic, :underline].each do |m| define_method "#{ m }" do |value| instance_variable_set("@style_#{ m }", !!value) end end # integers [:bottom, :size, :line, :top].each do |m| define_method "#{ m }" do |value| instance_variable_set("@style_#{ m }", value.to_i) end end # strings [:id, :name, :color, :font].each do |m| define_method "#{ m }" do |value| instance_variable_set("@style_#{ m }", value.to_s) end end # symbols [:align].each do |m| define_method "#{ m }" do |value| instance_variable_set("@style_#{ m }", value.to_s.to_sym) end end #=============== STATE ================================ def matches?(str) style_id.downcase == str.to_s.downcase end #=============== VALIDATION =========================== def valid? a = [:id, :name] a.map { |m| send("style_#{ m }") }.compact.size == a.size end #------------------------------------------------------------- # Private Instance Methods #------------------------------------------------------------- private def option_keys [:bold, :italic, :underline, :top, :bottom, :size, :line, :id, :name, :color, :font, :align] end end end end end