Skip to content

Commit

Permalink
Getting close to guilder object being ready. Added ancillary classes …
Browse files Browse the repository at this point in the history
…that guiler uses.
  • Loading branch information
Jason Harrelson committed Feb 28, 2009
1 parent 5c9c17b commit 22a831d
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 8 deletions.
10 changes: 7 additions & 3 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ Manifest.txt
PostInstall.txt
README.rdoc
Rakefile
lib/guilded_base.rb
lib/guilded.rb
lib/guilded/base.rb
lib/guilded/guilder.rb
script/console
script/destroy
script/generate
test/test_guilded_base.rb
test/test_helper.rb
spec/guilded_spec.rb
spec/spec.opts
spec/spec_helper.rb
tasks/rspec.rake
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
require File.dirname(__FILE__) + '/lib/guilded_base'
require File.dirname(__FILE__) + '/lib/guilded'

# Generate all the Rake tasks
# Run 'rake -T' to see list of generated tasks (from gem root directory)
$hoe = Hoe.new('guilded-base', GuildedBase::VERSION) do |p|
p.developer('midas', 'jason@lookforwardenterprises.com')
$hoe = Hoe.new('guilded', Guilded::VERSION) do |p|
p.developer('midas', 'jaosn@lookforwardenterprisescom')
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
p.rubyforge_name = p.name # TODO this is default value
Expand Down
12 changes: 12 additions & 0 deletions lib/guilded.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'guilded/base'
require 'guilded/component_def'
require 'guilded/guilder'
require 'guilded/base/view_helpers'
require 'guilded/exceptions'

module Guilded
VERSION = '0.0.1'
end
17 changes: 17 additions & 0 deletions lib/guilded/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Guilded
module Base

# A place to define common stylesheets for all guilded components
COMMON_CSS = []

# A place to define common stylesheets for all guilded components
COMMON_JS = []

JQUERY_JS = 'jquery/jquery-1.2.6.min.js'
GUILDED_JS = 'guilded/guilded.min.js'

# The reset stylesheet guilded will use.
RESET_CSS = 'guilded/reset-min.css'

end
end
51 changes: 51 additions & 0 deletions lib/guilded/base/view_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Guilded
module Base
module ViewHelpers

# Generates the javascript includes for each Guilded element that is used. Also
# generates the initGuildedElements function and includes a call to each GUIlded
# elements Init method.
#
# Must be called once per rendered page. You can include it just before the closing body
# tag of your application layout. If no Guilded elements were called in the template, the
# call to g_apply_behavior will not output anything.
#
def g_apply_behavior( options={} )

@g_elements.each_value do |guilded_def|
#TODO get stylesheet (skin) stuff using rails caching
combine_css_sources( guilded_def.kind, guilded_def.options[:skin], guilded_def.styles ) unless guilded_def.exclude_css?

# Combine all JavaScript sources so that the caching option can be used on
# the javascript_incldue_tag helper.
combine_js_sources( guilded_def.kind, guilded_def.libs ) unless guilded_def.exclude_js?
end

# Add default styles
Guilded::Base::COMMON_CSS.each do |style|
@combined_css_srcs.push( style ) unless @combined_css_srcs.include?( style )
end

# Add default JavaScripts
Guilded::Base::COMMON_JS.each do |item|
@combined_js_srcs.push( item ) unless @combined_js_srcs.include?( item )
end

to_init << stylesheet_link_tag( @combined_css_srcs, :cache => "cache/#{generate_css_cache_name( @combined_js_srcs )}" )
to_init << javascript_include_tag( @combined_js_srcs, :cache => "cache/#{generate_js_cache_name( @combined_js_srcs )}" )

to_init << "<script type=\"text/javascript\">"
to_init << "var initGuildedElements = function(){"

@g_elements.each_value do |guilded_def|
unless guilded_def.exclude_js?
to_init << " g.#{guilded_def.kind.to_s.camelize( :lower )}Init( #{guilded_def.options.to_json} );"
end
end

to_init << "}; jQuery('document').ready( initGuildedElements ); </script>"
end

end
end
end
26 changes: 26 additions & 0 deletions lib/guilded/component_def.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Guilded

class ComponentDef

attr_reader :kind, :options, :libs, :styles
attr_accessor :additional_js

def initialize( kind, options={}, libs=[], styles=[], additional_js='' )
@kind = kind
@options = options
@libs = libs
@styles = styles
@additional_js = additional_js
end

