Class: Cheddar

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

Overview

The Cheddar class converts human input strings to numbers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCheddar

:notnew: nothing special going on here


7
8
9
10
# File 'lib/cheddar.rb', line 7

def initialize #:notnew: nothing special going on here
  @mapping = {}
  @enabled_dialects = []
end

Instance Attribute Details

#enabled_dialectsObject

Returns the value of attribute enabled_dialects


5
6
7
# File 'lib/cheddar.rb', line 5

def enabled_dialects
  @enabled_dialects
end

Class Method Details

.cheddarizeObject

Add human_to_number method to the String class using the Cheddar singleton


90
91
92
# File 'lib/cheddar.rb', line 90

def self.cheddarize
  instance.cheddarize
end

.config {|instance| ... } ⇒ Object

Get/Configure the Cheddar instance

:call-seq:

Cheddar.config -> instance
Cheddar.config { block } -> instance

Example

Cheddar.config do |c|
  ch.dialect(:example) do |d|
    d.define 'e', 20
  end
end

Yields:


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

def self.config
  yield instance if block_given?
  instance
end

.instanceObject

Get the singleton instance of cheddar applied by cheddarize


116
117
118
# File 'lib/cheddar.rb', line 116

def self.instance
  @instance ||= new
end

.parse(string) ⇒ Object

Parse using the singleton instance


123
124
125
# File 'lib/cheddar.rb', line 123

def self.parse(string)
  instance.parse(string)
end

Instance Method Details

#available_dialectsObject

Get the list of currently available dialects


15
16
17
# File 'lib/cheddar.rb', line 15

def available_dialects
  mapping.keys
end

#cheddarizeObject

Add human_to_number method to the String class


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

def cheddarize
  String.send(:define_method, :human_to_number) do
    Cheddar.parse(self)
  end
end

#define(word, number) ⇒ Object

Define a word to number mapping for the current dialect

:call-seq:

define(word, number) -> number

Parameters

  • word - string to be mapped to number

  • number - Number to return when an occurrence of string is found


38
39
40
41
# File 'lib/cheddar.rb', line 38

def define(word, number)
  mapping[current_dialect] ||= {}
  mapping[current_dialect][word] = number
end

#dialect(dia) {|self.class.instance| ... } ⇒ Object

Set the current dialect being operated on when using the define method and optionally pass in a block to define the dialect

:call-seq:

dialect(dialect_name) -> {'word' => value...}
dialect(dialect_name){ block } -> {'word' => value...}

Parameters

  • dialect_name - symbol for dialect being worked on

Example

ch = Cheddar.new
ch.dialect(:example) do |d|
  d.define 'e', 20
end
ch.parse('1 e') #=> 20

Yields:


62
63
64
65
66
# File 'lib/cheddar.rb', line 62

def dialect(dia)
  self.current_dialect = dia
  yield self.class.instance if block_given?
  mapping[dia]
end

#parse(string) ⇒ Object

convert the given string to it's numeric equivalent

Parameters

  • string - string to be parsed as a number


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cheddar.rb', line 75

def parse(string)
  str = string.downcase

  enabled_dialects.each do |d|
    mapping[d].each { |num_name, value| apply_mapping(num_name, value, str) }
  end

  str.gsub!(/[^\d\*\.]/, '')

  Kernel.eval(str)
end