Class: Interscript::Node::Item::Any

Inherits:
Interscript::Node::Item show all
Defined in:
lib/interscript/node/item/any.rb,
lib/interscript/visualize/nodes.rb

Instance Attribute Summary collapse

Attributes inherited from Interscript::Node::Item

#item

Instance Method Summary collapse

Methods inherited from Interscript::Node::Item

#+, try_convert

Constructor Details

#initialize(data) ⇒ Any

Returns a new instance of Any.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/interscript/node/item/any.rb', line 3

def initialize data
  case data
  when Array, ::String, Range
    self.value = data
  when Interscript::Node::Item::Group # debug alalc-ara-Arab-Latn-1997  line 683
    self.value = data.children
  when Interscript::Node::Item::Alias # debug mofa-jpn-Hrkt-Latn-1989 line 116
    self.value = Interscript::Stdlib::ALIASES[data.name]
  else
    puts data.inspect
    raise Interscript::MapLogicError, "Wrong type #{data[0].class}, excepted Array, String or Range"
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



2
3
4
# File 'lib/interscript/node/item/any.rb', line 2

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
# File 'lib/interscript/node/item/any.rb', line 76

def ==(other)
  super && self.data == other.data
end

#dataObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/interscript/node/item/any.rb', line 17

def data
  case @value
  when Array
    value.map { |i| Interscript::Node::Item.try_convert(i) }
  when ::String
    value.split("").map { |i| Interscript::Node::Item.try_convert(i) }
  when Range
    value.map { |i| Interscript::Node::Item.try_convert(i) }
  end
end

#downcaseObject



28
# File 'lib/interscript/node/item/any.rb', line 28

def downcase; self.class.new(self.data.map(&:downcase)); end

#first_stringObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/interscript/node/item/any.rb', line 31

def first_string
  case @value
  when Array
    Interscript::Node::Item.try_convert(value.first).first_string
  when ::String
    value[0]
  when Range
    value.begin
  end
end

#inspectObject



80
81
82
# File 'lib/interscript/node/item/any.rb', line 80

def inspect
  "any(#{value.inspect})"
end

#max_lengthObject



52
53
54
# File 'lib/interscript/node/item/any.rb', line 52

def max_length
  self.data.map(&:max_length).max
end

#nth_stringObject



42
43
44
45
46
47
48
49
50
# File 'lib/interscript/node/item/any.rb', line 42

def nth_string
  return first_string unless $select_nth_string

  d = data
  Fiber.yield(:prepare)
  id = Fiber.yield(:select_nth_string, d.count, self.hash)
  Fiber.yield(:selection)
  Interscript::Node::Item.try_convert(value[id]).nth_string
end

#to_hashObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/interscript/node/item/any.rb', line 56

def to_hash
  hash = { :class => self.class.to_s }

  case @value
  when Array
    hash[:type] = "Array"
    hash[:data] = data.map { |i| i.to_hash }
  when ::String
    hash[:type] = "String"
    hash[:data] = @value
  when Range
    hash[:type] = "Range"
    hash[:data] = [@value.begin, @value.end]
  when NilClass
    hash[:type] = "nil (bug)"
  end

  hash
end

#to_html(doc) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/interscript/visualize/nodes.rb', line 25

def to_html(doc)
  "<nobr>any (</nobr>" +
    case @value
    when Array
      value.map(&Interscript::Node::Item.method(:try_convert)).map{|i|i.to_html(doc)}.join(", ")
    when ::String
      value.split("").map(&Interscript::Node::Item.method(:try_convert)).map{|i|i.to_html(doc)}.join(", ")
    when Range
      [value.begin, value.end].map(&Interscript::Node::Item.method(:try_convert)).map{|i|i.to_html(doc)}.join(" to ")
    else
      h(value.inspect)
    end +
  ")"
end

#upcaseObject



29
# File 'lib/interscript/node/item/any.rb', line 29

def upcase; self.class.new(self.data.map(&:upcase)); end