Skip to content

Commit

Permalink
refactor error messages, and data loading in page and blogs. add miss…
Browse files Browse the repository at this point in the history
…ing dentaku functions
  • Loading branch information
Steven Spriggs committed Feb 28, 2019
1 parent 3211477 commit f0a5d49
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 343 deletions.
41 changes: 38 additions & 3 deletions hammer/hammer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,44 @@ def get_mime_type
WEBrick::HTTPUtils::mime_type(@filesystem_path.to_s, mime_file)
end

def self.error(message)
puts "Hammer Error: #{message.gsub!(/(<[^>]*>)|\n|\t/s) {""}}".colorize(:red)
return "<strong>Hammer Error: </strong> #{message}"
def self.error(message, options={})
options = {
comment: false,
message: "",
warning: false
}.merge(options)

type = options[:warning] ? "Warning" : "Error"

error_background_color = options[:warning] ? "#9BD3DD" : "red"
error_text_color = options[:warning] ? "black" : "white"
span_style = "color: #000; border: 1px dashed #{error_background_color}; background-color: #fff; border-radius: 3px; font-family: monospace; padding: 0 3px 0 0;"
error_style= "color: #{error_text_color}; background-color: #{error_background_color}; border-radius: 3px 0 0 3px; padding: 0 3px; font-family: monospace;"

console_error = "Hammer #{type}: #{message.gsub(/(<[^>]*>)|\n|\t/s) {""}}"
puts console_error.colorize(:red)

error = "<strong style='#{error_style}'>Hammer #{type}:</strong> #{message}"
if options[:comment]
"<!-- #{console_error} #{options[:message]} -->"
else
"<span style='#{span_style}'>#{error} #{options[:message]}</span>"
end
end

def self.key_missing(key,options={})
options = {
parent_key: nil,
comment: false,
message: ""
}.merge(options)

style = "background-color: #eee; border-radius: 3px; font-family: monospace; padding: 0 3px;"
if options[:parent_key]
error("Missing key <code style='#{style}'>#{key}:</code> under <code style='#{style}'>#{options[:parent_key]}:</code> in mock_data.yml", options)
else
error("Missing key <code style='#{style}'>#{key}:</code> in mock_data.yml", options)
end
end

end
36 changes: 36 additions & 0 deletions hammer/services/dentaku_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Dentaku
module CustomFunctions
FUNCTIONS = [
[
:length,
:numeric,
->(str) { str.length }
],
[
:blank,
:logical,
->(str) { str.blank? }
],
[
:contains,
:logical,
->(mainStr, subStr) { mainStr.include?(subStr) }
],
[
:startswith,
:logical,
->(mainStr, subStr) { mainStr.starts_with?(subStr) }
],
[
:endswith,
:logical,
->(mainStr, subStr) { mainStr.ends_with?(subStr) }
],
[
:matches,
:logical,
->(str, regexStr) { !Regexp.new(regexStr).match(str).nil? }
]
]
end
end
8 changes: 6 additions & 2 deletions hammer/services/tag_binding.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'dentaku'
require '../hammer/services/dentaku_ext.rb'

module Radius
class TagBinding

CALCULATOR = Dentaku::Calculator.new
CALCULATOR.add_functions(Dentaku::CustomFunctions::FUNCTIONS)

# Evaluates the current tag and returns the rendered contents.
def expand(newcontext=nil,oldcontext=nil)
Expand Down Expand Up @@ -36,7 +38,7 @@ def attributes
begin
memo[k] = parse_value(v)
rescue Exception => e
memo[k] = Hammer.error "<strong>Attribute Error</strong> #{name} #{e}"
Hammer.error "<strong>Attribute Error</strong> #{k} #{e}"
#raise AttributeParseError.new(name, k, v, e.backtrace)
end

Expand Down Expand Up @@ -112,7 +114,8 @@ def math_dictionary
memo
end
end

# This method is used to parse tag attributes that may refer to variables or
# other tags
def parse_value(v)
str = v.to_s.strip
if str =~ /^{\s*\$(\w+)\s*}$/
Expand All @@ -129,5 +132,6 @@ def parse_value(v)
v
end
end

end
end
89 changes: 32 additions & 57 deletions hammer/services/tags/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,19 @@ class Asset < TagContainer
%w(id title name alt_text description).each do |mthd|
tag "file:#{mthd}" do |tag|
tag.locals.asset[mthd.to_sym]
#Hammer.error "file:#{mthd} tag is not implemented yet"
end
end

tag 'file:filename' do |tag|
# tag.locals.asset.try(:filename)
asset = tag.locals.asset
#Hammer.error "file:filename tag is not implemented yet"
asset[:filename]
tag.locals.asset[:filename]
end

