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 s JavaScript initialization function and fires the initialization function on page load.

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:



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

def initialize #:nodoc:
  if defined?( GUILDED_CONFIG )
    configure_guilded
  else
    raise Guilded::Exceptions::MissingConfiguration
  end
  @initialized_at = Time.now
  @g_elements = Hash.new
  @g_data_elements = Hash.new
  @combined_js_srcs = Array.new
  @combined_css_srcs = Array.new
  @assets_combined = false
  # 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.



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

def initialized_at
  @initialized_at
end

#jquery_jsObject (readonly)

Returns the value of attribute jquery_js.



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

def jquery_js
  @jquery_js
end

#mootools_jsObject (readonly)

Returns the value of attribute mootools_js.



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

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.



48
49
50
51
52
53
# File 'lib/guilded/guilder.rb', line 48

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_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.



62
63
64
# File 'lib/guilded/guilder.rb', line 62

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.



69
70
71
# File 'lib/guilded/guilder.rb', line 69

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



138
139
140
141
142
143
144
145
# File 'lib/guilded/guilder.rb', line 138

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.



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

def combined_css_srcs
  #generate_asset_lists unless @assets_combined
  @combined_css_srcs
end

#combined_js_srcsObject

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



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

def combined_js_srcs
  #generate_asset_lists unless @assets_combined
  @combined_js_srcs
end

#component_countObject

The number of Guilded components to be renderred.



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

def component_count
  count
end

#countObject

:nodoc:



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

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.



174
175
176
# File 'lib/guilded/guilder.rb', line 174

def css_cache_name
  generate_css_cache_name( @combined_css_srcs )
end

#generate_css_cache_name(sources) ⇒ Object

:nodoc:



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

def generate_css_cache_name( sources ) #:nodoc:
  generate_asset_lists unless @assets_combined
  #return "#{controller.class.to_s.underscore}_#{controller.action_name}" if development?
  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.



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/guilded/guilder.rb', line 150

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:



178
179
180
181
182
183
184
185
# File 'lib/guilded/guilder.rb', line 178

def generate_js_cache_name( sources ) #:nodoc: 
  generate_asset_lists unless @assets_combined
  #return"#{controller.class.to_s.underscore}_#{controller.action_name}" if development?
  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)


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

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

#inject_css(*sources) ⇒ Object



127
128
129
# File 'lib/guilded/guilder.rb', line 127

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

#inject_js(*sources) ⇒ Object



131
132
133
# File 'lib/guilded/guilder.rb', line 131

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.



166
167
168
# File 'lib/guilded/guilder.rb', line 166

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



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

def reset!
  @combined_css_srcs.clear
  @combined_js_srcs.clear
  @g_elements.clear
  @assets_combined = false
  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.



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

def script_count
  @combined_js_srcs.size
end

#style_countObject

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



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

def style_count
  @combined_css_srcs.size
end