Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for headers and footers #161

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish RubyGem

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true

- name: Tests
run: |
bundle exec rspec
40 changes: 40 additions & 0 deletions .github/workflows/publish_gem.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Publish RubyGem

on:
release:
types:
- released

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true

- name: Tests
run: |
bundle exec rspec

- name: Build and publish gem
run: |
mkdir -p ~/.gem
cat << EOF > ~/.gem/credentials
---
:github: Bearer ${GITHUB_TOKEN}
:rubygems_api_key: ${RUBYGEMS_API_KEY}
EOF

chmod 0600 ~/.gem/credentials

find . -name '*.gemspec' -maxdepth 1 -exec gem build {} \;
find . -name '*.gem' -maxdepth 1 -print0 | xargs -0 gem push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
RELEASE_COMMAND: rake release
6 changes: 3 additions & 3 deletions caracal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'caracal/version'

Gem::Specification.new do |spec|
spec.name = 'caracal'
spec.name = 'caracal_the_curve'
spec.version = Caracal::VERSION
spec.authors = ['Trade Infomatics', 'John Dugan']
spec.email = ['[email protected]']
Expand All @@ -19,10 +19,10 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'nokogiri', '~> 1.6'
spec.add_dependency 'rubyzip', '~> 1.1'
spec.add_dependency 'rubyzip', ['>= 1.1.0', '< 3.0']
spec.add_dependency 'tilt', '>= 1.4'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'bundler', '~> 2.4'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
end
16 changes: 16 additions & 0 deletions lib/caracal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ def self.root
include Caracal::Core::Tables
include Caracal::Core::Text
end

Caracal::Core::Models::FooterModel.class_eval do
include Caracal::Core::Images
include Caracal::Core::Lists
include Caracal::Core::Rules
include Caracal::Core::Tables
include Caracal::Core::Text
end

Caracal::Core::Models::HeaderModel.class_eval do
include Caracal::Core::Images
include Caracal::Core::Lists
include Caracal::Core::Rules
include Caracal::Core::Tables
include Caracal::Core::Text
end
33 changes: 33 additions & 0 deletions lib/caracal/core/footer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'caracal/core/models/footer_model'
require 'caracal/errors'


module Caracal
module Core

# This module encapsulates all the functionality related to adding a
# footer on every page of the document.
#
module Footer
def self.included(base)
base.class_eval do

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

def footer(*args, &block)
options = Caracal::Utilities.extract_options!(args)

model = Caracal::Core::Models::FooterModel.new(options, &block)

@footer_content = model

model
end

end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/caracal/core/header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'caracal/core/models/header_model'
require 'caracal/errors'


module Caracal
module Core

# This module encapsulates all the functionality related to adding a header
# to every page of the document.
#
module Header
def self.included(base)
base.class_eval do

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

def header(*args, &block)
options = Caracal::Utilities.extract_options!(args)

model = Caracal::Core::Models::HeaderModel.new(options, &block)

@header_content = model

model
end
end
end
end
end
end
137 changes: 137 additions & 0 deletions lib/caracal/core/models/field_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
require 'caracal/core/models/base_model'


module Caracal
module Core
module Models

# This class encapsulates the logic needed to store and manipulate
# text data.
#
class FieldModel < BaseModel

#--------------------------------------------------
# Configuration
#--------------------------------------------------

# constants
const_set(:TYPE_MAP, { page: 'PAGE', numpages: 'NUMPAGES' })

# accessors
attr_reader :field_dirty
attr_reader :field_type
attr_reader :field_style
attr_reader :field_font
attr_reader :field_color
attr_reader :field_size
attr_reader :field_bold
attr_reader :field_italic
attr_reader :field_underline
attr_reader :field_bgcolor
attr_reader :field_highlight_color
attr_reader :field_vertical_align

#-------------------------------------------------------------
# Public Class Methods
#-------------------------------------------------------------

def self.formatted_type(type)
TYPE_MAP.fetch(type.to_s.to_sym)
end


#-------------------------------------------------------------
# Public Instance Methods
#-------------------------------------------------------------

#=============== GETTERS ==============================

def formatted_type
self.class.formatted_type(field_type)
end

#========== GETTERS ===============================

# .run_attributes
def run_attributes
{
style: field_style,
font: field_font,
color: field_color,
size: field_size,
bold: field_bold,
italic: field_italic,
underline: field_underline,
bgcolor: field_bgcolor,
highlight_color: field_highlight_color,
vertical_align: field_vertical_align
}
end


#========== SETTERS ===============================

# booleans
[:bold, :italic, :underline].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", !!value)
end
end

# integers
[:size].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", value.to_i)
end
end

# strings
[:bgcolor, :color, :dirty, :font, :highlight_color, :style, :type,].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", value.to_s)
end
end

# symbols
[:vertical_align].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", value.to_s.to_sym)
end
end


#========== VALIDATION ============================

def valid?
a = [:type]
a.map { |m| send("field_#{ m }") }.compact.size == a.size
end


#--------------------------------------------------
# Private Methods
#--------------------------------------------------
private

def option_keys
[:type, :style, :font, :color, :size, :bold, :italic, :underline, :bgcolor, :highlight_color, :vertical_align]
end

def method_missing(method, *args, &block)
# TODO: Better field centric description

# I'm on the fence with respect to this implementation. We're ignoring
# :method_missing errors to allow syntax flexibility for paragraph-type
# models. The issue is the syntax format of those models--the way we pass
# the content value as a special argument--coupled with the model's
# ability to accept nested instructions.
#
# By ignoring method missing errors here, we can pass the entire paragraph
# block in the initial, built-in call to :text.
end

end

end
end
end
42 changes: 42 additions & 0 deletions lib/caracal/core/models/footer_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'
require 'caracal/core/models/paragraph_model'


module Caracal
module Core
module Models

# This class handles block options passed to tables via their data
# collections.
#
class FooterModel < BaseModel

#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

# initialization
def initialize(options={}, &block)
super options, &block
end

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

#=============== DATA ACCESSORS =======================

def contents
@contents ||= []
end

#=============== VALIDATION ===========================

def valid?
contents.size > 0
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/caracal/core/models/header_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'
require 'caracal/core/models/paragraph_model'


module Caracal
module Core
module Models

# This class handles block options passed to tables via their data
# collections.
#
class HeaderModel < BaseModel

#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

# initialization
def initialize(options={}, &block)
super options, &block
end

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

#=============== DATA ACCESSORS =======================

def contents
@contents ||= []
end

#=============== VALIDATION ===========================

def valid?
contents.size > 0
end
end
end
end
end
Loading