Class: Nonsense

Inherits:
Object
  • Object
show all
Defined in:
lib/nonsense.rb

Defined Under Namespace

Modules: Data

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, data = {}) ⇒ Nonsense

Returns a new instance of Nonsense.



12
13
14
15
16
# File 'lib/nonsense.rb', line 12

def initialize(template, data = {})
  @template = template
  @data     = Data::DEFAULT.merge(data)
  @state    = {}
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/nonsense.rb', line 6

def data
  @data
end

#debugObject

Returns the value of attribute debug.



6
7
8
# File 'lib/nonsense.rb', line 6

def debug
  @debug
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/nonsense.rb', line 6

def state
  @state
end

#templateObject

Returns the value of attribute template.



6
7
8
# File 'lib/nonsense.rb', line 6

def template
  @template
end

Instance Method Details

#adjust_case(parsed, parse_case) ⇒ Object

:nodoc:



125
126
127
128
129
130
131
132
# File 'lib/nonsense.rb', line 125

def adjust_case(parsed, parse_case)  #:nodoc:
  case parse_case
  when /^[A-Z0-9]+$/    then parsed.upcase
  when /^[a-z0-9]+$/    then parsed.downcase
  when /^\^/            then parsed.capitalize
  else                       parsed
  end
end

#assign_command(key, command) ⇒ Object

:nodoc:



98
99
100
101
# File 'lib/nonsense.rb', line 98

def assign_command(key, command) #:nodoc:
  state[key.downcase] = parse(command)
  ''
end

#assign_literal(key, value) ⇒ Object

:nodoc:



103
104
105
106
# File 'lib/nonsense.rb', line 103

def assign_literal(key, value)  #:nodoc:
  state[key.downcase] = value
  ''
end

#choose_from_list(list) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
# File 'lib/nonsense.rb', line 78

def choose_from_list(list) #:nodoc:
  items = list.split('|')
  choice = if items.size > 1
    items[rand(items.size)]       # pick one of the elements
  else
    rand(2) == 0 ? items[0] : ''  # pick it or pick nothing
  end
end

#embed_character(character) ⇒ Object

:nodoc:



87
88
89
90
91
92
93
94
95
96
# File 'lib/nonsense.rb', line 87

def embed_character(character)  #:nodoc:
  case character
  when 'n'    then "\n"               # newline
  when '0'    then ''                 # null
  when 'L'    then '{'                # left bracket
  when 'R'    then '}'                # right bracket
  when /^\d+/ then character.to_i.chr # ASCII code in decimal
  else ''                             # not in list
  end
end

#format_assigned_time(format, key, high) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nonsense.rb', line 66

def format_assigned_time(format, key, high) #:nodoc:
  existing_time = state[key.downcase]
  elapsed = rand(high)
  time = if existing_time
    existing_time - elapsed
  else
    Time.now - elapsed
  end
  state[key] = time
  time.strftime format
end

#format_current_time(format, low = nil, high = nil) ⇒ Object

:nodoc:



61
62
63
64
# File 'lib/nonsense.rb', line 61

def format_current_time(format, low = nil, high = nil) #:nodoc:
  diff = (low && high) ? number_in_range(low, high) : 0
  (Time.now - diff).strftime format
end

#number_in_range(low, high) ⇒ Object

:nodoc:



57
58
59
# File 'lib/nonsense.rb', line 57

def number_in_range(low, high) #:nodoc:
  rand(high - low).to_i + low
end

#parse(tag) ⇒ Object

:nodoc:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nonsense.rb', line 29

def parse(tag) #:nodoc:
  return '' unless tag
  parse_case = nil
  parsed = case tag
  when /^#(\d+)-(\d+)$/            then number_in_range($1.to_i, $2.to_i).to_s
  when /^\@([^|]+)$/               then format_current_time($1)
  when /^\@(.*?)\|\$(\w+)\|(\d+)$/ then format_assigned_time($1, $2, $3)
  when /^\@(.*?)\|(\d+)\|(\d+)$/   then format_current_time($1, $2.to_i, $3.to_i)
  when /^\[(.*)$/                  then choose_from_list($1)
  when /^\\(.*)$/                  then embed_character($1)
  when /^(.*?):=(.*)$/             then assign_command($1, $2)
  when /^(.*?)=(.*)$/              then assign_literal($1, $2)
  when /^[\$<](.*)$/
    parse_case = $1
    retrieve_assigned($1)
  when /^(.*?)#(\d+)-(\d+)$/
    parse_case = $1
    retrieve_multiple($1, $2.to_i, $3.to_i)
  else
    parse_case = tag
    retrieve_data tag.gsub(/\W/, '')
  end
  
  parsed = parse_template(parsed)

  parsed = adjust_case parsed, parse_case
end

#parse_template(t) ⇒ Object

:nodoc:



22
23
24
25
26
27
# File 'lib/nonsense.rb', line 22

def parse_template(t) #:nodoc:
  return '' unless t
  t = t.dup
  copy = t.gsub!(/\{[^}]+\}/) { |tag| parse tag[1..-2] }
  copy ? copy : t
end

#resultObject



18
19
20
# File 'lib/nonsense.rb', line 18

def result
  parse_template self.template
end

#retrieve_assigned(key) ⇒ Object

:nodoc:



108
109
110
111
# File 'lib/nonsense.rb', line 108

def retrieve_assigned(key)  #:nodoc:
  value = state[key.downcase]
  value ? value : parse(key)
end

#retrieve_data(key) ⇒ Object

:nodoc:



119
120
121
122
123
# File 'lib/nonsense.rb', line 119

def retrieve_data(key)  #:nodoc:
  return '' unless key
  choices = data[key.downcase]
  choices ? choices[rand(choices.size)] : ''
end

#retrieve_multiple(key, low, high) ⇒ Object

:nodoc:



113
114
115
116
117
# File 'lib/nonsense.rb', line 113

def retrieve_multiple(key, low, high)  #:nodoc:
  n = number_in_range(low, high)
  return '' if n == 0
  (1..n).collect { |i| parse(key) }.join(" ")
end