def exclude_css?
options.include?( :exclude_css ) && ( options[:exclude_css] == 'true' || options[:exclude_css] == true )
end

def exclude_js?
options.include?( :exclude_js ) && ( options[:exclude_js] == 'true' || options[:exclude_js] == true )
end

end

end
8 changes: 8 additions & 0 deletions lib/guilded/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'guilded/exceptions/guilded_exception'
require 'guilded/exceptions/id_missing'
require 'guilded/exceptions/duplicate_element_id'

module Guilded
module Exceptions
end
end
12 changes: 12 additions & 0 deletions lib/guilded/exceptions/duplicate_element_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Guilded
module Exceptions

class DuplicateElementId < GuildedException
def initialize( id='' ) #:nodoc:
@msg = ":id #{id} for element is already in use on current page"
end
end

end
end

13 changes: 13 additions & 0 deletions lib/guilded/exceptions/guilded_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Guilded
module Exceptions

class GuildedException < RuntimeError

def initialize( msg="" ) #:nodoc:
@msg = msg
end

end

end
end
11 changes: 11 additions & 0 deletions lib/guilded/exceptions/id_missing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Guilded
module Exceptions

class IdMissing < GuildedException
def initialize #:nodoc:
@msg = ":id for element must be present in the options hash"
end
end

end
end
11 changes: 11 additions & 0 deletions lib/guilded/exceptions/id_missing_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Guilded
module Exceptions

class IdMissingException < GuildedException
def initialize #:nodoc:
@msg = ":id for element must be present in the options hash"
end
end

end
end
150 changes: 150 additions & 0 deletions lib/guilded/guilder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
module Guilded
class Guilder

# The folder name for guilded JavaScript files. Must include trailing '/'.
GUILDED_JS_FOLDER = "guilded/"
# The folder name for guilded css files. Must include trailing '/'.
GUILDED_CSS_FOLDER = "guilded/"
GUILDED_NS = "guilded."

attr_reader :g_elements, :combined_js_srcs, :combined_css_srcs

def initialize( options={} ) #:nodoc:
@g_elements = Hash.new #unless @g_elements
@combined_js_srcs = Array.new #if @combined_js_srcs.nil?
@combined_css_srcs = Array.new #if @combined_css_srcs.nil?

# Make sure that the css reset file is first so that other files can override the reset,
# unless the user specified no reset to be included.
@combined_css_srcs.insert( 0, Guilded::Base::RESET_CSS ) unless options[:reset] == false
@combined_js_srcs.insert( 0, Guilded::Base::GUILDED_JS ) unless @combined_js_srcs.include?( Guilded::Base::GUILDED_JS )
@combined_js_srcs.insert( 0, Guilded::Base::JQUERY_JS ) unless @combined_js_srcs.include?( Guilded::Base::JQUERY_JS )
#TODO find a way to get the resolve js libs to work on these two so we get the non-min version in development
end

# Adds an element with its options to the @g_elements hash to be used later.
#
def add_to_inits( element, options={}, libs=[], styles=[] )
raise Guilded::Exceptions::IdMissing unless options.has_key?( :id )
raise Guilded::Exceptions::DuplicateElementId( options[:id] ) if @g_elements && @g_elements.has_key?( options[:id] )
@g_elements = Hash.new unless @g_elements
@g_elements[ options[:id] ] = Guilded::ComponentDef.new( element, options, libs, styles )
end

# Generates the markup required to include all the assets necessary for the Guilded compoents in
# @g_elements collection.
#
def apply
to_init = ""

@g_elements.each_value do |guilded_def|
#TODO get stylesheet (skin) stuff using rails caching
combine_css_sources( guilded_def.kind, guilded_def.options[:skin], guilded_def.styles ) unless guilded_def.exclude_css?

# Combine all JavaScript sources so that the caching option can be used on
# the javascript_incldue_tag helper.
combine_js_sources( guilded_def.kind, guilded_def.libs ) unless guilded_def.exclude_js?
end

# Add default styles
Guilded::Base::COMMON_CSS.each do |style|
@combined_css_srcs.push( style ) unless @combined_css_srcs.include?( style )
end

# Add default JavaScripts
Guilded::Base::COMMON_JS.each do |script|
@combined_js_srcs.push( script ) unless @combined_js_srcs.include?( script )
end

