ki_middleman 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ki_middleman CHANGED
@@ -8,7 +8,7 @@ module KiMiddleman
8
8
  include Methadone::Main
9
9
  include Methadone::CLILogging
10
10
 
11
- def init
11
+ def self.init
12
12
  config_dir = Kiseru::ConfigDir.new
13
13
  assets_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'assets'))
14
14
  assets_file = File.join(assets_dir, 'default_transforms.rb')
@@ -23,15 +23,24 @@ module KiMiddleman
23
23
  end
24
24
 
25
25
  main do |arg|
26
+ help_now! "argument '#{arg}' not supported" if arg
27
+
26
28
  if options[:init]
27
29
  init
30
+ elsif options[:list]
31
+ config = KiMiddleman::Config.from_file(Kiseru::ConfigDir.new.file('transforms.rb'))
32
+ $stdout.puts(config.textual_mapping)
28
33
  elsif options[:to]
29
34
  begin
30
35
  pretty = $stdout.tty?
31
36
 
32
37
  output_type = options[:to]
33
38
  config = KiMiddleman::Config.from_file(Kiseru::ConfigDir.new.file('transforms.rb'))
34
- input = MultiJson.load($stdin.read)
39
+ if options[:file]
40
+ input = MultiJson.load(File.read(options[:file]))
41
+ else
42
+ input = MultiJson.load($stdin.read)
43
+ end
35
44
  output = config.convert_data_to_format(input, output_type)
36
45
  $stdout.write MultiJson.dump(output, :pretty => pretty)
37
46
  rescue MultiJson::DecodeError => e
@@ -49,6 +58,8 @@ module KiMiddleman
49
58
 
50
59
  on("--to FORMAT", "The output JSON format")
51
60
  on("--init", "Set up the default config files")
61
+ on("--file FILE", "Read JSON from file")
62
+ on("--list", "List available transformations")
52
63
  go!
53
64
 
54
65
  end
data/ki_middleman.gemspec CHANGED
@@ -15,12 +15,12 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = KiMiddleman::VERSION
17
17
 
18
- gem.add_dependency 'multi_json'
19
- gem.add_dependency('methadone')
20
- gem.add_dependency('map')
18
+ gem.add_dependency('multi_json', '~> 1.3.6')
19
+ gem.add_dependency('methadone', '~> 1.2.1')
20
+ gem.add_dependency('map', '~> 4.6.1')
21
21
 
22
- gem.add_development_dependency('debugger')
23
- gem.add_development_dependency('rake')
24
- gem.add_development_dependency('simplecov')
25
- gem.add_development_dependency('rspec')
22
+ gem.add_development_dependency('debugger', '~> 1.2.0')
23
+ gem.add_development_dependency('rake', '~> 0.9.2')
24
+ gem.add_development_dependency('rspec', '~> 2.11.0')
25
+ gem.add_development_dependency('simplecov', '~> 0.6.4')
26
26
  end
@@ -9,6 +9,24 @@ module KiMiddleman
9
9
  parse(code)
10
10
  end
11
11
 
12
+ def textual_mapping
13
+ output = ""
14
+ @mapping.each do |key, sub_hash|
15
+ subkeys = sub_hash.keys.reject { |x| x.to_s=="outer_block"}
16
+ subkeys.each_with_index do |sub_key, i|
17
+ case i
18
+ when 0
19
+ output << "#{key} +-> #{sub_key}\n"
20
+ when subkeys.length - 1
21
+ output << "#{' '*key.length} \\-> #{sub_key}\n"
22
+ else
23
+ output << "#{' '*key.length} |-> #{sub_key}\n"
24
+ end
25
+ end
26
+ end
27
+ output
28
+ end
29
+
12
30
  def self.parse(code)
13
31
  Config.new(code)
14
32
  end
@@ -28,19 +46,42 @@ module KiMiddleman
28
46
 
29
47
  def convert_data_to_format(data, format)
30
48
  self.detect_from_format(data)
31
- @format_to_evaluate = format
32
- self.set_evaluation_context
49
+
33
50
  raise ConfigError, "No transform from '#{@from_format}' found." unless @mapping.has_key?(@from_format.to_sym)
