Class: Ari::Generators::ResourceGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ari/generators/resource_generator.rb

Instance Method Summary collapse

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, options = {})
  @resource_name = resource_name.underscore
  @klass_name = @resource_name.classify
  @specification = specification
  @options = options
end

Instance Method Details

#apisObject



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

#generateObject



34
35
36
37
# File 'lib/ari/generators/resource_generator.rb', line 34

def generate
  generate_resource
  generate_models
end

#generate_modelsObject



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

Returns:

  • (Boolean)


30
31
32
# File 'lib/ari/generators/resource_generator.rb', line 30

def generate_only_models?
  @options.fetch(:only_models, false)
end

#generate_resourceObject



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_nameObject



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_fromObject



88
89
90
# File 'lib/ari/generators/resource_generator.rb', line 88

def inherits_from
  generate_only_models? ? 'Model' : 'Resource'
end

#klass_nameObject



56
57
58
# File 'lib/ari/generators/resource_generator.rb', line 56

def klass_name
  @klass_name
end

#modelsObject



84
85
86
# 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_attributesObject



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_nameObject



60
61
62
# File 'lib/ari/generators/resource_generator.rb', line 60

def resource_klass_name
  options.fetch(:resource_klass_name, klass_name)
end

#resource_nameObject



68
69
70
# File 'lib/ari/generators/resource_generator.rb', line 68

def resource_name
  @resource_name.singularize
end

#resource_plural_nameObject



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