to_init << stylesheet_link_tag( @combined_css_srcs, :cache => "cache/#{generate_css_cache_name( @combined_js_srcs )}" )
to_init << javascript_include_tag( @combined_js_srcs, :cache => "cache/#{generate_js_cache_name( @combined_js_srcs )}" )

to_init << "<script type=\"text/javascript\">"
to_init << "var initGuildedElements = function(){"

@g_elements.each_value do |guilded_def|
unless guilded_def.exclude_js?
to_init << " g.#{guilded_def.kind.to_s.camelize( :lower )}Init( #{guilded_def.options.to_json} );"
end
end

to_init << "}; jQuery('document').ready( initGuildedElements ); </script>"
end

protected

# Helper method that takes the libs and component specific js files and puts them
# into one array so that the javascript_include_tag can correctly cache them. Automatically
# ignores files that have already been inlcuded.
#
# *parameters*
# combined_src (required) An array of the combined sorces for the page being renderred.
# component (required) The name of a guilded component.
# libs An array of JavaScript libraries that this component depends on. More than likely
# a jQuery plugin, etc.
#
def combine_js_sources( component, libs=[] ) #:doc:
#combined_src = Array.new
resolve_js_libs( libs )

comp_src = add_guilded_js_path( component )
@combined_js_srcs.push( comp_src ) unless @combined_js_srcs.include?( comp_src )
end

# Helper method that adds the aditional JavaScript library icludes to the include set.
#
# If running development mode, it will try to remove any .pack, .min, or.compressed
# parts fo the name to try and get the debug version of the library. If it cannot
# find the debug version of the file, it will just remain what was initially provded.
#
def resolve_js_libs( libs )
if Rails.env.development?
# Try to use an unpacked or unminimized version
libs.each do |lib|
debug_lib = lib.gsub( /.pack/, '' ).gsub( /.min/, '' ).gsub( /.compressed/, '' )
path = "#{RAILS_ROOT}/public/javascripts/#{debug_lib}"
if File.exist?( path )
@combined_js_srcs.push( debug_lib ) unless @combined_js_srcs.include?( debug_lib )
else
@combined_js_srcs.push( lib ) unless @combined_js_srcs.include?( lib )
end
end
else
libs.each do |lib|
@combined_js_srcs.push( lib ) unless @combined_js_srcs.include?( lib )
end
end
end

# Helper method that takes an array of js sources and adds the correct guilded
# path to them. Returns an array with the new path resolved sources.
#
def map_guilded_js_paths( *sources )
sources.map { |source| add_guilded_js_path( source ) }
end

# Adds the guilded JS path to the
#
def add_guilded_js_path( source )
part = "#{GUILDED_JS_FOLDER}#{GUILDED_NS}#{source.to_s}"
ext = 'js'

if Rails.env.development?
"#{part}.#{ext}"
else

possibles = {
"#{RAILS_ROOT}/public/javascripts/#{part}.pack.#{ext}" => "#{part}.pack.#{ext}",
"#{RAILS_ROOT}/public/javascripts/#{part}.min.#{ext}" => "#{part}.min.#{ext}",
"#{RAILS_ROOT}/public/javascripts/#{part}.compressed#{ext}" => "#{part}.compressed#{ext}",
"#{RAILS_ROOT}/public/javascripts/#{part}.#{ext}" => "#{part}.#{ext}"
}

possibles.each do |full_path, part_path|
return part_path if File.exists?( full_path )
end
end
end

end
end
4 changes: 2 additions & 2 deletions script/console
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
libs = " -r irb/completion"
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
libs << " -r #{File.dirname(__FILE__) + '/../lib/guilded_base.rb'}"
puts "Loading guilded-base gem"
libs << " -r #{File.dirname(__FILE__) + '/../lib/guilded.rb'}"
puts "Loading guilded gem"
exec "#{irb} #{libs} --simple-prompt"
11 changes: 11 additions & 0 deletions spec/guilded_base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

# Time to add your specs!
# http://rspec.info/
describe "Place your specs here" do

it "find this spec in spec directory" do
violated "Be sure to write your specs"
end

end
11 changes: 11 additions & 0 deletions spec/guilded_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

# Time to add your specs!
# http://rspec.info/
describe "Place your specs here" do

it "find this spec in spec directory" do
violated "Be sure to write your specs"
end

end
1 change: 1 addition & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour
Loading

0 comments on commit 22a831d

Please sign in to comment.