Class: Guilded::Guilder

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/guilded/guilder.rb

Overview

Guilder is the worker for the entire Guilded framework. It collects all of the necessary components for a page through its add() method. When the g_apply_behavior() method is called at the end of a page, the Guilder writes HTML to include all of the necessary asset files (caching them in production). It also writes a JavaScript initialization function and fires the initialization function on page load and a before and after initialization callback allowing for custom initializtion to occur.

This initialization function calls the initialization function for each Guilded component that was added to the current page. For example, if a Guilded component named ‘g_load_alerter’ was added to a page, the Guilder would include this line in the initialization function it writes: g.initLoadAlerter( /* passing options hash here */ ); The g before the function is a JavaScript namespace that Guilded automatically creates to facilitate avoiding name collisions with other JavaScript libraries and code.

Th options hash that is passed to the init functions for each Guilded component is simply the options hash from the component’s view helper. The Guilder calls .to_json() on the options hash. Thus, if there are pairs in the options hash that need not go to the JavaScript init method they should be removed within the view helper.

Constant Summary collapse

GUILDED_NS =
"guilded."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGuilder

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/guilded/guilder.rb', line 30

def initialize #:nodoc:
  if defined?( GUILDED_CONFIG )
    @config = GUILDED_CONFIG
  else
    raise Guilded::Exceptions::MissingConfiguration
  end
  configure_guilded
  @initialized_at = Time.now
  @g_elements = {}
  @g_data_elements = {}
  @combined_js_srcs = []
  @combined_css_srcs = []
  @css_temp_hold = { :pre => [], :post => [], :components => [], :reset => [] }
  @valid_css_positions = @css_temp_hold.keys
  # 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.
  init_sources
end

Instance Attribute Details

#initialized_atObject (readonly)

Returns the value of attribute initialized_at.



28
29
30
# File 'lib/guilded/guilder.rb', line 28

def initialized_at
  @initialized_at
end

#jquery_jsObject (readonly)

Returns the value of attribute jquery_js.



28
29
30
# File 'lib/guilded/guilder.rb', line 28

def jquery_js
  @jquery_js
end

#mootools_jsObject (readonly)

Returns the value of attribute mootools_js.



28
29
30
# File 'lib/guilded/guilder.rb', line 28

def mootools_js
  @mootools_js
end

Instance Method Details

#add(element, options = {}, libs = [], styles = []) ⇒ Object

Adds an element with its options to the @g_elements hash to be used later.



51
52
53
54
55
56
# File 'lib/guilded/guilder.rb', line 51

def add( element, options={}, libs=[], styles=[] )
  raise Guilded::Exceptions::IdMissing.new unless options.has_key?( :id )
  raise Guilded::Exceptions::DuplicateElementId.new( options[:id] ) if @g_elements.has_key?( options[:id] )
  @need_mootools = true if options[:mootools]
  @g_elements[ options[:id].to_sym ] = Guilded::ComponentDef.new( element, options, libs, styles )
end

#add_css_source(src, position = :post) ⇒ Object



76
77
78
# File 'lib/guilded/guilder.rb', line 76

def add_css_source( src, position=:post )
  @css_temp_hold[position.to_sym] << src unless @css_temp_hold[position.to_sym].include?( src )
end

#add_data(name, data) ⇒ Object

Adds a data structure to be passed to the Guilded JavaScript environment for use on the client side. The data is passed using the ruby to_json method on the data structure provided.

Parameters

  • name - The desired name of the variable on the client side.

  • data - The data to pass to the Guilded JavaScript environment.



65
66
67
# File 'lib/guilded/guilder.rb', line 65

def add_data( name, data )
  @g_data_elements.merge!( name.to_sym => data )
end

#add_js_sources(*sources) ⇒ Object

Adds JavaScript sources to the libs collection by resolving them to the normal or min version based on the current running environment, development, production, etc.



72
73
74
# File 'lib/guilded/guilder.rb', line 72

def add_js_sources( *sources )
  resolve_js_libs( *sources )
end

#applyObject

Generates the markup required to include all the assets necessary for the Guilded compoents in



148
149
150
151
152
153
154
155
# File 'lib/guilded/guilder.rb', line 148

def apply #:nodoc:
  to_init = ""
  generate_asset_lists #unless @assets_combined
  @combined_css_srcs.each { |css| to_init << "<link href=\"/stylesheets/#{css}\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />" }
  @combined_js_srcs.each { |js| to_init << "<script type=\"text/javascript\" src=\"/javascripts/#{js}\"></script>" }
  to_init << generate_javascript_init
  reset!
end

#combined_css_srcsObject

The collection of CSS assets for the current Guilded component set.



116
117
118
119
120
121
122
# File 'lib/guilded/guilder.rb', line 116

def combined_css_srcs
  @css_temp_hold[:reset].each { |src| @combined_css_srcs << src }
  @css_temp_hold[:pre].each { |src| @combined_css_srcs << src }
  @css_temp_hold[:components].each { |src| @combined_css_srcs << src }
  @css_temp_hold[:post].each { |src| @combined_css_srcs << src }
  @combined_css_srcs
