Class: Tamiyo::Yaml::YamlSource

Inherits:
Object
  • Object
show all
Includes:
DataStore, Process::Node, Tamiyo::YamlHelper
Defined in:
lib/tamiyo/yaml/yaml_source.rb

Defined Under Namespace

Classes: Prototype

Instance Method Summary collapse

Methods included from DataStore

#default_set_data, #provide_path_for

Methods included from Tamiyo::YamlHelper

#emit_block_mapping, #emit_block_sequence, #emit_end_of_one_document, #emit_flow_sequence, #emit_literal, #emit_pair, #emit_pair_with_literal_value, #emit_pair_with_optional_sequence_value, #emit_pair_with_sequence_value, #emit_plain, #emit_start_of_one_document, #setup_yaml_emitter_for, #yaml_event_stream_for

Methods included from Process::Node

#affix

Instance Method Details

#parse_card_data_from(events, proto) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tamiyo/yaml/yaml_source.rb', line 28

def parse_card_data_from(events, proto)
  events.on_keys(
    'name' => ->{ proto.name = events.assume_scalar },
    'identity' => ->{
      events.each_within_sequence do
        proto.colors << events.assume_scalar
      end },
    'mana cost' => ->{
      events.on_types(
        scalar: ->{ proto.cost = events.value },
        seq: ->{
          events.each_within_sequence do
            proto.cost ||= []
            proto.cost << events.assume_scalar
          end }) },
    'type' => ->{ proto.type = events.assume_scalar },
    'text' => ->{ proto.text = events.assume_scalar },
    'pt' => ->{ proto.pt = events.assume_scalar },
    'loyalty' => ->{ proto.loyalty = events.assume_scalar },
    'row' => ->{ proto.table_row = events.assume_scalar.to_sym },
    'played tapped' => ->{
      events.assume_scalar
      proto.played_tapped = true },
    'picture url' => ->{ proto.picture_url = events.assume_scalar })
end

#pullObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tamiyo/yaml/yaml_source.rb', line 13

def pull
  using_object [] do |cards|
    with_the_cards_file do |events|
      events.each_within_sequence do
        proto = using_object(Prototype.new) do |proto|
          events.each_within_mapping do
            parse_card_data_from(events, proto)
          end
        end
        cards << proto.build_card
      end
    end
  end
end

#using_object(obj) {|obj| ... } ⇒ Object

Yields:

  • (obj)


63
64
65
66
# File 'lib/tamiyo/yaml/yaml_source.rb', line 63

def using_object(obj)
  yield obj
  obj
end

#with_the_cards_fileObject



54
55
56
57
58
59
60
61
# File 'lib/tamiyo/yaml/yaml_source.rb', line 54

def with_the_cards_file
  path = provide_path_for 'cards.yaml'
  if block_given?
    File.open(path, 'r') do |file|
      yield yaml_event_stream_for file
    end
  end
end