moblues 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -5
  3. data/lib/moblues/cli.rb +3 -4
  4. data/lib/moblues/generator/base/model.rb +24 -0
  5. data/lib/moblues/generator/base/type.rb +63 -0
  6. data/lib/moblues/generator/objc/base.rb +67 -0
  7. data/lib/moblues/generator/objc/human.rb +27 -0
  8. data/lib/moblues/generator/{human_header.h.erb → objc/human_header.h.erb} +0 -0
  9. data/lib/moblues/generator/{human_implementation.m.erb → objc/human_implementation.m.erb} +0 -0
  10. data/lib/moblues/generator/objc/machine.rb +31 -0
  11. data/lib/moblues/generator/{machine_header.h.erb → objc/machine_header.h.erb} +2 -2
  12. data/lib/moblues/generator/{machine_implementation.m.erb → objc/machine_implementation.m.erb} +0 -0
  13. data/lib/moblues/generator/objc/type.rb +24 -0
  14. data/lib/moblues/generator/swift/base.rb +39 -0
  15. data/lib/moblues/generator/swift/human.rb +18 -0
  16. data/lib/moblues/generator/swift/human.swift.erb +4 -0
  17. data/lib/moblues/generator/swift/machine.rb +18 -0
  18. data/lib/moblues/generator/swift/machine.swift.erb +12 -0
  19. data/lib/moblues/generator.rb +9 -4
  20. data/lib/moblues/reader/type.rb +3 -37
  21. data/lib/moblues/version.rb +1 -1
  22. data/lib/moblues.rb +20 -4
  23. data/spec/features/moblues_spec.rb +35 -16
  24. data/spec/lib/moblues/cli_spec.rb +12 -2
  25. data/spec/lib/moblues/generator/base/model_spec.rb +27 -0
  26. data/spec/lib/moblues/generator/base/type_spec.rb +46 -0
  27. data/spec/lib/moblues/generator/objc/human_spec.rb +54 -0
  28. data/spec/lib/moblues/generator/{machine_spec.rb → objc/machine_spec.rb} +9 -8
  29. data/spec/lib/moblues/generator/objc/type_spec.rb +23 -0
  30. data/spec/lib/moblues/generator/swift/human_spec.rb +34 -0
  31. data/spec/lib/moblues/generator/swift/machine_spec.rb +57 -0
  32. data/spec/lib/moblues/reader/type_spec.rb +3 -54
  33. data/spec/moblues_spec.rb +46 -12
  34. data/spec/resources/expected/{Author.h → objc/Author.h} +0 -0
  35. data/spec/resources/expected/{Author.m → objc/Author.m} +0 -0
  36. data/spec/resources/expected/{_Author.h → objc/_Author.h} +0 -0
  37. data/spec/resources/expected/{_Author.m → objc/_Author.m} +0 -0
  38. data/spec/resources/expected/{_Book.h → objc/_Book.h} +0 -0
  39. data/spec/resources/expected/{_Book.m → objc/_Book.m} +0 -0
  40. data/spec/resources/expected/{_Person.h → objc/_Person.h} +0 -0
  41. data/spec/resources/expected/{_Person.m → objc/_Person.m} +0 -0
  42. data/spec/resources/expected/{_Team.h → objc/_Team.h} +0 -0
  43. data/spec/resources/expected/{_Team.m → objc/_Team.m} +0 -0
  44. data/spec/resources/expected/{human → objc/human}/Playable.h +0 -0
  45. data/spec/resources/expected/{human → objc/human}/Playable.m +0 -0
  46. data/spec/resources/expected/{human → objc/human}/Playlist.h +0 -0
  47. data/spec/resources/expected/{human → objc/human}/Playlist.m +0 -0
  48. data/spec/resources/expected/{human → objc/human}/Track.h +0 -0
  49. data/spec/resources/expected/{human → objc/human}/Track.m +0 -0
  50. data/spec/resources/expected/{human → objc/human}/User.h +0 -0
  51. data/spec/resources/expected/{human → objc/human}/User.m +0 -0
  52. data/spec/resources/expected/{machine → objc/machine}/_Playable.h +0 -0
  53. data/spec/resources/expected/{machine → objc/machine}/_Playable.m +0 -0
  54. data/spec/resources/expected/{machine → objc/machine}/_Playlist.h +0 -0
  55. data/spec/resources/expected/{machine → objc/machine}/_Playlist.m +0 -0
  56. data/spec/resources/expected/{machine → objc/machine}/_Track.h +0 -0
  57. data/spec/resources/expected/{machine → objc/machine}/_Track.m +0 -0
  58. data/spec/resources/expected/{machine → objc/machine}/_User.h +0 -0
  59. data/spec/resources/expected/{machine → objc/machine}/_User.m +0 -0
  60. data/spec/resources/expected/swift/Author.swift +4 -0
  61. data/spec/resources/expected/swift/_Author.swift +11 -0
  62. data/spec/resources/expected/swift/_Book.swift +8 -0
  63. data/spec/resources/expected/swift/human/Playable.swift +4 -0
  64. data/spec/resources/expected/swift/human/Playlist.swift +5 -0
  65. data/spec/resources/expected/swift/human/Track.swift +4 -0
  66. data/spec/resources/expected/swift/human/User.swift +4 -0
  67. data/spec/resources/expected/swift/machine/_Playable.swift +8 -0
  68. data/spec/resources/expected/swift/machine/_Playlist.swift +8 -0
  69. data/spec/resources/expected/swift/machine/_Track.swift +9 -0
  70. data/spec/resources/expected/swift/machine/_User.swift +10 -0
  71. data/spec/resources/factories/data_model/attribute_factory.rb +28 -0
  72. data/spec/resources/factories/data_model/relationship_factory.rb +15 -0
  73. data/spec/resources/fixtures.rb +10 -10
  74. data/spec/resources/tmp/{human → objc/human}/Playlist.h +0 -0
  75. data/spec/resources/tmp/{human → objc/human}/Playlist.m +0 -0
  76. data/spec/resources/tmp/swift/.gitkeep +0 -0
  77. data/spec/resources/tmp/swift/human/Playlist.swift +5 -0
  78. metadata +113 -76
  79. data/lib/moblues/generator/base.rb +0 -65
  80. data/lib/moblues/generator/human.rb +0 -25
  81. data/lib/moblues/generator/machine.rb +0 -36
  82. data/lib/moblues/generator/model.rb +0 -23
  83. data/spec/lib/moblues/generator/human_spec.rb +0 -74
  84. data/spec/lib/moblues/generator/model_spec.rb +0 -27
  85. data/spec/resources/tmp/_Team.h +0 -18
  86. data/spec/resources/tmp/_Team.m +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c36abc1af5154b1816b202fe56210ff27c3bab41
