class ODFWriter::Field
Field: replace fields
Constants
- DELIMITERS
constants
- HTML_ESCAPE
Attributes
name[RW]
constants
Public Class Methods
new(options, &block)
click to toggle source
initialize
# File lib/odf_writer/field.rb, line 50 def initialize(options, &block) @name = options[:name] @value = options[:value] @field = options[:field] @key = @field || @name @proc = options[:proc] @remove_classes = options[:remove_classes] @remove_class_prefix = options[:remove_class_prefix] @remove_class_suffix = options[:remove_class_suffix] @value ||= @proc unless @value if block_given? @value = block else @value = lambda { |item, key| field(item, key) } end end end
Public Instance Methods
field(item, key)
click to toggle source
field
# File lib/odf_writer/field.rb, line 98 def field(item, key) case item when NilClass key when Hash hash_value(item, key) else item_field(item, key) end end
replace!(content, item = nil)
click to toggle source
replace!
# File lib/odf_writer/field.rb, line 78 def replace!(content, item = nil) txt = content.inner_html txt.gsub!(placeholder, sanitize(value(item))) content.inner_html = txt end
value(item = nil)
click to toggle source
value
# File lib/odf_writer/field.rb, line 89 def value(item = nil) @value.is_a?(Proc) ? @value.call(item, @key) : @value end
Private Instance Methods
deep_fields(fs)
click to toggle source
# File lib/odf_writer/field.rb, line 165 def deep_fields(fs) fs.split(/\./) end
deep_try(item, f)
click to toggle source
# File lib/odf_writer/field.rb, line 169 def deep_try(item, f) deep_fields(f).inject(item) {|obj,f| obj.try(f.to_s.underscore.to_sym)} end
hash_value(hash, key)
click to toggle source
# File lib/odf_writer/field.rb, line 119 def hash_value(hash, key) hash[key.to_s] || hash[key.to_sym] || hash[key.to_s.underscore] || hash[key.to_s.underscore.to_sym] end
html_escape(s)
click to toggle source
# File lib/odf_writer/field.rb, line 154 def html_escape(s) return "" unless s s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] } end
item_field(item, field)
click to toggle source
# File lib/odf_writer/field.rb, line 127 def item_field(item, field) item.try(field.to_s.to_sym) || item.try(field.to_s.underscore.to_sym) end
odf_linebreak(s)
click to toggle source
# File lib/odf_writer/field.rb, line 159 def odf_linebreak(s) return "" unless s s = s.encode(universal_newline: true) s.to_s.gsub("\n", "<text:line-break/>").gsub("<br.*?>", "<text:line-break/>") end
placeholder()
click to toggle source
placeholder
# File lib/odf_writer/field.rb, line 135 def placeholder "#{DELIMITERS[0]}#{@name.to_s.upcase}#{DELIMITERS[1]}" end
sanitize(text)
click to toggle source
sanitize
# File lib/odf_writer/field.rb, line 142 def sanitize(text) # if we get some object, which is not a string, Numeric or the like # f.i. a Hash or an Arry or a CollectionProxy or an image then return @key to avoid # uggly errors return @key.to_s if text.respond_to?(:each) text = html_escape(text) text = odf_linebreak(text) text end