end

#combined_js_srcsObject

The collection of JavaScript assets for the current Guilded component set.



110
111
112
# File 'lib/guilded/guilder.rb', line 110

def combined_js_srcs
  @combined_js_srcs
end

#component_countObject

The number of Guilded components to be renderred.



86
87
88
# File 'lib/guilded/guilder.rb', line 86

def component_count
  count
end

#countObject

:nodoc:



80
81
82
# File 'lib/guilded/guilder.rb', line 80

def count #:nodoc:
  @g_elements.size
end

#css_cache_nameObject

Generates a name to use when caching the current set of Guilded component CSS assets. Sorts and concatenates the name of each JavaScript asset in @combined_js_srcs. Then hashes this string to generate a reproducible, unique and shorter string.



184
185
186
187
# File 'lib/guilded/guilder.rb', line 184

def css_cache_name
  name = generate_css_cache_name( @combined_css_srcs )
  name
end

#generate_asset_listsObject

Combines all JavaScript and CSS files into lists to include based on what Guilded components are on the current page.



206
207
208
209
210
211
212
213
214
215
# File 'lib/guilded/guilder.rb', line 206

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

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

#generate_css_cache_name(sources) ⇒ Object

:nodoc:



196
197
198
199
200
201
# File 'lib/guilded/guilder.rb', line 196

def generate_css_cache_name( sources ) #:nodoc:
  sorted_srcs = sources.sort
  stripped_srcs = sorted_srcs.map { |src| src.gsub( /.css/, '' ).gsub( /\//, '_') }
  joined = stripped_srcs.join( "+" )
  "#{Digest::MD5.hexdigest( joined )}"
end

#generate_javascript_initObject

Writes an initialization method that calls each Guilded components initialization method. This method will exceute on document load finish.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/guilded/guilder.rb', line 160

def generate_javascript_init #:nodoc:
  code = "<script type=\"text/javascript\">"
  code << "var initGuildedElements = function(){"
  @g_data_elements.each do |name, data|
    code << "g.#{name} = #{data.to_json};" 
  end
  @g_elements.each_value do |guilded_def| 
    code << "g.#{guilded_def.kind.to_s.camelize( :lower )}Init(#{guilded_def.options.to_json});" unless guilded_def.exclude_js?
  end
  code << "jQuery('body').trigger('guildedInitialized');};jQuery('document').ready(initGuildedElements);</script>"
end

#generate_js_cache_name(sources) ⇒ Object

:nodoc:



189
190
191
192
193
194
# File 'lib/guilded/guilder.rb', line 189

def generate_js_cache_name( sources ) #:nodoc: 
  sorted_srcs = sources.sort
  stripped_srcs = sorted_srcs.map { |src| src.gsub( /.js/, '' ).gsub( /\//, '_') }
  joined = stripped_srcs.join( "+" )
  "#{Digest::MD5.hexdigest( joined )}"
end

#include_component?(type) ⇒ Boolean

Returns true if the component type is included, otherwise false.

Returns:

  • (Boolean)


104
105
106
# File 'lib/guilded/guilder.rb', line 104

def include_component?( type )
  @g_elements.has_key?( type.to_sym )
end

#inject_css(*sources) ⇒ Object



137
138
139
# File 'lib/guilded/guilder.rb', line 137

def inject_css( *sources )
  @combined_css_srcs.insert( @default_css_count, *sources )
end

#inject_js(*sources) ⇒ Object



141
142
143
# File 'lib/guilded/guilder.rb', line 141

def inject_js( *sources )
  @combined_js_srcs.insert( @default_js_count, *sources )
end

#js_cache_nameObject

Generates a name to use when caching the current set of Guilded component JavaScript assets. Sorts and concatenates the name of each JavaScript asset in @combined_js_srcs. Then hashes this string to generate a reproducible, unique and shorter string.



176
177
178
# File 'lib/guilded/guilder.rb', line 176

def js_cache_name
  generate_js_cache_name( @combined_js_srcs )
end

#reset!Object

Clears out all but the reset CSS and the base JavaScripts



126
127
128
129
130
131
132
133
134
135
# File 'lib/guilded/guilder.rb', line 126

def reset!
  @combined_css_srcs.clear
  @combined_js_srcs.clear
  @g_elements.clear
  @css_temp_hold = { :pre => [], :post => [], :components => [], :reset => [] }
  @valid_css_positions = @css_temp_hold.keys
  init_sources
  @default_css_count = @combined_css_srcs.size
  @default_js_count = @combined_js_srcs.size
end

#script_countObject

The current number of JavaScript assets necessary for the Guilded component set.



98
99
100
# File 'lib/guilded/guilder.rb', line 98

def script_count
  @combined_js_srcs.size
end

#style_countObject

The current number of CSS assets necessary for the Guilded component set.



92
93
94
# File 'lib/guilded/guilder.rb', line 92

def style_count
  @combined_css_srcs.size
end