4
- data.tar.gz: fbb6b0c0cc21e59bc02ed710a4ba072e74fe7678
3
+ metadata.gz: 1f22698058e9f78222db4f63caa41e57b48356fc
4
+ data.tar.gz: 2e597a01de3f94ebac46c69c4453804f0b063ad5
5
5
  SHA512:
6
- metadata.gz: 8a853abe6bed750daa4bd87b4c1fd79b7b51b34dfda63390407325485d5b74abfd03d1ed5c6d566dab5fe41b439d30267bfd386174b1e7f59026de2ef904184a
7
- data.tar.gz: 738e69c5b6fc8216013ff86bcee9c7745597830a0ce9c4e8b5dbb14226c512a7904346e17c2623046fb1bbdbb04d4dd20734f80a15c057e23f271cfd50af7254
6
+ metadata.gz: 519ccda057113e8d835581c6d495698628b2c1f9d0f8f005017a2c969e0dbc1bfdf0f00dc4c79d8fd19d3a5fba66fd352c30384cdec7c31526bf57400a9423a7
7
+ data.tar.gz: 249f7ffa4ab130fdb8c8b6daf50b7868f9a229b98b1d8cb0045e54e9bd5cff8baeed4d8400f30d132895b769ad3a68f93db1dee4c552340c32916449d867ea24
data/README.md CHANGED
@@ -24,14 +24,16 @@ Or install it yourself as:
24
24
 
