Class: RailsApiDoc::Controller::Response::RablCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_api_doc/controller/response/rabl_compiler.rb

Overview

Class that will compile RABL source code into a hash representing data structure

Instance Method Summary collapse

Constructor Details

#initialize(file_path, view_path: 'app/views') ⇒ RablCompiler

Returns a new instance of RablCompiler.



51
52
53
54
55
56
57
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 51

def initialize(file_path, view_path: 'app/views')
  paths = Dir["#{view_path}/#{file_path}{.json,}.rabl"]
  file_path = paths.find { |path| File.exist?(path) }

  @source = _preserve_ivars File.read(file_path)
rescue
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *attrs) ⇒ Object

end



198
199
200
201
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 198

def method_missing(name, *attrs)
  return p("#{name} is not implemented in railsApiDoc") if name.in?(['merge', 'condtiion'])
  super
end

Instance Method Details

#_preserve_ivars(source) ⇒ Object

find all instance variables used and prepend them with ‘:’ to turn into symbols on compile. otherwise they turn to nil(random ivar is nil)



61
62
63
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 61

def _preserve_ivars(source)
  source.gsub /(@[^\s]*)/, ':\1'
end

#attribute(*args) ⇒ Object Also known as: attributes

Includes the attribute or method in the output Example:

attributes :id, :name
attribute :email => :super_secret

save attribute to nodes



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 92

def attribute(*args)
  if args.first.is_a?(Hash)
    args.first.each_pair do |key, name|
      @attributes.add(name: name, attr: key)
    end
  else
    options = args.extract_options!
    args.each do |name|
      key = options[:as] || name
      @attributes.add(name: name, attr: key)
    end
  end
end

#child(name_or_data, options = {}) ⇒ Object

Creates a child node to be included in the output. name_or data can be an object or collection or a method to call on the data. It accepts :root and :partial options. Notes that partial and blocks are not compatible Example:

child(:@posts, :root => :posts) { attribute :id }
child(:posts, :partial => 'posts/base')
child(:@posts => :cool_posts, :root => :posts) { attribute :id }


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 117

def child(name_or_data, options = {})
  data, name = extract_data_and_name(name_or_data)

  if options.empty? && name_or_data.is_a?(Hash) && new_options = name_or_data.to_a.second
    options = [new_options].to_h
  end

  name = options[:root] if options.key? :root

  if options.key?(:partial)
    attrs = RablCompiler.new(options[:partial]).compile_source
  elsif block_given?
    attrs = sub_compile(data) { yield }
  end

  @attributes.add(name: name, attr: data, nested: attrs.nodes)
end

#compile_sourceObject

Compile from source code and return the CompiledAttributes created.



66
67
68
69
70
71
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 66

def compile_source
  return unless @source
  @attributes = CompiledAttributes.new
  instance_eval(@source)
  @attributes
end

#extends(path) ⇒ Object

Extends an existing rabl template Example:

extends 'users/base'


180
181
182
183
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 180

def extends(path)
  extended = RablCompiler.new(path).compile_source
  @attributes.extends extended
end

#glue(data) ⇒ Object

Glues data from a child node to the output Example:

glue(:@user) { attribute :name }


140
141
142
143
144
145
146
147
148
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 140

def glue(data)
  return unless block_given?

  template = sub_compile(data) { yield }

  template.nodes.each_value do |value|
    @attributes.add name: value.name, attr: "#{data}.#{value.attr}", nested: value.nested
  end
end

#node(name = nil, _options = {}) ⇒ Object Also known as: code

Creates an arbitrary node in the json output. It accepts :if option to create conditionnal nodes. The current data will be passed to the block so it is advised to use it instead of ivars. Example:

node(:name) { |user| user.first_name + user.last_name }
node(:role, if: ->(u) { !u.admin? }) { |u| u.role }


158
159
160
161
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 158

def node(name = nil, _options = {})
  return unless block_given?
  @attributes.add name: name
end

#object(a) ⇒ Object Also known as: collection

Sets the object to be used as the data for the template Example:

object :@user
object :@user, :root => :author

dont care about object / collection options. need only attributes



80
81
82
# File 'lib/rails_api_doc/controller/response/rabl_compiler.rb', line 80

def object(a)
  @attributes.data = a
end