[![Gem Version](https://badge.fury.io/rb/wrap_it.png)](http://badge.fury.io/rb/wrap_it) [![Code Climate](https://codeclimate.com/github/cybernetlab/wrap_it.png)](https://codeclimate.com/github/cybernetlab/wrap_it) [![Build Status](https://travis-ci.org/cybernetlab/wrap_it.png?branch=master)](https://travis-ci.org/cybernetlab/wrap_it) [![Coverage Status](https://coveralls.io/repos/cybernetlab/wrap_it/badge.png?branch=master)](https://coveralls.io/r/cybernetlab/wrap_it?branch=master) # WrapIt This library provides set of classes and modules with simple DSL for quick and easy creating html helpers with your own DSL. It's usefull for implementing CSS frameworks, or making your own. > Required ruby version is 2.0.0 > **Warning** A lot of code refactored. API changed. Review you code if you using previous versions of library. For example, your designer makes perfect button style for some site. This element will appears in many places of site in some variations. The button have `danger`, `success` and `default` look, and can have `active` state. Button can have some icon. So, you make some CSS styles, and now you should place HTML markup of this element in many places of site. With `wrap_it` library you can do it with following code: ```ruby module Helpers; end WrapIt.register_module Helpers module Helpers class PerfectButton < WrapIt::Container include TextContainer html_class 'button' enum :look, %i(default success danger), html_class_prefix: 'button-' switch :active, html_class: 'button-active' child :icon, tag: 'img', class: 'button-icon' end register :p_button, 'PerfectButton' end ``` Now, include this helper into you template engine. For Rails: ```ruby class MyController < ApplicationController helper Helpers ... end ``` And you can use it in you ERB: ```html <%= p_button %>button 1<% end %> <%= p_button 'button 2', :active, :success %> <%= p_button active: true, look: :success, body: 'button 3' %> <%= p_button :danger do |b| %> <%= b.icon src: '/path/to/icon.png' %> button 4 <% end %> ``` This will produce following code: ```html
``` Note, that lines 2 and 3 produces same html markup. # Status This is a first release version - `1.0.0`. # Installation Library have a gem. So, just install it: ```sh gem install wrap_it ``` or include in your `Gemfile` ```ruby gem 'wrap_it' ``` and run ```sh bundle install ``` # Configuration Library have no specific configuration. # Usage Now, package is well documented, so make sure to inspect [Reference documentation](http://rubydoc.info/github/cybernetlab/wrap_it/frames) > This library actively used in [BootstrapIt](https://github.com/cybernetlab/bootstrap_it) package, so explore this project, especially it's [lib/bootstrap_it/view_helpers](https://github.com/cybernetlab/bootstrap_it/tree/master/lib/bootstrap_it/view_helpers) folder for usage examples. All helpers classes derived from `WrapIt::Base` class, that provides allmost all functionality. For helpers, thats includes other helpers, use `WrapIt::Container` class. Simple example explained above. More complex usage is to provide some logic to initalization, capturing and rendering process. To do this, use `after` or `before` `initialize`, `capture` and `reder` callbacks respectively. Usually `after` callbacks used. `initialize` callbacks runs around arguments and optioins parsing, `capture` callbacks runs around capturing element sections and `render` callbacks runs around wrapping content into element tag. Also, please inspect arguments [module documentation](http://rubydoc.info/github/cybernetlab/wrap_it/WrapIt/Arguments) for details about creation arguments and options. Inside callbacks some usefull instance variables available. `tag` contains tag name for element. `html_attr` contains HTML attributes hash. `html_data` contains HTML data hash. `html_class` contains array of HTML classes and provides array-like acces to its. See [class documentation](http://rubydoc.info/github/cybernetlab/wrap_it/WrapIt/HTMLClass) for details. Inside `capture` callback you deals with sections. This mechanism explained in [module documentation](http://rubydoc.info/github/cybernetlab/wrap_it/WrapIt/Sections). `template` contains rendering template. Use this variable carefully, so if you call `template.link_to` or something else Rails-related, your library will not be portable to other frameworks. So, if you use this gem in user-end application, or Rails-only library, you are free to use all of `template` methods. *Examples* Prevent user from changing element tag: ```ruby class Helper < WrapIt::Base after_initialize { self.tag = 'table' } end ``` Including some simple HTML into content ```ruby class IconHelper < WrapIt::Base option :icon attr_accessor :icon after_capture do unless @icon.nil? self[:content] = html_safe("") end end ``` ## WrapIt #### WrapIt.register_module(*args) Registers helpers module and defines `register` and `unregister` class methods in this module for registering helper methods. You can specify module to register in first argument. If ommited, anonymous module will be created and returned from method. Use `prefix` option to add specified prefix to all methods in helper module. Typical usage of library and this method is: Define empty module and register it with `register_method`: ```ruby module YourPerfectLib module PerfectHelpers; end WrapIt.register_module PerfectHelpers, prefix: 'perfect_' # You can register all your helper methods right here, but in complex # projects recommended to keep calls to register inside file where # helper class defined. # # PerfectHelpers.register :button, 'YourPerfectLib::PerfectHelpers::Button' end ``` Describe your classes and register helper methods for it: ```ruby module YourPerfectLib module PerfectHelpers class Button < WrapIt::Base include WrapIt::TextContainer html_class 'button' ... end end register :button, 'YourPerfectLib::PerfectHelpers::Button' end ``` Include it in your template (example for Rails): ```ruby class MyController < ApplicationController helper Helpers ... end ``` And now use it in templates: ```html <%= perfect_button 'button text' %> ``` will produce: ```html