25
25
  ```
26
26
  Usage:
27
- moblues generate --human=HUMAN --machine=MACHINE --model=MODEL
27
+ moblues generate --human=HUMAN --machine=MACHINE --model=MODEL
28
28
 
29
29
  Options:
30
- --model=MODEL # CoreData model path
31
- --human=HUMAN # Path where the human files will be stored
32
- --machine=MACHINE # Path where the machine files will be stored
30
+ --model=MODEL # Core Data model path
31
+ --human=HUMAN # Path where the human files will be stored
32
+ --machine=MACHINE # Path where the machine files will be stored
33
+ [--lang=LANG] # The language used to generate the files (objc or swift)
34
+ # Default: objc
33
35
 
34
- generate the machine and human files
36
+ generate the machine and human files for entities contained in a Core Data model
35
37
  ```
36
38
 
37
39
  ## Contributing
data/lib/moblues/cli.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  require 'thor'
2
- require 'moblues/reader/model'
3
- require 'moblues/generator/model'
4
2
 
5
3
  module Moblues
6
4
  class CLI < Thor
7
- desc 'generate', 'generate the machine and human files'
5
+ desc 'generate', 'generate the machine and human files for entities contained in a Core Data model'
8
6
  option :model, :required => true, :desc => 'Core Data model path'
9
7
  option :human, :required => true, :desc => 'Path where the human files will be stored'
10
8
  option :machine, :required => true, :desc => 'Path where the machine files will be stored'
9
+ option :lang, :default => 'objc', :desc => 'The language used to generate the files (objc or swift)'
11
10
  def generate
12
- Moblues.generate(options[:model], options[:human], options[:machine])
11
+ Moblues.generate(options[:model], options[:human], options[:machine], options[:lang].to_sym)
13
12
  end
14
13
  end
15
14
  end
