Class: KLookup::Lookup::Radical

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

Overview

An abstract representation of radicals.

Constant Summary collapse

@@data =

A class variable because we have class methods which need this.

KLookup::Lookup.default_handler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(radical) ⇒ Radical

Returns a new instance of Radical.



18
19
20
21
22
23
# File 'lib/klookup/lookup_radical.rb', line 18

def initialize(radical)
  unless @@data.instance.is_radical?(radical)
    raise ArgumentError
  end
  @radical = radical.to_s
end

Class Method Details

.exist?(radical) ⇒ Boolean

Returns true if radical exists in the database.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/klookup/lookup_radical.rb', line 53

def self.exist?(radical)
  begin
    new(radical)
    return true
  rescue ArgumentError
    return false
  end
end

.handlerObject

Get handler used by this class.



26
27
28
# File 'lib/klookup/lookup_radical.rb', line 26

def self.handler
  @@data
end

.handler=(h) ⇒ Object

Set handler used by this class (see also Lookup.handler=).



31
32
33
# File 'lib/klookup/lookup_radical.rb', line 31

def self.handler=(h)
  @@data = h
end

.list(strokes = nil) ⇒ Object

Returns an array of radicals with strokes strokes. Without an argument, all radicals are returned.



42
43
44
45
46
47
48
49
50
# File 'lib/klookup/lookup_radical.rb', line 42

def self.list(strokes=nil)
  if strokes.nil?
    @@data.instance.radicals_by_strokes.values.flatten
  else
    raise ArgumentError unless strokes.respond_to?(:to_i)

    @@data.instance.radicals_by_strokes[strokes.to_i]
  end
end

.strokesObject

Return an array of how many strokes radicals can be made up of.



36
37
38
# File 'lib/klookup/lookup_radical.rb', line 36

def self.strokes
  @@data.instance.stroke_count_list
end

Instance Method Details

#==(radical) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/klookup/lookup_radical.rb', line 62

def == (radical)
  unless radical.is_a? KLookup::Lookup::Radical
    radical = KLookup::Lookup::Radical.new(radical.to_s)
  end
  return true if radical.to_s == to_s
  false
end

#to_sObject

Returns a textual representation of a radical.



71
72
73
# File 'lib/klookup/lookup_radical.rb', line 71

def to_s
  @radical
end