tag 'file:download_url' do |tag|
asset = tag.locals.asset
# asset.public_download_url
#Hammer.error "file:download_url tag is not implemented yet"
asset[:download_url]
tag.locals.asset[:download_url]
end

tag 'file:image_url' do |tag|
asset = tag.locals.asset
# size = tag.attr['size']
#
# if asset.is_a?(ImageAsset)
# asset.image_url(size)
# end
asset[:image_url]
tag.locals.asset[:image_url]

end

Expand All @@ -59,73 +46,61 @@ class Asset < TagContainer
%w(id class alt title).each do |opt|
options[opt.to_sym] = tag.attr[opt] if tag.attr.has_key?(opt)
end
#
# if asset.is_a?(ImageAsset)

options[:alt] ||= asset[:alt_text]
# asset.image_tag(size, options)
# end
#Hammer.error "file:image_tag is not implemented yet"

content_tag :img, { :src=>asset[:image_url], :id => options[:id], :class => options[:class], :alt => options[:alt], :title => options[:title]}
content_tag :img, {
src: asset[:image_url],
id: options[:id],
class: options[:class],
alt: options[:alt],
title: options[:title]
}
end

# <r:files labels="foo,bar" labels_match="any|all|none" by="name|title|size|etc" order="asc|desc"/>
tag 'files' do |tag|
# tag.locals.assets = find_with_options(tag, tag.globals.site.assets)
# tag.expand
if tag.globals.context.data && tag.globals.context.data['files']
if tag.globals.context.data['files']
tag.expand
else
Hammer.key_missing "files"
end

#Hammer.error "files tag is not implemented yet"
end

tag 'files:count' do |tag|
# count_items tag, tag.locals.assets
if tag.globals.context.data && tag.globals.context.data['files']
files = tag.globals.context.data['files']
loop_over(tag, files, false).count
end
tag.globals.context.data['files'].count
end

tag 'files:each' do |tag|
# loop_over tag, tag.locals.assets
if tag.globals.context.data && tag.globals.context.data['files']
#loop_over tag, tag.globals.context.data['files']
files = tag.globals.context.data['files']
loop_over tag, files
end

#Hammer.error "files:each tag is not implemented yet"
loop_over tag, tag.globals.context.data['files']
end

# def self.decorated_asset(asset)
# unless asset.is_a? ApplicationDecorator
# AssetDecorator.decorate(asset)
# end
# end
def self.decorated_asset(asset)
# unless asset.is_a? ApplicationDecorator
# AssetDecorator.decorate(asset)
# end
end

def self.find_with_options(tag, target)
conditions = tag.attr.symbolize_keys

filter = {
:title => conditions[:title],
:types => conditions[:types] || [],
:tags => conditions[:labels] || [],
:tags_op => conditions[:labels_match] || 'any',
:order => conditions[:by] || 'name',
:reverse_order => conditions[:order] == 'desc' ? '1' : '0',
:random => conditions[:random].to_s.to_b,
:page => conditions[:offset].present? ? conditions[:offset].to_i : 1,
:limit => conditions[:limit].present? ? conditions[:limit].to_i : 50
title: conditions[:title],
types: conditions[:types] || [],
tags: conditions[:labels] || [],
tags_op: conditions[:labels_match] || 'any',
order: conditions[:by] || 'name',
reverse_order: conditions[:order] == 'desc' ? '1' : '0',
random: conditions[:random].to_s.to_b,
page: conditions[:offset].present? ? conditions[:offset].to_i : 1,
limit: conditions[:limit].present? ? conditions[:limit].to_i : 50
}

# assets = Filter::Assets.new(target, filter).all
end

def self.count_items(tag, target)
items = find_with_options(tag, target)
items.reorder(nil).count # Order is irrelevant for counting
# items = find_with_options(tag, target)
# items.reorder(nil).count # Order is irrelevant for counting
end

def self.loop_over(tag, target, out=true)
Expand Down
4 changes: 2 additions & 2 deletions hammer/services/tags/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Basic < TagContainer
if tag.globals.context.data && tag.globals.context.data['site'] && tag.globals.context.data['site'][attr.to_s]
tag.globals.context.data['site'][attr.to_s]
else
Hammer.error "Missing key <em>#{attr}</em> under <em>site:</em>"
Hammer.key_missing attr, {parent_key: "site"}
end
end
end
Expand All @@ -56,7 +56,7 @@ class Basic < TagContainer
if tag.globals.context.data && tag.globals.context.data['site'] && tag.globals.context.data['site']['data'] && tag.globals.context.data['site']['data'][attr]
tag.globals.context.data['site']['data'][attr]
else
Hammer.error "Missing key <em>#{attr}</em> under <em>site:data</em>"
Hammer.key_missing attr, {parent_key: "site:data"}
end
end

Expand Down
Loading

0 comments on commit f0a5d49

Please sign in to comment.