Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aizatto committed Jun 5, 2011
0 parents commit af62ef7
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README
@@ -0,0 +1,60 @@
XRB was inspired by XHP ( http://github.com/facebook/xhp/ ).
XRB is a Rails Engine to be used.

Having used XHP intensively, I saw the benefits of XML literals as first class
elements in a programming language. The biggest benefit was that you could
easily build large libraries of components to reuse on many sites.

As I was unable to figure out how to add XML literals into the Ruby parser.
I thought I would start with a more Ruby approach.

Installation
============

Edit Your Application's Gemfile
-------------------------------

Add to your Gemfile

gem 'xrb', :require => 'xrb/engine'

In your ApplicationHelper add

require UiHelper

Usage
=====

Inside your template files you can now use XRB.

<%= ui :image, :block do %>
<% ui :link => user_path(user) %>
<%= image_tag(user.photo.url(:thumbnail), :title => user %>
<% end %>
<% ui :group do %>
<% ui :link => user_path(user) %>
<%= user %>
<% end %>
<% ui :group do %>
<%= user.description %>
<% end %>
<% end %>
<% end %>


Defining your own XRB Element
-----------------------------

Say you want to define an element `user`.
Inside a helper file we need to add a function:

def ui_user(xrb)
user = xrb.attributes.delete(:user)

xrb.content = ui_output do
content_tag :div, user, xrb.attributes
end
end

Change the code inside `ui_output` to let design how your component will look
like.
80 changes: 80 additions & 0 deletions app/helpers/ui_helper.rb
@@ -0,0 +1,80 @@
module UiHelper

def ui(*args, &block)
component = []
if args.length == 1 && args.first.is_a?(Hash) && args.first.length == 1
component << args.first.keys.first
else
while args.first.is_a? Symbol
component << args.shift
end
end
component = "ui_#{component.join '_'}"
xrb = ui_capture(*args, &block)
@components << xrb if @components

self.send(component, xrb)
end

def ui_image_block(xrb)
image = xrb.components.shift
group = xrb.components.shift

title = group.components.shift
contents = group.components.shift

ui_output do <<-HTML
<div class="image-block">
<div class="image">#{image}</div>
<div class="content">
<div class="title">#{title}</div>
#{contents}
</div>
</div>
<div class="clear"></div>
HTML
end
end

def ui_link(xrb)
xrb.attributes[:href] = xrb.attributes[:link] if xrb.attributes[:link]

xrb.content = ui_output do
content_tag(:a, xrb.inner_content, xrb.attributes)
end
end

def ui_group(xrb)
xrb.content = ui_output do
xrb.inner_content
end
end

# helper
def ui_output
value = yield

value.html_safe
end

def ui_capture(*args, &block)
xrb = ::XRB::Element.new
xrb.attributes = args.extract_options!

if block
@components, old_components = [], @components
xrb.inner_content = capture(&block)
xrb.components = @components
@components = old_components
else
xrb.inner_content = args.first || ''
end

if xrb.inner_content.is_a?(String) && ! xrb.inner_content.html_safe?
xrb.inner_content = xrb.inner_content.html_safe
end

xrb
end

end
9 changes: 9 additions & 0 deletions app/models/xrb/element.rb
@@ -0,0 +1,9 @@
module XRB
class Element
attr_accessor :content, :inner_content, :attributes, :components

def to_s
content
end
end
end
4 changes: 4 additions & 0 deletions lib/xrb/engine.rb
@@ -0,0 +1,4 @@
module XRB
class Engine < Rails::Engine
end
end
1 change: 1 addition & 0 deletions rails/init.rb
@@ -0,0 +1 @@
require 'xrb/engine'
12 changes: 12 additions & 0 deletions xrb.gemspec
@@ -0,0 +1,12 @@
Gem::Specification.new do |s|
s.name = %q{xrb}
s.version = "0.1"
s.authors = ["Aizat Faiz"]
s.email = %q{aizat.faiz@gmail.com}
s.date = Time.now.utc.strftime("%Y-%m-%d")
s.files = `git ls-files`.split("\n")
s.homepage = %q{http://github.com/aizatto/xrb}
s.rdoc_options = ["--charset=UTF-8"]
s.rubygems_version = %q{1.7.2}
s.summary = %q{XRB summary}
end

0 comments on commit af62ef7

Please sign in to comment.