Skip to content

Commit

Permalink
Initial Commit - Extracted from Relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpace committed Sep 24, 2010
0 parents commit 4851246
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
relevant-widget-*.gem
12 changes: 12 additions & 0 deletions README.md
@@ -0,0 +1,12 @@
Relevant/Widget
===============

This library is the core of the widget system for Relevant. As a result, it will be a dependency of every widget.

Registering Widgets
-------------------

In order for Relevant to be aware of your widget you will need to register it. You can do this in the same file as your widget, or you can just call it somewhere else if you'd like it to be optional.

Relevant.register FooWidget

10 changes: 10 additions & 0 deletions Rakefile
@@ -0,0 +1,10 @@
require 'rake'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

task :install do
`gem build relevant-widget.gemspec`
`gem install relevant-widget-0.0.1.gem`
end
16 changes: 16 additions & 0 deletions lib/relevant/renderer.rb
@@ -0,0 +1,16 @@
module Relevant

class Renderer
def initialize(widget)
@widget = widget
end

def to_html
return unless @widget.class.template

template_handler = Tilt[@widget.class.template_format].new { @widget.class.template.strip }
template_handler.render(@widget)
end
end

end
50 changes: 50 additions & 0 deletions lib/relevant/widget.rb
@@ -0,0 +1,50 @@
require "tilt"
require 'relevant/renderer'

module Relevant

@widgets = []

def self.widgets
@widgets
end

def self.register(widget)
@widgets << widget
end

module Widget

def self.included(base)
base.extend ClassMethods

attr_accessor :options
end

module ClassMethods
def setup(options = {})
widget = new
widget.options = options

widget
end

def available_options(options = nil)
options.nil? ? @available_options : (@available_options = options)
end

def template_format(format = nil)
format.nil? ? @template_format : (@template_format = format)
end

def template(data = nil)
data.nil? ? @template : (@template = data)
end

def refresh_every(seconds = nil)
seconds.nil? ? @refresh_every : (@refresh_every = seconds)
end
end

end
end
20 changes: 20 additions & 0 deletions relevant-widget.gemspec
@@ -0,0 +1,20 @@
Gem::Specification.new do |s|
s.name = %q{relevant-widget}
s.version = "0.0.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["jdpace, rsanheim"]
s.date = %q{2010-09-24}
s.description = %q{Relevant Widget Core}
s.email = %q{jared@thinkrelevance.com}
s.files = ['Rakefile','README.md']
s.files += Dir['lib/**/*.rb']
s.homepage = %q{http://github.com/relevance/relevant-widget}
s.add_dependency "tilt", '1.1'
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Provides base functionalit for widgets. This is meant to be a dependency of all Relevant widgets.}
s.test_files = Dir['spec/**/*.rb']
end

30 changes: 30 additions & 0 deletions spec/relevant/twitter_spec.rb
@@ -0,0 +1,30 @@
require 'spec_helper'

describe Relevant::Twitter do
describe "to_html" do
it "does not blow up" do
lambda {
Relevant::Twitter.new.to_html
}.should_not raise_error
end

it "contains seach results" do
widget = Relevant::Twitter.new
results = [Hashie::Mash.new({:text => "I'm on the internet!", :from_user => "jdoe"})]
widget.expects(:results).returns(results)
widget.to_html.should include("I'm on the internet!")
end
end

describe '#limit' do
it 'is an available option' do
widget = Relevant::Twitter.new :limit => 3
widget.limit.should == 3
end

it 'defaults to 10' do
widget = Relevant::Twitter.new
widget.limit.should == 10
end
end
end
51 changes: 51 additions & 0 deletions spec/relevant/widget_spec.rb
@@ -0,0 +1,51 @@
require 'spec_helper'

describe Relevant::Widget do
class TestWidget
include Relevant::Widget

available_options :name => String

template_format :erb
template 'Hello <%= @options[:name] %>'
end

describe "options" do
it "tracks the types for form building" do
TestWidget.available_options[:name].should == String
end
end

describe 'template_format' do
it "reads the format if given no options" do
TestWidget.template_format.should == :erb
end

it "allows sets the format if you pass it" do
TestWidget.template_format :haml
TestWidget.template_format.should == :haml
end
end

describe 'template' do
it "reads the template if given no options" do
TestWidget.template.should == "Hello <%= @options[:name] %>"
end

it "allows sets the template if you pass it" do
TestWidget.template "Hello World"
TestWidget.template.should == "Hello World"
end
end

describe "to_html" do
it "renders the template" do
TestWidget.template "Hello <%= @options[:name] %>"
TestWidget.template_format :erb

widget = TestWidget.new(:name => 'Mr. Roboto')
widget.to_html.should == "Hello Mr. Roboto"
end
end

end
18 changes: 18 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,18 @@
require 'rspec'
require 'active_support'
require 'active_support/all'

$LOAD_PATH.unshift "../lib"
require "relevant/widget"
require "relevant/twitter"

RSpec.configure do |config|
config.color_enabled = true
config.mock_with :mocha

config.formatter = :progress
config.color_enabled = true
config.filter_run :focused => true
config.run_all_when_everything_filtered = true
config.alias_example_to :fit, :focused => true
end

0 comments on commit 4851246

Please sign in to comment.