module Phil
Constants
- VERSION
Public Class Methods
address(include_secondary = false)
click to toggle source
# File lib/phil.rb, line 110 def address(include_secondary = false) FFaker::Address.street_address(include_secondary) end
blockquote(paragraphs = nil)
click to toggle source
# File lib/phil.rb, line 46 def blockquote(paragraphs = nil) tag "blockquote", paragraphs end
city()
click to toggle source
# File lib/phil.rb, line 82 def city FFaker::Address.city end
company_name()
click to toggle source
# File lib/phil.rb, line 106 def company_name FFaker::Company.name end
currency(num, symbol = "$")
click to toggle source
# File lib/phil.rb, line 58 def currency(num, symbol = "$") val = ((pick(num) * 100) / 100).round(2) + pick(0..99).to_f / 100 sprintf("$%.2f", val) end
date(day_window = nil)
click to toggle source
# File lib/phil.rb, line 71 def date(day_window = nil) now = Time.now.to_f if !day_window Time.at(now * rand) elsif day_window.respond_to?(:to_a) Time.at(now - pick(day_window) * 86400) else Time.at(now - rand * day_window * 86400) end end
domain_name()
click to toggle source
# File lib/phil.rb, line 86 def domain_name FFaker::Internet.domain_name end
email()
click to toggle source
# File lib/phil.rb, line 90 def email FFaker::Internet.email end
first_name()
click to toggle source
# File lib/phil.rb, line 94 def first_name FFaker::Name.first_name end
image(*arguments)
click to toggle source
# File lib/phil.rb, line 122 def image(*arguments) opts = arguments.extract_options! opts = format_image_argument_output(opts.merge! parse_image_arguments(arguments)) opts[:size] && "http://placehold.it/#{opts[:size]}#{opts[:color]}#{opts[:text]}" end
last_name()
click to toggle source
# File lib/phil.rb, line 98 def last_name FFaker::Name.last_name end
link_list(list_items = (3..10), item_length = (1..5))
click to toggle source
# File lib/phil.rb, line 50 def link_list(list_items = (3..10), item_length = (1..5)) build_tag "ul", build_tags("li", -> { "<a href='#'>#{words(item_length)}</a>" }, list_items) end
loop(num) { |i| ... }
click to toggle source
# File lib/phil.rb, line 11 def loop(num) pick(num).times do |i| yield(i) end end
markup(pattern="h1 p p h2 p ol h2 p ul")
click to toggle source
# File lib/phil.rb, line 54 def markup(pattern="h1 p p h2 p ol h2 p ul") pattern.split(" ").map{ |t| tag(t) }.join.html_safe end
Also aliased as: body_content
name()
click to toggle source
# File lib/phil.rb, line 102 def name FFaker::Name.name end
number(length)
click to toggle source
# File lib/phil.rb, line 63 def number(length) (1..pick(length)).map { rand(10) }.join end
ol(list_items = nil, item_length = nil)
click to toggle source
# File lib/phil.rb, line 42 def ol(list_items = nil, item_length = nil) tag "ol", item_length, list_items end
paragraphs(num)
click to toggle source
# File lib/phil.rb, line 33 def paragraphs(num) content_method = -> { FFaker::Lorem.paragraphs(1).join } build_tags "p", content_method, pick(num) end
phone(format = "(
click to toggle source
# File lib/phil.rb, line 67 def phone(format = "(###) ###-####") format.gsub(/#/) { rand(9) + 1 } end
pick(num)
click to toggle source
# File lib/phil.rb, line 7 def pick(num) num.respond_to?(:to_a) ? num.to_a.sample : num end
sometimes(num_or_content = 3, num = 3) { || ... }
click to toggle source
# File lib/phil.rb, line 17 def sometimes(num_or_content = 3, num = 3) if block_given? if num_or_content == pick(1..num_or_content) yield end else if num == pick(1..num) num_or_content end end end
state()
click to toggle source
# File lib/phil.rb, line 114 def state FFaker::AddressUS.state end
state_abbr()
click to toggle source
# File lib/phil.rb, line 118 def state_abbr FFaker::AddressUS.state_abbr end
ul(list_items = nil, item_length = nil)
click to toggle source
# File lib/phil.rb, line 38 def ul(list_items = nil, item_length = nil) tag "ul", item_length, list_items end
words(num)
click to toggle source
# File lib/phil.rb, line 29 def words(num) FFaker::Lorem.words(pick(num)).join(' ').html_safe end
Private Class Methods
build_tag(name, content)
click to toggle source
# File lib/phil.rb, line 154 def build_tag(name, content) "<#{name}>#{content}</#{name}>".html_safe end
convert_range(str)
click to toggle source
# File lib/phil.rb, line 158 def convert_range(str) range_ends = str.split('..') str = range_ends[1] ? range_ends[0]..range_ends[1] : range_ends[0] end
format_image_argument_output(args)
click to toggle source
# File lib/phil.rb, line 178 def format_image_argument_output(args) if !args[:color] args[:color] = "#{args[:background]}#{args[:foreground]}" end if args[:width] && args[:height] args[:size] = "#{Phil.pick(args[:width])}x#{Phil.pick(args[:height])}" end args.each do |k, v| case k when /color/ args[k] = v.gsub(/\/?#/, '/') when /text/ args[:text] = "&text=#{v.gsub(/[^\d\w\!,\.;:-]+/, '+').gsub(/\+?$/, '')}" end end end
parse_image_arguments(args)
click to toggle source
# File lib/phil.rb, line 163 def parse_image_arguments(args) output = {} args.each do |arg| case arg.to_s when /^(#[a-f\d]{3,6}(\/(?=#))?){1,2}$/ output[:color] = arg when /^([\d\.]*)x?([\d\.]*?)$/ output[:size] = arg else output[:text] = arg end end output end
tag(name, content = nil, children = nil)
click to toggle source
# File lib/phil.rb, line 132 def tag(name, content = nil, children = nil) case name when "ul", "ol" content ||= (3..15) children ||= (3..10) build_tag name, build_tags("li", content, children) when "blockquote" content ||= (1..3) build_tag name, paragraphs(content) when "p" paragraphs(1) else content ||= (3..15) build_tags name, content end end