Code Monkey home page Code Monkey logo

venduitz's Introduction

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

venduitz's People

Contributors

gabrielcorado avatar

Stargazers

Joakim Nylén avatar  avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.