Class: Bones::Common
- Inherits:
-
Object
- Object
- Bones::Common
- Defined in:
- lib/bones.rb
Overview
This class is created to be a parent class of the Bones engine, the Bones species and the Bones algorithm class.
Instance Method Summary collapse
-
#flatten_hash(hash, flat_hash = {}, prefix = '') ⇒ Object
This method flattens a multidimensional hash into a one dimensional hash.
-
#from(range) ⇒ Object
Helper to obtain the ‘from’ part from a range.
-
#replace_defines(original_code, defines) ⇒ Object
Method to process the defines in a piece of code.
-
#search_and_replace(hash, code) ⇒ Object
This method calls search_and_replace! to replaces markers in code with a hash of search-and-replace values.
-
#search_and_replace!(hash, code) ⇒ Object
This method performs a search-and-replace.
-
#sum(range) ⇒ Object
Helper to obtain the sum of a range.
-
#sum_and_from(range) ⇒ Object
Helper to obtain the sum and ‘from’ part of a range.
-
#to(range) ⇒ Object
Helper to obtain the ‘to’ part from a range.
Instance Method Details
#flatten_hash(hash, flat_hash = {}, prefix = '') ⇒ Object
This method flattens a multidimensional hash into a one dimensional hash. This method is called recursively.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bones.rb', line 152 def flatten_hash(hash,flat_hash={},prefix='') hash.each do |key,value| if value.is_a?(Hash) flatten_hash(value,flat_hash,prefix+key.to_s+'_') else flat_hash[(prefix+key.to_s).to_sym] = value end end return flat_hash end |
#from(range) ⇒ Object
Helper to obtain the ‘from’ part from a range. For example, the method will yield ‘1’ if applied to ‘1:N-1’.
124 125 126 |
# File 'lib/bones.rb', line 124 def from(range) return '('+simplify(range.split(RANGE_SEP)[0])+')' end |
#replace_defines(original_code, defines) ⇒ Object
Method to process the defines in a piece of code. The code must be formatted as a string. It returns a copy of the code with all defines replaced.
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/bones.rb', line 190 def replace_defines(original_code,defines) code = original_code.clone list = defines.sort_by { |key,value| key.to_s.length } list.reverse.each do |pair| search = pair[0].to_s replace = pair[1] code.gsub!(search,replace) end return code end |
#search_and_replace(hash, code) ⇒ Object
This method calls search_and_replace! to replaces markers in code with a hash of search-and-replace values. Before, it clones the existing code so that the original copy is maintained.
181 182 183 184 185 |
# File 'lib/bones.rb', line 181 def search_and_replace(hash,code) new_code = code.clone search_and_replace!(hash,new_code) return new_code end |
#search_and_replace!(hash, code) ⇒ Object
This method performs a search-and-replace. It searches for the <index> of the input hash and replaces it with the corresponding key. It searches for to-be-replaced variables of the form ‘<name>’. If such patterns still occur after searching and replacing, the method raises an error.
169 170 171 172 173 174 175 |
# File 'lib/bones.rb', line 169 def search_and_replace!(hash,code) flat_hash = flatten_hash(hash) 2.times do flat_hash.each { |search,replace| code.gsub!(SAR_MARKER1+search.to_s+SAR_MARKER2,replace) } end raise_error('Unrecognized replace variable "'+($~).to_s+'" in the skeleton library') if code =~ /<[a-zA-Z]+\w*>/ end |
#sum(range) ⇒ Object
Helper to obtain the sum of a range. For example, the method will yield ‘N-2’ if applied to ‘1:N-1’. There is a check to ensure that the range is correct.
138 139 140 141 |
# File 'lib/bones.rb', line 138 def sum(range) raise_error('Incorrect range given: "'+range+'"') if range.split(RANGE_SEP).length != 2 return '('+simplify("(#{to(range)}-#{from(range)}+1)")+')' end |
#sum_and_from(range) ⇒ Object
Helper to obtain the sum and ‘from’ part of a range. For example, the method will yield ‘((N-2)+1)’ if applied to ‘1:N-1’.
146 147 148 |
# File 'lib/bones.rb', line 146 def sum_and_from(range) return '('+simplify(sum(range)+'+'+from(range))+')' end |
#to(range) ⇒ Object
Helper to obtain the ‘to’ part from a range. For example, the method will yield ‘N-1’ if applied to ‘1:N-1’.
131 132 133 |
# File 'lib/bones.rb', line 131 def to(range) return '('+simplify(range.split(RANGE_SEP)[1])+')' end |