class Ari::Generators::ResourceGenerator

Public Class Methods

new(resource_name, specification, options = {}) click to toggle source
# File lib/ari/generators/resource_generator.rb, line 15
def initialize(resource_name, specification, options = {})
  @resource_name = resource_name.underscore
  @klass_name = @resource_name.classify
  @specification = specification
  @options = options
end

Public Instance Methods

apis() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 80
def apis
  @apis ||= @specification['apis'].map { |api| Api.new(api) }
end
destination_path(klass) click to toggle source
# File lib/ari/generators/resource_generator.rb, line 26
def destination_path(klass)
  File.join(__dir__, '..', klass)
end
generate() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 34
def generate
  generate_resource
  generate_models
end
generate_models() click to toggle source
# 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?() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 30
def generate_only_models?
  @options.fetch(:only_models, false)
end
generate_resource() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 88
def inherits_from
  generate_only_models? ? 'Model' : 'Resource'
end
klass_name() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 56
def klass_name
  @klass_name
end
models() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 84
def models
  @models ||= @specification['models'].map { |klass_name, options| Model.new(klass_name, self, options) }
end
resource_attributes() click to toggle source
# 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() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 60
def resource_klass_name
  options.fetch(:resource_klass_name, klass_name)
end
resource_name() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 68
def resource_name
  @resource_name.singularize
end
resource_plural_name() click to toggle source
# File lib/ari/generators/resource_generator.rb, line 72
def resource_plural_name
  @resource_name
end
template_path(klass) click to toggle source
# File lib/ari/generators/resource_generator.rb, line 22
def template_path(klass)
  File.join(__dir__, 'templates', "#{klass}.rb.erb")
end