@@ -0,0 +1,24 @@
1
+ module Moblues
2
+ module Generator
3
+ module Base
4
+ class Model
5
+ def initialize(params)
6
+ @human = params.fetch(:human)
7
+ @machine = params.fetch(:machine)
8
+ @human_dir = params.fetch(:human_dir)
9
+ @machine_dir = params.fetch(:machine_dir)
10
+ end
11
+
12
+ def generate(entities)
13
+ entities.each do |entity|
14
+ human.generate(human_dir, entity)
15
+ machine.generate(machine_dir, entity)
16
+ end
17
+ end
18
+
19
+ private
20
+ attr_reader :human, :machine, :human_dir, :machine_dir
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ module Moblues
2
+ module Generator
3
+ module Base
4
+ class Type
5
+ SWIFT = {
6
+ attribute_types:
7
+ {
8
+ string: 'String',
9
+ number: 'NSNumber',
10
+ decimal: 'NSDecimalNumber',
11
+ date: 'NSDate',
12
+ data: 'NSData',
13
+ id: 'AnyObject'
14
+ },
15
+ relationship_types:
16
+ {
17
+ to_many: 'NSSet',
18
+ to_many_ordered: 'NSOrderedSet',
19
+ suffix: ''
20
+ }
21
+ }
22
+
23
+ OBJC = {
24
+ attribute_types:
25
+ {
26
+ string: 'NSString *',
27
+ number: 'NSNumber *',
28
+ decimal: 'NSDecimalNumber *',
29
+ date: 'NSDate *',
30
+ data: 'NSData *',
31
+ id: 'id '
32
+ },
33
+ relationship_types:
34
+ {
35
+ to_many: 'NSSet *',
36
+ to_many_ordered: 'NSOrderedSet *',
37
+ suffix: ' *'
38
+ }
39
+ }
40
+
41
+ def initialize(params = SWIFT)
42
+ @attribute_types = params.fetch(:attribute_types)
43
+ @relationship_types = params.fetch(:relationship_types)
44
+ end
45
+
46
+ def attribute_type(attribute)
47
+ attribute_types.fetch(attribute.type)
48
+ end
49
+
50
+ def relationship_type(relationship)
51
+ if relationship.to_many
52
+ relationship.ordered ? relationship_types[:to_many_ordered] : relationship_types[:to_many]
53
+ else
54
+ relationship.destination_entity + relationship_types[:suffix]
55
+ end
56
+ end
57
+
58
+ protected
59
+ attr_reader :attribute_types, :relationship_types
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,67 @@
1
+ require 'erb'
2
+ require 'moblues/generator/objc/type'
3
+
4
+ module Moblues
5
+ module Generator
6
+ module Objc
7
+ class Base
8
+ def initialize
9
+ @type_generator = Type.new
10
+ end
11
+
12
+ protected
13
+ attr_reader :type_generator
14
+
15
+ def write_header(output_dir, entity)
16
+ write_file(header_file(output_dir, entity), header(entity))
17
+ end
18
+
19
+ def write_implementation(output_dir, entity)
20
+ write_file(implementation_file(output_dir, entity), implementation(entity))
21
+ end
22
+
23
+ def write_file(file, text)
24
+ File.open(file, 'w+') do |f|
25
+ f.write(text)
26
+ end
27
+ end
28
+
29
+ def header_file(output_dir, entity)
30
+ File.join(output_dir, format_name(entity, 'h'))
31
+ end
32
+
33
+ def implementation_file(output_dir, entity)
34
+ File.join(output_dir, format_name(entity, 'm'))
35
+ end
36
+
37
+ def format_name(entity, extension)
38
+ raise NotImplemented
39
+ end
40
+
41
+ def header(entity)
42
+ render(header_template, entity)
43
+ end
44
+
45
+ def implementation(entity)
46
+ render(implementation_template, entity)
47
+ end
48
+
49
+ def render(template, entity)
50
+ ERB.new(template, 0, '-').result(entity.send(:binding))
51
+ end
52
+
53
+ def header_template
54
+ raise NotImplemented
55
+ end
56
+
57
+ def implementation_template
58
+ raise NotImplemented
59
+ end
60
+
61
+ def file_template(name)
62
+ File.read(File.expand_path("../#{name}.erb", __FILE__))
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,27 @@
1
+ require 'moblues/generator/objc/base'
2
+
3
+ module Moblues
4
+ module Generator
5
+ module Objc
6
+ class Human < Base
7
+ def generate(output_dir, entity)
8
+ write_header(output_dir, entity) unless File.exists?(header_file(output_dir, entity))
9
+ write_implementation(output_dir, entity) unless File.exists?(implementation_file(output_dir, entity))
10
+ end
11
+
12
+ private
13
+ def format_name(entity, extension)
14
+ "#{entity.name}.#{extension}"
15
+ end
16
+
17
+ def header_template
18
+ file_template('human_header.h')
19
+ end
20
+
21
+ def implementation_template
22
+ file_template('human_implementation.m')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ require 'moblues/generator/objc/base'
2
+
3
+ module Moblues
4
+ module Generator
5
+ module Objc
6
+ class Machine < Base
7
+ def generate(output_dir, entity)
8
+ write_header(output_dir, entity)
9
+ write_implementation(output_dir, entity)
10
+ end
11
+
12
+ private
13
+ def format_name(entity, extension)
14
+ "_#{entity.name}.#{extension}"
15
+ end
16
+
17
+ def capitalized_name(relationship)
18
+ relationship.name.sub(/^./) { |first_character| first_character.upcase }
19
+ end
20
+
21
+ def header_template
22
+ file_template('machine_header.h')
23
+ end
24
+
25
+ def implementation_template
26
+ file_template('machine_implementation.m')
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -11,11 +11,11 @@
11
11
 
12
12
  @interface _<%= entity.name %> : <%= entity.parent_entity %>
13
13
  <% for attribute in entity.attributes -%>
14
- @property (<%= type_reader.property_attributes(attribute.type).join(', ') %>) <%= type_reader.property_type(attribute.type)%><%= attribute.name %>;
14
+ @property (<%= type_generator.property_attributes(attribute).join(', ') %>) <%= type_generator.attribute_type(attribute)%><%= attribute.name %>;
15
15
  <% end -%>
16
16
 
17
17
  <% for relationship in entity.relationships -%>
18
- @property (nonatomic, strong) <%= type_reader.relationship_type(relationship) %><%= relationship.name %>;
18
+ @property (nonatomic, strong) <%= type_generator.relationship_type(relationship) %><%= relationship.name %>;
19
19
  <% end -%>
20
20
  @end
21
21
  <% if !entity.relationships.select { |e| e.to_many }.empty? %>
