Skip to content

Commit

Permalink
standardrb --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrods committed Mar 22, 2024
1 parent bed4417 commit f732fec
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 248 deletions.
32 changes: 16 additions & 16 deletions lib/odf-report.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
require 'rubygems'
require 'zip'
require 'fileutils'
require 'nokogiri'
require 'mime/types'
require 'securerandom'
require "rubygems"
require "zip"
require "fileutils"
require "nokogiri"
require "mime/types"
require "securerandom"

require File.expand_path('../odf-report/parser/default', __FILE__)
require File.expand_path("../odf-report/parser/default", __FILE__)

require File.expand_path('../odf-report/data_source', __FILE__)
require File.expand_path('../odf-report/field', __FILE__)
require File.expand_path('../odf-report/text', __FILE__)
require File.expand_path('../odf-report/template', __FILE__)
require File.expand_path('../odf-report/image', __FILE__)
require File.expand_path('../odf-report/nestable', __FILE__)
require File.expand_path('../odf-report/section', __FILE__)
require File.expand_path('../odf-report/table', __FILE__)
require File.expand_path('../odf-report/report', __FILE__)
require File.expand_path("../odf-report/data_source", __FILE__)
require File.expand_path("../odf-report/field", __FILE__)
require File.expand_path("../odf-report/text", __FILE__)
require File.expand_path("../odf-report/template", __FILE__)
require File.expand_path("../odf-report/image", __FILE__)
require File.expand_path("../odf-report/nestable", __FILE__)
require File.expand_path("../odf-report/section", __FILE__)
require File.expand_path("../odf-report/table", __FILE__)
require File.expand_path("../odf-report/report", __FILE__)
16 changes: 6 additions & 10 deletions lib/odf-report/data_source.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module ODFReport
class DataSource

attr_reader :value

def initialize(opts, &block)
@value = opts[:value] || opts[:collection]
@value = opts[:value] || opts[:collection]
@data_field = opts[:data_field] || opts[:collection_field] || opts[:name]
@block = block
@block = block
end

def set_source(record)
Expand All @@ -25,7 +24,6 @@ def empty?
private

def extract_value_from_item(record)

if @block
@block.call(record)

Expand All @@ -43,23 +41,21 @@ def extract_value_from_item(record)
record.send(@data_field)

else
raise "Can't find [#{@data_field.to_s}] in this #{record.class}"
raise "Can't find [#{@data_field}] in this #{record.class}"

end

end

def execute_methods_on_item(record)
tmp = record.dup
@data_field.each do |f|
if f.is_a?(Hash)
tmp = tmp.send(f.keys[0], f.values[0])
tmp = if f.is_a?(Hash)
tmp.send(f.keys[0], f.values[0])
else
tmp = tmp.send(f)
tmp.send(f)
end
end
tmp
end

end
end
11 changes: 3 additions & 8 deletions lib/odf-report/field.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module ODFReport
class Field

DELIMITERS = %w([ ])

def initialize(opts, &block)
Expand All @@ -14,16 +13,14 @@ def set_source(record)
end

def replace!(content, data_item = nil)

txt = content.inner_html

if txt.gsub!(to_placeholder, sanitize(@data_source.value))
content.inner_html = txt
end

end

private
private

def to_placeholder
if DELIMITERS.is_a?(Array)
Expand All @@ -35,11 +32,10 @@ def to_placeholder

def sanitize(txt)
txt = html_escape(txt)
txt = odf_linebreak(txt)
txt
odf_linebreak(txt)
end

HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
HTML_ESCAPE = {"&" => "&amp;", ">" => "&gt;", "<" => "&lt;", '"' => "&quot;"}

def html_escape(s)
return "" unless s
Expand All @@ -50,6 +46,5 @@ def odf_linebreak(s)
return "" unless s
s.to_s.gsub("\n", "<text:line-break/>")
end

end
end
17 changes: 6 additions & 11 deletions lib/odf-report/image.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module ODFReport
class Image < Field

IMAGE_DIR_NAME = "Pictures"

attr_reader :files
Expand All @@ -11,7 +10,6 @@ def initialize(opts, &block)
end

def replace!(doc, data_item = nil)

frame = doc.xpath("//draw:frame[@draw:name='#{@name}']").first
image = doc.xpath("//draw:frame[@draw:name='#{@name}']/draw:image").first

Expand All @@ -20,14 +18,13 @@ def replace!(doc, data_item = nil)
file = @data_source.value

if file
image.attribute('href').content = File.join(IMAGE_DIR_NAME, File.basename(file))
frame.attribute('name').content = SecureRandom.uuid
image.attribute("href").content = File.join(IMAGE_DIR_NAME, File.basename(file))
frame.attribute("name").content = SecureRandom.uuid

@files << file
else
frame.remove
end

end

def self.include_image_file(zip_file, image_file)
Expand All @@ -41,17 +38,15 @@ def self.include_image_file(zip_file, image_file)
def self.include_manifest_entry(content, image_file)
return unless image_file

return unless root_node = content.at("//manifest:manifest")
return unless (root_node = content.at("//manifest:manifest"))

href = File.join(IMAGE_DIR_NAME, File.basename(image_file))

entry = content.create_element('manifest:file-entry')
entry['manifest:full-path'] = href
entry['manifest:media-type'] = MIME::Types.type_for(href)[0].content_type
entry = content.create_element("manifest:file-entry")
entry["manifest:full-path"] = href
entry["manifest:media-type"] = MIME::Types.type_for(href)[0].content_type

root_node.add_child entry

end

end
end
31 changes: 16 additions & 15 deletions lib/odf-report/nestable.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
module ODFReport
class Nestable

def initialize(opts)
@name = opts[:name]

@data_source = DataSource.new(opts)

@fields = []
@texts = []
@tables = []
@fields = []
@texts = []
@tables = []
@sections = []
@images = []

@images = []
end

def set_source(data_item)
@data_source.set_source(data_item)
self
end

def add_field(name, data_field=nil, &block)
opts = { name: name, data_field: data_field }
def add_field(name, data_field = nil, &block)
opts = {name: name, data_field: data_field}
@fields << Field.new(opts, &block)
end
alias_method :add_column, :add_field

def add_text(name, data_field=nil, &block)
def add_text(name, data_field = nil, &block)
opts = {name: name, data_field: data_field}
@texts << Text.new(opts, &block)
end

def add_image(name, data_field=nil, &block)
def add_image(name, data_field = nil, &block)
opts = {name: name, data_field: data_field}
@images << Image.new(opts, &block)
end

def add_table(table_name, collection_field, opts={})
opts.merge!(name: table_name, collection_field: collection_field)
def add_table(table_name, collection_field, opts = {})
opts[:name] = table_name
opts[:collection_field] = collection_field

tab = Table.new(opts)
@tables << tab

yield(tab)
end

def add_section(section_name, collection_field, opts={})
opts.merge!(name: section_name, collection_field: collection_field)
def add_section(section_name, collection_field, opts = {})
opts[:name] = section_name
opts[:collection_field] = collection_field

sec = Section.new(opts)
@sections << sec

Expand All @@ -60,6 +62,5 @@ def wrap_with_ns(node)
<root xmlns:draw="a" xmlns:xlink="b" xmlns:text="c" xmlns:table="d">#{node.to_xml}</root>
XML
end

end
end
Loading

0 comments on commit f732fec

Please sign in to comment.