Class: KiMiddleman::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ki_middleman/config.rb

Constant Summary collapse

FROM_BLOCK_KEY =
:outer_blocks

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Config

Returns a new instance of Config.



76
77
78
79
80
# File 'lib/ki_middleman/config.rb', line 76

def initialize(code)
  self.set_parsing_context
  @mapping = Map.new({})
  instance_eval code
end

Class Method Details

.from_file(path) ⇒ Object



9
10
11
12
# File 'lib/ki_middleman/config.rb', line 9

def self.from_file(path)
  code = File.read(path)
  parse(code)
end

.parse(code) ⇒ Object



14
15
16
# File 'lib/ki_middleman/config.rb', line 14

def self.parse(code)
  Config.new(code)
end

Instance Method Details

#convert_data_to_format(data, format, constants = '') ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ki_middleman/config.rb', line 35

def convert_data_to_format(data, format, constants='')
  self.detect_from_format(data)

  raise ConfigError, "No transform from '#{@from_format}' found." unless @mapping.has_key?(@from_format.to_sym)

  best_to_format = find_best_to_format(@from_format, format)

  if best_to_format != format
    $stderr.puts "WARNING: Using unambiguous match '#{best_to_format}' for '#{format}'"
  end

  @format_to_evaluate = best_to_format

  self.set_evaluation_context
  evaluation_contexts = @mapping[@from_format.to_sym][FROM_BLOCK_KEY]
  instance_eval constants
  converted_data = nil
  evaluation_contexts.each do |evaluation_context|
    converted_data = evaluation_context.call(Map.new(data))
    break if converted_data
  end
  converted_data['ki_type'] = best_to_format

  @evaluation = nil
  converted_data
end

#detect_from_format(data) ⇒ Object

Raises:



22
23
24
25
# File 'lib/ki_middleman/config.rb', line 22

def detect_from_format(data)
  @from_format = data['ki_type']
  raise ConfigError, "Cannot convert from unknown type. Include 'ki_type' in JSON" if @from_format.nil?
end

#find_best_to_format(from_format, format) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ki_middleman/config.rb', line 62

def find_best_to_format(from_format, format)
  keys = @mapping[from_format].keys.map(&:to_s)
  matching = keys.select { |key| key.include?(format.to_s) }

  case matching.length
  when 0
    raise "Cannot convert '#{from_format}' to '#{format}'. No matching transformations."
  when 1
    match = matching.first
  else
    raise "Cannot pick a transformation for '#{from_format}' to '#{format}'.\nPossible options are #{matching.map{|x| %Q{'#{x}'} }.join(', ')}."
  end
end

#from(from_format, &block) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/ki_middleman/config.rb', line 82

def from(from_format, &block)
  @current_from = from_format
  @mapping[@current_from] ||= {
  }
  @mapping[@current_from][FROM_BLOCK_KEY] ||= []
  @mapping[@current_from][FROM_BLOCK_KEY] << block
  yield
end

#set_evaluation_contextObject



31
32
33
# File 'lib/ki_middleman/config.rb', line 31

def set_evaluation_context
  @parsing = false
end

#set_parsing_contextObject



27
28
29
# File 'lib/ki_middleman/config.rb', line 27

def set_parsing_context
  @parsing = true
end

#textual_mappingObject



18
19
20
# File 'lib/ki_middleman/config.rb', line 18

def textual_mapping
  TransformationsRenderer.render(simple_mapping)
end

#to(to_format, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ki_middleman/config.rb', line 91

def to(to_format, &block)
  # If 'parsing' then add the block to our lookup table, otherwise
  # call the appropriate block (evaluation)
  if @parsing
    @mapping[@current_from][to_format] = block
  else
    #return @evaluation if @evaluation
    if @format_to_evaluate.to_s == to_format.to_s
      @evaluation = block.call
      @format_to_evaluate = nil
    end
    @evaluation
  end
end