@@ -0,0 +1,24 @@
1
+ require 'moblues/generator/base/type'
2
+
3
+ module Moblues
4
+ module Generator
5
+ module Objc
6
+ class Type < Base::Type
7
+ def initialize
8
+ super(OBJC)
9
+ end
10
+
11
+ def property_attributes(attribute)
12
+ case attribute.type
13
+ when :string
14
+ %w{nonatomic copy}
15
+ when :number, :decimal, :date, :data, :id
16
+ %w{nonatomic strong}
17
+ else
18
+ raise ArgumentError.new("unknown type #{type}")
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ require 'erb'
2
+ require 'moblues/generator/base/type'
3
+
4
+ module Moblues
5
+ module Generator
6
+ module Swift
7
+ class Base
8
+ def initialize
9
+ @type = Generator::Base::Type.new
10
+ end
11
+
12
+ protected
13
+ attr_reader :type
14
+
15
+ def write_swift(output_dir, entity, file_type)
16
+ write_file(file_path(output_dir, entity), render(entity, file_type))
17
+ end
18
+
19
+ def write_file(file, text)
20
+ File.open(file, 'w+') do |f|
21
+ f.write(text)
22
+ end
23
+ end
24
+
25
+ def file_path(output_dir, entity)
26
+ raise NotImplemented
27
+ end
28
+
29
+ def render(entity, file_type)
30
+ ERB.new(template(file_type), 0, '-').result(entity.send(:binding))
31
+ end
32
+
33
+ def template(file_type)
34
+ File.read(File.expand_path("../#{file_type}.swift.erb", __FILE__))
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ require 'moblues/generator/swift/base'
2
+
3
+ module Moblues
4
+ module Generator
5
+ module Swift
6
+ class Human < Base
7
+ def generate(output_dir, entity)
8
+ write_swift(output_dir, entity, :human) unless File.exists?(file_path(output_dir, entity))
9
+ end
10
+
11
+ private
12
+ def file_path(output_dir, entity)
13
+ File.join(output_dir, "#{entity.name}.swift")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ import Foundation
2
+
3
+ class <%= entity.name %>: _<%= entity.name %> {
4
+ }
@@ -0,0 +1,18 @@
1
+ require 'moblues/generator/swift/base'
2
+
3
+ module Moblues
4
+ module Generator
5
+ module Swift
6
+ class Machine < Base
7
+ def generate(output_dir, entity)
8
+ write_swift(output_dir, entity, :machine)
9
+ end
10
+
11
+ private
12
+ def file_path(output_dir, entity)
13
+ File.join(output_dir, "_#{entity.name}.swift")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ import Foundation
2
+ import CoreData
3
+
4
+ class _<%= entity.name %>: <%= entity.parent_entity %> {
5
+ <% for attribute in entity.attributes -%>
6
+ @NSManaged var <%= attribute.name %>: <%= type.attribute_type(attribute) %>
7
+ <% end -%>
8
+
9
+ <% for relationship in entity.relationships -%>
10
+ @NSManaged var <%= relationship.name %>: <%= type.relationship_type(relationship) %>
11
+ <% end -%>
12
+ }
@@ -1,4 +1,9 @@
1
- require 'moblues/generator/base'
2
- require 'moblues/generator/model'
3
- require 'moblues/generator/machine'
4
- require 'moblues/generator/human'
1
+ require 'moblues/generator/base/model'
2
+ require 'moblues/generator/base/type'
3
+ require 'moblues/generator/objc/base'
4
+ require 'moblues/generator/objc/human'
5
+ require 'moblues/generator/objc/machine'
6
+ require 'moblues/generator/objc/type'
7
+ require 'moblues/generator/swift/base'
8
+ require 'moblues/generator/swift/human'
9
+ require 'moblues/generator/swift/machine'
@@ -5,8 +5,10 @@ module Moblues
5
5
  case type_str
6
6
  when 'String'
7
7
  :string
8
- when 'Integer 16', 'Integer 32', 'Integer 64', 'Boolean'
8
+ when 'Integer 16', 'Integer 32', 'Integer 64', 'Boolean', 'Float', 'Double'
9
9
  :number
10
+ when 'Decimal'
11
+ :decimal
10
12
  when 'Date'
11
13
  :date