51
+
52
+ best_to_format = find_best_to_format(@from_format, format)
53
+
54
+ if !@mapping[@current_from].has_key?(best_to_format)
55
+ raise ConfigError, "No transform from '#{@current_from}' to '#{format}' found."
56
+ end
57
+
58
+ if best_to_format != format
59
+ $stderr.puts "WARNING: Using unambiguous match '#{best_to_format}' for '#{format}'"
60
+ end
61
+
62
+ @format_to_evaluate = best_to_format
63
+ self.set_evaluation_context
34
64
  evaluation_context = @mapping[@from_format.to_sym][:outer_block]
35
65
  converted_data = evaluation_context.call Map.new(data)
36
- converted_data['ki_type'] = format
66
+ converted_data['ki_type'] = best_to_format
37
67
 
38
68
  converted_data
39
69
  end
40
70
 
71
+ def find_best_to_format(from_format, format)
72
+ keys = @mapping[from_format].keys.map(&:to_s)
73
+ matching = keys.select { |key| key.include?(format.to_s) }
74
+
75
+ if matching.length == 1
76
+ match = matching.first
77
+ else
78
+ format
79
+ end
80
+ end
81
+
41
82
  def initialize(code)
42
83
  self.set_parsing_context
43
- @mapping = {}
84
+ @mapping = Map.new({})
44
85
  instance_eval code
45
86
  end
46
87
 
@@ -1,3 +1,3 @@
1
1
  module KiMiddleman
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ki_middleman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,85 +10,85 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-15 00:00:00.000000000Z
13
+ date: 2012-08-16 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
17
- requirement: &70130545466640 !ruby/object:Gem::Requirement
17
+ requirement: &70146996153140 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
- - - ! '>='
20
+ - - ~>
21
21
  - !ruby/object:Gem::Version
22
- version: '0'
22
+ version: 1.3.6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70130545466640
25
+ version_requirements: *70146996153140
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: methadone
28
- requirement: &70130545466220 !ruby/object:Gem::Requirement
28
+ requirement: &70146996152640 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
- - - ! '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 1.2.1
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70130545466220
36
+ version_requirements: *70146996152640
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: map
39
- requirement: &70130545465800 !ruby/object:Gem::Requirement
39
+ requirement: &70146996152180 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ! '>='
42
+ - - ~>
43
43
  - !ruby/object:Gem::Version
44
- version: '0'
44
+ version: 4.6.1
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70130545465800
47
+ version_requirements: *70146996152180
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: debugger
50
- requirement: &70130545465380 !ruby/object:Gem::Requirement
50
+ requirement: &70146996151720 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
- - - ! '>='
53
+ - - ~>
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: 1.2.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70130545465380
58
+ version_requirements: *70146996151720
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rake
61
- requirement: &70130545464960 !ruby/object:Gem::Requirement
61
+ requirement: &70146996151260 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
- - - ! '>='
64
+ - - ~>
65
65
  - !ruby/object:Gem::Version
66
- version: '0'
66
+ version: 0.9.2
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70130545464960
69
+ version_requirements: *70146996151260
70
70
  - !ruby/object:Gem::Dependency
71
- name: simplecov
72
- requirement: &70130545464540 !ruby/object:Gem::Requirement
71
+ name: rspec
72
+ requirement: &70146996150800 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
- - - ! '>='
75
+ - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 2.11.0
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *70130545464540
80
+ version_requirements: *70146996150800
81
81
  - !ruby/object:Gem::Dependency
82
- name: rspec
83
- requirement: &70130545464120 !ruby/object:Gem::Requirement
82
+ name: simplecov
83
+ requirement: &70146996150340 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
- - - ! '>='
86
+ - - ~>
87
87
  - !ruby/object:Gem::Version
88
- version: '0'
88
+ version: 0.6.4
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *70130545464120
91
+ version_requirements: *70146996150340
92
92
  description: Transforms JSON into other JSON
93
93
  email:
94
94
  - ben@freeagent.com