Project

venduitz

0.0
No commit activity in last 3 years
No release in over 3 years
This rubygem does not have a description or summary.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.0

Runtime

>= 0
 Project Readme

Venduitz Build Status Gem Version

A simple JSON-only(until now) template engine, with focus on performance for better Restful-APIs.

How to use it

Install

gem install venduitz
# OR...
gem 'venduitz'

Define your views

require 'venduitz'

class MetaView < Venduitz::View
  # Define the MEta view properties
  prop :current, -> (search) { search.current_page }
  prop :total, -> (search) { search.total_count }
  prop :total_pages
  prop :per_page
end

# Image view
class ImageView < Venduitz::View
  # Define the view properties
  prop :url
  prop :width
  prop :height
  prop :alt
end

# Product view
class ProductView < Venduitz::View
  # Define the view properties
  prop :name
  prop :sku
  prop :price

  # Also some collections
  collection :images, ImageView, value: -> (product) { product.variants.images }

  # You could also pass a property as a proc
  # In this case I will use the ImageView again
  prop :cover, -> (product) { ImageView.generate(product.cover) }
end

# Its also possible to exclude collection/prop values when generating it
# in this way you could generate a Ember-like result
class SearchView < Venduitz::View
  # Define the view properties
  prop :meta, -> (search) { MetaView.generate(search) }

  collection :products, ProductView, value: -> (search) { search.products }, exclude: [:images]
  collection :images, ImageView, value: -> (search) { search.products.images }
end

# Get your Object ready
search = Product.search 'Hello'

# Transform it to JSON!
res = SearchView.to_json(search)

# OR... If you don't want to show the images for this search
# just exclude them in the generation method
res = SearchView.to_json(search, [:images])

# Return to your framwork
[200, {'Content-Type' => 'application/json'}, res]

Using Cache

To use cache you have to define a Driver for the Venduitz:

# Defining the CacheDriver
Venduitz::Cache.driver = CacheDriver

# This cache driver must define some methods (specified on the Vendtuiz::Cache module)

# As common, generate your view but with some differences
# `true` in this case will enable the cache
res = SearchView.to_json(search, [:images], true)

# To use a 'partial views' the method #generate also supports cache
class CachedView < ProductView
  prop :cover, -> (product) { ImageView.generate(product.cover, [], true) }
end

# To pass the expiration time to your driver just use a fourth argument
# to define the driver call options
res = SearchView.to_json(search, [:images], true, { expire_in: 10.minutes })

Development

  • Building the docker container: docker build -t venduitz .
  • Running the tests:
    • With volume: docker run --rm -it -v (PWD):/venduitz venduitz bundle exec rspec
    • Without volume: docker run --rm -it venduitz bundle exec rspec