12
14
  when 'Binary'
@@ -17,42 +19,6 @@ module Moblues
17
19
  raise ArgumentError.new("unknown type #{type_str}")
18
20
  end
19
21
  end
20
-
21
- def property_type(type)
22
- case type
23
- when :string
24
- 'NSString *'
25
- when :number
26
- 'NSNumber *'
27
- when :date
28
- 'NSDate *'
29
- when :data
30
- 'NSData *'
31
- when :id
32
- 'id '
33
- else
34
- raise ArgumentError.new("unknown type #{type}")
35
- end
36
- end
37
-
38
- def property_attributes(type)
39
- case type
40
- when :string
41
- %w{nonatomic copy}
42
- when :number, :date, :data, :id
43
- %w{nonatomic strong}
44
- else
45
- raise ArgumentError.new("unknown type #{type}")
46
- end
47
- end
48
-
49
- def relationship_type(relationship)
50
- if relationship.to_many
51
- relationship.ordered ? 'NSOrderedSet *' : 'NSSet *'
52
- else
53
- relationship.destination_entity + ' *'
54
- end
55
- end
56
22
  end
57
23
  end
58
24
  end
@@ -1,3 +1,3 @@
1
1
  module Moblues
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/moblues.rb CHANGED
@@ -6,11 +6,11 @@ require 'moblues/generator'
6
6
  module Moblues
7
7
  extend self
8
8
 
9
- def generate(model, human_dir, machine_dir)
9
+ def generate(model, human_dir, machine_dir, lang = :objc)
10
10
  mkdir([human_dir, machine_dir])
11
- reader = Moblues::Reader::Model.new
12
- generator = Moblues::Generator::Model.new(human_dir: human_dir, machine_dir: machine_dir)
13
- entities = reader.model(model)
11
+ reader = Moblues::Reader::Model.new
12
+ generator = generator(human_dir, machine_dir, lang)
13
+ entities = reader.model(model)
14
14
  generator.generate(entities)
15
15
  end
16
16
 
@@ -20,4 +20,20 @@ module Moblues
20
20
  Dir.mkdir(dir) unless Dir.exists?(dir)
21
21
  end
22
22
  end
23
+
24
+ def generator(human_dir, machine_dir, lang)
25
+ base_params = { human_dir: human_dir,
26
+ machine_dir: machine_dir }
27
+ case lang
28
+ when :swift
29
+ additional_params = { human: Moblues::Generator::Swift::Human.new,
30
+ machine: Moblues::Generator::Swift::Machine.new }
31
+ when :objc
32
+ additional_params = { human: Moblues::Generator::Objc::Human.new,
33
+ machine: Moblues::Generator::Objc::Machine.new }
34
+ else
35
+ raise ArgumentError.new("Language #{lang} is not supported")
36
+ end
37
+ Moblues::Generator::Base::Model.new(base_params.merge(additional_params))
38
+ end
23
39
  end
@@ -7,30 +7,49 @@ describe 'moblues' do
7
7
 
8
8
  describe 'generate' do
9
9
  let(:command) { 'generate' }
10
- let(:options) { %w{--model=spec/resources/Model.xcdatamodeld --human=spec/resources/tmp/human --machine=spec/resources/tmp/machine} }
11
10
 
12
- after do
13
- Fixtures.delete_tmp_files(machine_files + human_files.select { |f| !f.include?('Playlist') } )
11
+ let(:entities) { %w{User Playable Track Playlist} }
12
+
13
+ shared_examples_for 'generate' do |lang|
14
+ it 'generates human and machine files' do
15
+ perform
16
+
17
+ (machine_files + human_files).each do |file|
18
+ expect(Fixtures.read_file(Fixtures.generated_dir(lang), file)).to eq(Fixtures.read_file(Fixtures.expected_dir(lang), file))
19
+ end
20
+ end
14
21
  end
15
22
 
16
- it 'generates human and machine files' do
17
- perform
23
+ context 'swift' do
24
+ let(:options) { %w{--model=spec/resources/Model.xcdatamodeld --human=spec/resources/tmp/swift/human --machine=spec/resources/tmp/swift/machine --lang=swift} }
25
+
26
+ let(:machine_files) { entities.map{|f| "machine/_#{f}.swift" } }
27
+ let(:human_files) { entities.map{|f| "human/#{f}.swift" } }
18
28
 
