Module: Rabl::Helpers
Instance Method Summary collapse
-
#data_name(data) ⇒ Object
data_name(data) => “user” data_name(@user => :person) => :person data_name(@users) => :user data_name() => “user” data_name([]) => “array”.
-
#data_object(data) ⇒ Object
data_object(data) => <AR Object> data_object(@user => :person) => @user data_object(:user => :person) => @_object.send(:user).
-
#fetch_source(file, options = {}) ⇒ Object
Returns source for a given relative file fetch_source(“show”, :view_path => “…”) => “…contents…”.
-
#is_record?(obj) ⇒ Boolean
Returns true if item is a ORM record; false otherwise is_record?(@user) => true is_record?([]) => false.
-
#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 } => { … }.
-
#partial(file, options = {}, &block) ⇒ Object
Renders a partial hash based on another rabl template partial(“users/show”, :object => @user).
-
#resolve_condition(options) ⇒ Object
resolve_condition(:if => true) => true resolve_condition(:if => lambda { |m| false }) => false resolve_condition(:unless => lambda { |m| true }) => true.
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, ={}) root_path = Rails.root if defined?(Rails) root_path = Padrino.root if defined?(Padrino) view_path = [: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
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, ={}, &block) return object unless is_record?(object) || object.respond_to?(:each) = { :format => "hash", :root => ([:root] || false) } Rabl::Engine.new([:source], ).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, ={}, &block) source = self.fetch_source(file, ) self.object_to_hash([: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() return true if [:if].nil? && [:unless].nil? result = [:if] == true || ([:if].respond_to?(:call) && [:if].call(@_object)) if .has_key?(:if) result = [:unless] == false || ([:unless].respond_to?(:call) && ![:unless].call(@_object)) if .has_key?(:unless) result end |