Class: Kor::Input::Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/kor/input/yaml.rb,
lib/kor/input/yaml/version.rb

Constant Summary collapse

DEFAULT_GUESS_TIME =
5
START =
"---"
VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Yaml

Returns a new instance of Yaml.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kor/input/yaml.rb', line 9

def initialize(io)
  @io = io
  @keys = []
  @yamls = []
  @prekeys = nil
  @count = 0
  @stock = []
  @guess_time = DEFAULT_GUESS_TIME
  @fiber = Fiber.new do
    @yamls.each do |yaml|
      Fiber.yield @keys.map{ |k| yaml[k] }
    end
    # gets should be return nil when last
    Fiber.yield nil
  end
end

Instance Method Details

#getsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kor/input/yaml.rb', line 52

def gets
  if @prekeys
    if yaml = read_yaml
      @keys.map{ |key| yaml[key] }
    else
      nil
    end
  elsif 0 < @guess_time
    if @count < @guess_time
      @count += 1
      return resume
    end
    if yaml = read_yaml
      @keys.map { |k| yaml[k] }
    else
      nil
    end
  else
    resume
  end
end

#headObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kor/input/yaml.rb', line 35

def head
  if @prekeys
    @keys = @prekeys.split(",")
  else
    while yaml = read_yaml
      @yamls << yaml
      if 0 < @guess_time && @guess_time <= @yamls.length
        break
      end
    end
    @keys = @yamls.map { |yaml| yaml.keys }
    @keys.flatten!
    @keys.uniq!
  end
  @keys
end

#parse(opt) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/kor/input/yaml.rb', line 26

def parse(opt)
  opt.on("--key=KEY", "select keys (e.g. foo,bar,baz)") do |arg|
    @prekeys = arg
  end
  opt.on("--guess-time=NUM", "guess number of times (default #{DEFAULT_GUESS_TIME})") do |arg|
    @guess_time = arg.to_i
  end
end