Module: Chinese::ClassMethods

Defined in:
lib/chinese.rb

Instance Method Summary collapse

Instance Method Details

#sort_chi(*fields) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chinese.rb', line 6

def sort_chi(*fields)
  #this just gathers up the characters into an array -> @characters
  spec = Gem::Specification.find_by_name("a-b-chi")
  gem_root = spec.gem_dir
  @gem_lib = gem_root + "/lib"
  @characters = File.read("#{@gem_lib}/characters.txt").split("\n")
  #this section compiles the sort_query by checking the max length of the content of each of the supplied fields
  sort_query = '['
  fields.each do |field|
    puts "#{field} - " + self.maximum("#{field}").length.to_s
    (0..(self.maximum("#{field}").length-1)).each { |i| sort_query << " @characters.index(sort_string.#{field}[#{i}]),"}
  end
  sort_query.chop!
  sort_query << ']'
  #this should sort the collection (and does when run outside of this module but within the module i get the no method error for sort_by)
  self.all.sort_by { |sort_string| eval(sort_query) }
end

#sort_chi!(*fields) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chinese.rb', line 24

def sort_chi!(*fields)
  #this just gathers up the characters into an array -> @characters
  spec = Gem::Specification.find_by_name("a-b-chi")
  gem_root = spec.gem_dir
  @gem_lib = gem_root + "/lib"
  @characters = File.read("#{@gem_lib}/characters.txt").split("\n")
  #this section compiles the sort_query by checking the max length of the content of each of the supplied fields
  sort_query = '['
  fields.each do |field|
    puts "#{field} - " + self.maximum("#{field}").length.to_s
    (0..(self.maximum("#{field}").length-1)).each { |i| sort_query << " @characters.index(sort_string.#{field}[#{i}]),"}
  end
  sort_query.chop!
  sort_query << ']'
  #this should sort the collection (and does when run outside of this module but within the module i get the no method error for sort_by)
  self.all.sort_by! { |sort_string| eval(sort_query) }
end