Class: FSymbol

Inherits
Object show all
Defined in
lib/kyanite/fsymbol.rb

CallerUtils

Kyanite definitions

FSymbol

Kyanite tests and examples

TestKyaniteFSymbols

Usage

require ‘kyanite/fsymbol’

A FSymbol (“focusable Symbol”) is a comparable Symbol within a hierarchy. You can use it for classifications. In its values​​, it is limited to the values​​, which are defined in the hierarchy. FSymbols are comparable with <= >= <=>. A more specific FSymbol is larger than a more general FSymbol (because the specific FSymbol contains more information) E.g. if you define a hierarchy if animal species, you can say:

:insect.to_f_symbol <= :bee.to_f_symbol

FSymbols are equal to Symbols with the same name.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constant Summary

@@instance_cache =
{}
@@def_tree_cache =
nil

Constructor Details

- (FSymbol) initialize(value, def_tree = nil)

A new instance of FSymbol

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kyanite/fsymbol.rb', line 29

def initialize( value, def_tree=nil)
  raise ArgumentError, 'First parameter (value) must not be nil!'       if value.nil?    
  @value = value    
  
  if @@instance_cache[value]
    @childs = @@instance_cache[value].childs 
    return @@instance_cache[value] 
  end
  
  if def_tree.nil?
    def_tree = @@def_tree_cache
  else
    @@def_tree_cache = def_tree
  end
  raise ArgumentError, "FSymbol could not be created. No def_tree."                                 if def_tree.nil?     
  tree = def_tree.find(value) || nil
  raise ArgumentError, "FSymbol could not be created. Did not found value :#{value} in def_tree."   if tree.nil? 
  
  @childs = tree.all_child_keys    
  @@instance_cache[value] = self
  self
end

Instance Attribute Details

- (Object) childs

Returns the value of attribute childs



16
17
18
# File 'lib/kyanite/fsymbol.rb', line 16

def childs
  @childs
end

- (Object) value

Returns the value of attribute value



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

def value
  @value
end

Instance Method Details

- (Object) <=(other)



83
84
85
86
87
88
89
90
91
# File 'lib/kyanite/fsymbol.rb', line 83

def <=(other)
  return nil       if other.nil?
  begin
    c = (self <=> other)
    (c == -1 || c == 0)
  rescue
    nil
  end
end

- (Object) <=>(other)



63
64
65
66
67
68
69
70
# File 'lib/kyanite/fsymbol.rb', line 63

def <=>(other)
  return nil       if other.nil?
  other = other.to_fsymbol  unless other.class == FSymbol
  return 0   if @value == other.value    
  return -1  if @childs.include?(other.value)
  return 1   if other.childs.include?(@value) 
  return nil
end

- (Object) ==(other)



73
74
75
76
77
78
79
80
# File 'lib/kyanite/fsymbol.rb', line 73

def ==(other)
  return nil       if other.nil?
  begin
    (self <=> other) == 0
  rescue
    nil
  end
end

- (Object) >=(other)



94
95
96
97
98
99
100
101
102
# File 'lib/kyanite/fsymbol.rb', line 94

def >=(other)
  return nil       if other.nil?
  begin
    c = (self <=> other)
    (c == 1 || c == 0)
  rescue
    nil
  end
end

- (Object) inspect



58
59
60
# File 'lib/kyanite/fsymbol.rb', line 58

def inspect
  @value.inspect
end

- (Boolean) nil?

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/kyanite/fsymbol.rb', line 105

def nil?
  return true if @value.nil?
  false
end

- (Object) to_s



53
54
55
# File 'lib/kyanite/fsymbol.rb', line 53

def to_s
  @value.to_s
end