Module: Rabl::Helpers

Included in:
Builder, Engine
Defined in:
lib/rabl/helpers.rb

Instance Method Summary collapse

Instance Method Details

#data_name(data) ⇒ Object

data_name(data) => “user” data_name(@user => :person) => :person data_name(@users) => :user data_name() => “user” data_name([]) => “array”



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rabl/helpers.rb', line 16

def data_name(data)
  return nil unless data # nil or false
  return data.values.first if data.is_a?(Hash) # @user => :user
  data = @_object.send(data) if data.is_a?(Symbol) && @_object # :address
  if data.respond_to?(:first) && data.first.respond_to?(:valid?)
    data_name(data.first).pluralize
  else # actual data object
    object_name = @_collection_name.to_s.singularize if @_collection_name
    object_name ||= data.class.respond_to?(:model_name) ? data.class.model_name.element : data.class.to_s.downcase
    object_name
  end
end

#data_object(data) ⇒ Object

data_object(data) => <AR Object> data_object(@user => :person) => @user data_object(:user => :person) => @_object.send(:user)



6
7
8
9
# File 'lib/rabl/helpers.rb', line 6

def data_object(data)
  data = (data.is_a?(Hash) && data.keys.one?) ? data.keys.first : data
  data.is_a?(Symbol) && @_object ? @_object.send(data) : data
end

#fetch_source(file, options = {}) ⇒ Object

Returns source for a given relative file fetch_source(“show”, :view_path => “…”) => “…contents…”



64
65
66
67
68
69
70
# File 'lib/rabl/helpers.rb', line 64

def fetch_source(file, options={})
  root_path = Rails.root if defined?(Rails)
  root_path = Padrino.root if defined?(Padrino)
  view_path = options[:view_path] || File.join(root_path, "app/views/")
  file_path = Dir[File.join(view_path, file + "*.rabl")].first
  File.read(file_path) if file_path
end

#is_record?(obj) ⇒ Boolean

Returns true if item is a ORM record; false otherwise is_record?(@user) => true is_record?([]) => false

Returns:

  • (Boolean)


58
59
60
# File 'lib/rabl/helpers.rb', line 58

def is_record?(obj)
  obj && data_object(obj).respond_to?(:valid?)
end

#object_to_hash(object, options = {}, &block) ⇒ Object

Returns a hash based representation of any data object given ejs template block object_to_hash(@user) { attribute :full_name } => { … } object_to_hash(@user, :source => “…”) { attribute :full_name } => { … }



39
40
41
42
43
# File 'lib/rabl/helpers.rb', line 39

def object_to_hash(object, options={}, &block)
  return object unless is_record?(object) || object.respond_to?(:each)
  engine_options = { :format => "hash", :root => (options[:root] || false) }
  Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
end

#partial(file, options = {}, &block) ⇒ Object

Renders a partial hash based on another rabl template partial(“users/show”, :object => @user)



31
32
33
34
# File 'lib/rabl/helpers.rb', line 31

def partial(file, options={}, &block)
  source = self.fetch_source(file, options)
  self.object_to_hash(options[:object], :source => source, &block)
end

#resolve_condition(options) ⇒ Object

resolve_condition(:if => true) => true resolve_condition(:if => lambda { |m| false }) => false resolve_condition(:unless => lambda { |m| true }) => true



48
49
50
51
52
53
# File 'lib/rabl/helpers.rb', line 48

def resolve_condition(options)
  return true if options[:if].nil? && options[:unless].nil?
  result = options[:if] == true || (options[:if].respond_to?(:call) && options[:if].call(@_object)) if options.has_key?(:if)
  result = options[:unless] == false || (options[:unless].respond_to?(:call) && !options[:unless].call(@_object)) if options.has_key?(:unless)
  result
end