19
- (machine_files + human_files).each do |file|
20
- expect(Fixtures.read_file(Fixtures.generated_dir, file)).to eq(Fixtures.read_file(Fixtures.expected_dir, file))
29
+ after do
30
+ Fixtures.delete_tmp_files(machine_files + human_files.select { |f| !f.include?('Playlist') }, :swift)
21
31
  end
32
+
33
+ it_behaves_like 'generate', :swift
22
34
  end
23
- end
24
35
 
25
- def machine_files
26
- entities.map{|f| "machine/_#{f}" }.map{ |f| %W(#{f}.h #{f}.m) }.flatten
27
- end
36
+ context 'objc' do
37
+ let(:options) { %w{--model=spec/resources/Model.xcdatamodeld --human=spec/resources/tmp/objc/human --machine=spec/resources/tmp/objc/machine} }
28
38
 
29
- def human_files
30
- entities.map{|f| "human/#{f}" }.map{ |f| %W(#{f}.h #{f}.m) }.flatten
31
- end
39
+ let(:machine_files) { entities.map{|f| "machine/_#{f}" }.map{ |f| %W(#{f}.h #{f}.m) }.flatten }
40
+ let(:human_files) { entities.map{|f| "human/#{f}" }.map{ |f| %W(#{f}.h #{f}.m) }.flatten }
41
+
42
+ after do
43
+ Fixtures.delete_tmp_files(machine_files + human_files.select { |f| !f.include?('Playlist') }, :objc)
44
+ end
32
45
 
33
- def entities
34
- %w{User Playable Track Playlist}
46
+ it 'generates human and machine files' do
47
+ perform
48
+
49
+ (machine_files + human_files).each do |file|
50
+ expect(Fixtures.read_file(Fixtures.generated_dir(:objc), file)).to eq(Fixtures.read_file(Fixtures.expected_dir(:objc), file))
51
+ end
52
+ end
53
+ end
35
54
  end
36
55
  end
@@ -28,11 +28,21 @@ describe Moblues::CLI do
28
28
  context 'when options given' do
29
29
  let(:options) { %w{--model=model/path --human=human/path --machine=machine/path} }
30
30
 
31
- it 'created the directories reads the model and generates the files' do
32
- expect(Moblues).to receive(:generate).with('model/path', 'human/path', 'machine/path').once
31
+ it 'uses Moblues and defaults to objc' do
32
+ expect(Moblues).to receive(:generate).with('model/path', 'human/path', 'machine/path', :objc).once
33
33
 
34
34
  perform
35
35
  end
36
+
37
+ context 'when using lang option' do
38
+ let(:options) { %w{--model=model/path --human=human/path --machine=machine/path --lang=swift} }
39
+
40
+ it 'uses Moblues' do
41
+ expect(Moblues).to receive(:generate).with('model/path', 'human/path', 'machine/path', :swift).once
42
+
43
+ perform
44
+ end
45
+ end
36
46
  end
37
47
  end
38
48
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Moblues::Generator::Base::Model do
4
+ describe '#generate' do
5
+ subject { described_class.new(human: human, machine: machine, human_dir: human_dir, machine_dir: machine_dir) }
6
+
7
+ let(:entities) { [build(:entity)] }
8
+ let(:entity) { entities.first }
9
+ let(:human_dir) { 'human/dir'}
10
+ let(:machine_dir) { 'machine/dir'}
11
+
12
+ let(:human) { double(Moblues::Generator::Objc::Human) }
13
+ let(:machine) { double(Moblues::Generator::Objc::Machine) }
14
+
15
+ before do
16
+ allow(Moblues::Generator::Objc::Human).to receive(:new) { human }
17
+ allow(Moblues::Generator::Objc::Machine).to receive(:new) { machine }
18
+ end
19
+
20
+ it 'generates the human and machine files' do
21
+ expect(human).to receive(:generate).with(human_dir, entity).once
22
+ expect(machine).to receive(:generate).with(machine_dir, entity).once
23
+
24
+ subject.generate(entities)
25
+ end
26
+ end
27
+ end