module YeshouaCrm::Liquid::Filters::Base
Public Instance Methods
attachment(input, file_name)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 106 def attachment(input, file_name) if input.respond_to?(:attachments) if input.attachments.is_a?(Hash) attachment = input.attachments[file_name] else attachment = input.attachments.detect{|a| a.file_name == file_name} end AttachmentDrop.new attachment if attachment end end
call_method(input, method_name)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 94 def call_method(input, method_name) if input.respond_to?(method_name) input.method(method_name).call end end
ceil(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 82 def ceil(input) to_number(input).ceil.to_i end
currency(input, currency_code = 'BRL')
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 90 def currency(input, currency_code = 'BRL') price_to_currency(input, currency_code, :converted => false) end
custom_field(input, field_name)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 100 def custom_field(input, field_name) if input.respond_to?(:custom_fields) input.custom_fields[field_name] end end
dasherize(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 25 def dasherize(input) input.to_s.gsub(' ', '-').gsub('/', '-').dasherize end
date_range(input, distanse)
click to toggle source
example:
{{ today | date_range: '2015-12-12' }}
# File lib/yeshoua_crm/liquid/filters/base.rb, line 59 def date_range(input, distanse) return '' if input.nil? (input.to_date - distanse.to_date).to_i rescue 'Invalid date' end
default(input, value)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 17 def default(input, value) input.blank? ? value : input end
encode(input)
click to toggle source
example:
{{ "http:://www.example.com?key=hello world" | encode }} => http%3A%3A%2F%2Fwww.example.com%3Fkey%3Dhello+world
# File lib/yeshoua_crm/liquid/filters/base.rb, line 45 def encode(input) ::Rack::Utils.escape(input) end
floor(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 86 def floor(input) to_number(input).floor.to_i end
md5(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 37 def md5(input) Digest::MD5.hexdigest(input) unless input.blank? end
modulo(input, operand)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 71 def modulo(input, operand) apply_operation(input, operand, :%) end
plus_days(input, distanse)
click to toggle source
example:
{{ today | plus_days: 2 }}
# File lib/yeshoua_crm/liquid/filters/base.rb, line 51 def plus_days(input, distanse) return '' if input.nil? days = distanse.to_i input.to_date + days.days rescue 'Invalid date' end
random(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 33 def random(input) rand(input.to_i) end
round(input, n = 0)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 75 def round(input, n = 0) result = to_number(input).round(to_number(n)) result = result.to_f if result.is_a?(BigDecimal) result = result.to_i if n == 0 result end
shuffle(array)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 29 def shuffle(array) array.to_a.shuffle end
textilize(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 13 def textilize(input) RedCloth3.new(input).to_html end
underscore(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 21 def underscore(input) input.to_s.gsub(' ', '_').gsub('/', '_').underscore end
utc(input)
click to toggle source
example:
{{ now | utc }}
# File lib/yeshoua_crm/liquid/filters/base.rb, line 66 def utc(input) return '' if input.nil? input.to_time.utc rescue 'Invalid date' end
Protected Instance Methods
args_to_options(*args)
click to toggle source
Convert an array of properties ('key:value') into a hash Ex: ['width:50', 'height:100'] => { :width => '50', :height => '100' }
# File lib/yeshoua_crm/liquid/filters/base.rb, line 121 def args_to_options(*args) options = {} args.flatten.each do |a| if (a =~ /^(.*):(.*)$/) options[$1.to_sym] = $2 end end options end
as_liquid(item)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 183 def as_liquid(item) case item when Hash pairs = item.map { |k, v| as_liquid([k, v]) } Hash[pairs] when Array item.map { |i| as_liquid(i) } else if item.respond_to?(:to_liquid) liquidated = item.to_liquid # prevent infinite recursion for simple types (which return `self`) if liquidated == item item else as_liquid(liquidated) end else item end end end
groupable?(element)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 169 def groupable?(element) element.respond_to?(:group_by) end
inline_options(options = {})
click to toggle source
Write options (Hash) into a string according to the following pattern: <key1>=“<value1>”, <key2>=“<value2”, …etc
# File lib/yeshoua_crm/liquid/filters/base.rb, line 133 def inline_options(options = {}) return '' if options.empty? (options.stringify_keys.sort.to_a.collect { |a, b| "#{a}=\"#{b}\"" }).join(' ') << ' ' end
item_property(item, property)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 173 def item_property(item, property) if item.respond_to?(:to_liquid) item.to_liquid[property.to_s] elsif item.respond_to?(:data) item.data[property.to_s] else item[property.to_s] end end
sort_input(input, property, order)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 138 def sort_input(input, property, order) input.sort do |apple, orange| apple_property = item_property(apple, property) orange_property = item_property(orange, property) if !apple_property.nil? && orange_property.nil? - order elsif apple_property.nil? && !orange_property.nil? + order else apple_property <=> orange_property end end end
time(input)
click to toggle source
# File lib/yeshoua_crm/liquid/filters/base.rb, line 153 def time(input) case input when Time input.clone when Date input.to_time when String Time.parse(input) rescue Time.at(input.to_i) when Numeric Time.at(input) else raise Errors::InvalidDateError, "Invalid Date: '#{input.inspect}' is not a valid datetime." end.localtime end