Class: Ari::Generators::ResourceGenerator
- Inherits:
-
Object
- Object
- Ari::Generators::ResourceGenerator
- Defined in:
- lib/ari/generators/resource_generator.rb
Instance Method Summary collapse
- #apis ⇒ Object
- #destination_path(klass) ⇒ Object
- #generate ⇒ Object
- #generate_models ⇒ Object
- #generate_only_models? ⇒ Boolean
- #generate_resource ⇒ Object
- #id_attribute_name ⇒ Object
- #inherits_from ⇒ Object
-
#initialize(resource_name, specification, options = {}) ⇒ ResourceGenerator
constructor
A new instance of ResourceGenerator.
- #klass_name ⇒ Object
- #models ⇒ Object
- #resource_attributes ⇒ Object
- #resource_klass_name ⇒ Object
- #resource_name ⇒ Object
- #resource_plural_name ⇒ Object
- #template_path(klass) ⇒ Object
Constructor Details
#initialize(resource_name, specification, options = {}) ⇒ ResourceGenerator
Returns a new instance of ResourceGenerator.
15 16 17 18 19 20 |
# File 'lib/ari/generators/resource_generator.rb', line 15 def initialize(resource_name, specification, = {}) @resource_name = resource_name.underscore @klass_name = @resource_name.classify @specification = specification @options = end |
Instance Method Details
#apis ⇒ Object
80 81 82 |
# File 'lib/ari/generators/resource_generator.rb', line 80 def apis @apis ||= @specification['apis'].map { |api| Api.new(api) } end |
#destination_path(klass) ⇒ Object
26 27 28 |
# File 'lib/ari/generators/resource_generator.rb', line 26 def destination_path(klass) File.join(__dir__, '..', klass) end |
#generate ⇒ Object
34 35 36 37 |
# File 'lib/ari/generators/resource_generator.rb', line 34 def generate generate_resource generate_models end |
#generate_models ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/ari/generators/resource_generator.rb', line 46 def generate_models erb = ERB.new(IO.read(template_path('model')), nil, '-') models.each do |model| next if model.name == resource_name File.open(File.join(destination_path('models'), "#{model.name}.rb"), 'w') do |f| f.write erb.result(model.instance_eval { binding }) end end end |
#generate_only_models? ⇒ Boolean
30 31 32 |
# File 'lib/ari/generators/resource_generator.rb', line 30 def generate_only_models? @options.fetch(:only_models, false) end |
#generate_resource ⇒ Object
39 40 41 42 43 44 |
# File 'lib/ari/generators/resource_generator.rb', line 39 def generate_resource erb = ERB.new(IO.read(template_path('resource')), nil, '-') File.open(File.join(destination_path('resources'), "#{resource_name}.rb"), 'w') do |f| f.write erb.result(binding) end end |
#id_attribute_name ⇒ Object
76 77 78 |
# File 'lib/ari/generators/resource_generator.rb', line 76 def id_attribute_name @options.fetch(:id_attribute_name, "#{klass_name.camelcase(:lower)}Id") end |
#inherits_from ⇒ Object
88 89 90 |
# File 'lib/ari/generators/resource_generator.rb', line 88 def inherits_from generate_only_models? ? 'Model' : 'Resource' end |
#klass_name ⇒ Object
56 57 58 |
# File 'lib/ari/generators/resource_generator.rb', line 56 def klass_name @klass_name end |
#models ⇒ Object
84 85 86 |
# File 'lib/ari/generators/resource_generator.rb', line 84 def models @models ||= @specification['models'].map { |klass_name, | Model.new(klass_name, self, ) } end |
#resource_attributes ⇒ Object
64 65 66 |
# File 'lib/ari/generators/resource_generator.rb', line 64 def resource_attributes models.detect { |m| m.klass_name == klass_name }.properties rescue [] end |
#resource_klass_name ⇒ Object
60 61 62 |
# File 'lib/ari/generators/resource_generator.rb', line 60 def resource_klass_name .fetch(:resource_klass_name, klass_name) end |
#resource_name ⇒ Object
68 69 70 |
# File 'lib/ari/generators/resource_generator.rb', line 68 def resource_name @resource_name.singularize end |
#resource_plural_name ⇒ Object
72 73 74 |
# File 'lib/ari/generators/resource_generator.rb', line 72 def resource_plural_name @resource_name end |
#template_path(klass) ⇒ Object
22 23 24 |
# File 'lib/ari/generators/resource_generator.rb', line 22 def template_path(klass) File.join(__dir__, 'templates', "#{klass}.rb.erb") end |