Skip to content

Commit

Permalink
use class_attribute instead of redefining read_inheritable_attribute …
Browse files Browse the repository at this point in the history
…and write_inheritable_attribute
  • Loading branch information
Diego Rodriguez committed Apr 10, 2012
1 parent ed28cea commit 465b729
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 97 deletions.
13 changes: 5 additions & 8 deletions lib/netzke/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module Actions

included do
alias_method_chain :js_config, :actions

# Returns registered actions
class_attribute :registered_actions
self.registered_actions = []
end

module ClassMethods
Expand All @@ -61,14 +65,7 @@ def action(name, config = {}, &block)

# Register an action
def register_action(name)
current_actions = read_inheritable_attribute(:actions) || []
current_actions << name
write_inheritable_attribute(:actions, current_actions.uniq)
end

# Returns registered actions
def registered_actions
read_inheritable_attribute(:actions) || []
self.registered_actions |= [name]
end

end
Expand Down
17 changes: 6 additions & 11 deletions lib/netzke/composition.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# require 'active_support/core_ext/class/inheritable_attributes'

module Netzke
# This module takes care of components composition.
#
Expand All @@ -25,7 +23,11 @@ module Composition

included do

# Loads a component on browser's request. Every Nettzke component gets this endpoint.
# Returns registered components
class_attribute :registered_components
self.registered_components = []

# Loads a component on browser's request. Every Netzke component gets this endpoint.
# <tt>params</tt> should contain:
# * <tt>:cache</tt> - an array of component classes cached at the browser
# * <tt>:id</tt> - reference to the component
Expand Down Expand Up @@ -87,14 +89,7 @@ def js_component(name, config = {})

# Register a component
def register_component(name)
current_components = read_inheritable_attribute(:components) || []
current_components << name
write_inheritable_attribute(:components, current_components.uniq)
end

# Returns registered components
def registered_components
read_inheritable_attribute(:components) || []
self.registered_components |= [name]
end

end
Expand Down
22 changes: 12 additions & 10 deletions lib/netzke/config_to_dsl_delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ module Netzke
module ConfigToDslDelegator
extend ActiveSupport::Concern

included do
class_attribute :delegated_options
self.delegated_options = []
end

module ClassMethods
# Delegates specified configuration options to the class level. See ConfigToDslDelegator.
def delegates_to_dsl(*option_names)
delegated_options = read_inheritable_attribute(:delegated_options) || []
delegated_options += option_names
write_inheritable_attribute(:delegated_options, delegated_options)
self.delegated_options |= option_names
end

def inherited(inherited_class) # :nodoc:
super

properties = read_inheritable_attribute(:delegated_options) || []
properties.size.times do |i|
inherited_class.class.send(:define_method, properties[i], lambda { |value|
default_config = read_inheritable_attribute(:default_config) || {}
default_config.merge!(properties[i].to_sym => value)
write_inheritable_attribute(:default_config, default_config)
properties = self.delegated_options
properties.each do |property|
inherited_class.class.send(:define_method, property, lambda { |value|
default_config = self.default_config_attr.dup
self.default_config_attr = default_config.merge(property.to_sym => value)
})
end
end
end
end
end
end
6 changes: 5 additions & 1 deletion lib/netzke/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Configuration
CONFIGURATION_LEVELS.each do |level|
define_method("weak_#{level}_options"){ {} }
end

class_attribute :default_config_attr
self.default_config_attr = {}

end

module ClassMethods
Expand Down Expand Up @@ -92,7 +96,7 @@ def app_level_config_excluding_parents

# Default config - before applying any passed configuration
def default_config
@default_config ||= {}.merge(weak_default_options).merge(self.class.default_instance_config).merge(self.class.read_inheritable_attribute(:default_config) || {})
@default_config ||= {}.merge(weak_default_options).merge(self.class.default_instance_config).merge(self.default_config_attr)
end

# Static, hardcoded config. Consists of default values merged with config that was passed during instantiation
Expand Down
2 changes: 1 addition & 1 deletion lib/netzke/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
require "netzke/core_ext/string"
require "netzke/core_ext/symbol"
require "netzke/core_ext/time_with_zone"
require "netzke/core_ext/class"

22 changes: 0 additions & 22 deletions lib/netzke/core_ext/class.rb

This file was deleted.

20 changes: 10 additions & 10 deletions lib/netzke/inheritance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ def class_ancestors
end
end

# Same as +read_inheritable_attribute+ returning a hash, but returns empty hash when it's equal to superclass's
def read_clean_inheritable_hash(attr_name)
res = read_inheritable_attribute(attr_name) || {}
# We don't want here any values from the superclass (which is the consequence of using inheritable attributes).
res == self.superclass.read_inheritable_attribute(attr_name) ? {} : res
# Same as +class_attribute+ returning a hash, but returns empty hash when it's equal to superclass's
def clean_class_attribute_hash(attr_name)
res = self.send(attr_name)
# We don't want here any values from the superclass (which is the consequence of using class attributes).
res == self.superclass.send(attr_name) ? {} : res
end

# Same as +read_inheritable_attribute+ returning a hash, but returns empty hash when it's equal to superclass's
def read_clean_inheritable_array(attr_name)
res = read_inheritable_attribute(attr_name) || []
# We don't want here any values from the superclass (which is the consequence of using inheritable attributes).
res == self.superclass.read_inheritable_attribute(attr_name) ? [] : res
# Same as +class_attribute+ returning an array, but returns empty array when it's equal to superclass's
def clean_class_attribute_array(attr_name)
res = self.send(attr_name) || []
# We don't want here any values from the superclass (which is the consequence of using class attributes).
res == self.superclass.send(attr_name) ? [] : res
end

end
Expand Down
53 changes: 34 additions & 19 deletions lib/netzke/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ module Javascript

class_attribute :js_included_files
self.js_included_files = []

# Returns all JS method definitions in a hash
class_attribute :js_methods_attr
self.js_methods_attr = {}

class_attribute :js_properties_attr
self.js_properties_attr = {}

# Returns all objects to be mixed in (as array of strings)
class_attribute :js_mixins_attr
self.js_mixins_attr = []

class_attribute :js_translated_properties_attr
self.js_translated_properties_attr = {}

class_attribute :js_base_class_attr
self.js_base_class_attr = 'Ext.panel.Panel'
end

module ClassMethods
Expand All @@ -32,7 +49,7 @@ module ClassMethods
#
# If called without parameters, returns the JS base class declared for the component.
def js_base_class(class_name = nil)
class_name.nil? ? (read_inheritable_attribute(:js_base_class) || "Ext.panel.Panel") : write_inheritable_attribute(:js_base_class, class_name)
class_name.nil? ? self.js_base_class_attr : self.js_base_class_attr = class_name
end

# Use it to define a public method of the component's JavaScript class, e.g.:
Expand All @@ -46,14 +63,13 @@ def js_base_class(class_name = nil)
# This will effectively result in definition of a public method called +doSomething+ in the JavaScript class (note the conversion from underscore_name to camelCaseName).
def js_method(name, definition = nil)
definition = yield.l if block_given?
current_js_methods = read_clean_inheritable_hash(:js_methods)
current_js_methods.merge!(name => definition.l)
write_inheritable_attribute(:js_methods, current_js_methods)
current_js_methods = clean_class_attribute_hash(:js_methods_attr).dup
self.js_methods_attr = current_js_methods.merge(name => definition.l)
end

# Returns all JS method definitions in a hash
def js_methods
read_clean_inheritable_hash(:js_methods)
clean_class_attribute_hash(:js_methods_attr)
end

# Use it to specify JS files to be loaded before this component's JS code. Useful when using external extensions required by this component.
Expand All @@ -72,7 +88,7 @@ def js_methods
def js_include(*args)
callr = caller.first

self.js_included_files += args.map{ |a| a.is_a?(Symbol) ? expand_js_include_path(a, callr) : a }
self.js_included_files |= args.map{ |a| a.is_a?(Symbol) ? expand_js_include_path(a, callr) : a }
end

# Used to define default properties of the JavaScript class, e.g.:
Expand All @@ -87,11 +103,10 @@ def js_include(*args)
# Note, that not all the configuration options can be defined on the prototype of the class. For example, defining +items+ on the prototype won't take any effect, so, +items+ should be passed as a configuration option at the moment of instantiation (see Netzke::Base#configuration and Netzke::Base#default_config).
def js_properties(hsh = nil)
if hsh.nil?
read_clean_inheritable_hash(:js_properties)
clean_class_attribute_hash(:js_properties_attr)
else
current_js_properties = read_clean_inheritable_hash(:js_properties)
current_js_properties.merge!(hsh)
write_inheritable_attribute(:js_properties, current_js_properties)
current_js_properties = clean_class_attribute_hash(:js_properties_attr).dup
self.js_properties_attr = current_js_properties.merge(hsh)
end
end

Expand All @@ -100,11 +115,11 @@ def js_properties(hsh = nil)
def js_property(name, value = nil)
name = name.to_sym
if value.nil?
(read_inheritable_attribute(:js_properties) || {})[name]
clean_class_attribute_hash(:js_properties_attr)[name]
else
current_js_properties = read_clean_inheritable_hash(:js_properties)
current_js_properties = clean_class_attribute_hash(:js_properties_attr).dup
current_js_properties[name] = value
write_inheritable_attribute(:js_properties, current_js_properties)
self.js_properties_attr = current_js_properties
end
end

Expand All @@ -122,9 +137,9 @@ def js_property(name, value = nil)
# TODO: make the name of the root property configurable
def js_translate(*properties)
if properties.empty?
read_clean_inheritable_hash(:js_translated_properties)
clean_class_attribute_hash(:js_translated_properties_attr)
else
current_translated_properties = read_clean_inheritable_hash(:js_translated_properties)
current_translated_properties = clean_class_attribute_hash(:js_translated_properties_attr).dup
properties.each do |p|
if p.is_a?(Hash)
# TODO: make it possible to nest translated objects
Expand All @@ -133,7 +148,7 @@ def js_translate(*properties)
end
end

write_inheritable_attribute(:js_translated_properties, current_translated_properties)
self.js_translated_properties_attr = current_translated_properties
end
end

Expand All @@ -160,15 +175,15 @@ def js_translate(*properties)
# With no parameters, will assume :component_class_name_underscored.
def js_mixin(*args)
args << name.split("::").last.underscore.to_sym if args.empty? # if no args provided, component_class_underscored_name is assumed
current_mixins = read_clean_inheritable_array(:js_mixins) || []
current_mixins = clean_class_attribute_array(:js_mixins_attr)
callr = caller.first
args.each{ |a| current_mixins << (a.is_a?(Symbol) ? File.read(expand_js_include_path(a, callr)) : File.read(a))}
write_inheritable_attribute(:js_mixins, current_mixins)
self.js_mixins_attr = current_mixins
end

# Returns all objects to be mixed in (as array of strings)
def js_mixins
read_clean_inheritable_array(:js_mixins) || []
clean_class_attribute_array(:js_mixins_attr)
end

# Builds this component's xtype
Expand Down
14 changes: 7 additions & 7 deletions lib/netzke/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module Netzke
module Plugins
extend ActiveSupport::Concern

included do
# Returns registered plugins
class_attribute :registered_plugins
self.registered_plugins = []
end

module ClassMethods
# Defines a plugin
def plugin(name, config = {}, &block)
Expand All @@ -11,15 +17,9 @@ def plugin(name, config = {}, &block)

# Register a plugin
def register_plugin(name)
current_plugins = read_inheritable_attribute(:plugins) || []
current_plugins << name
write_inheritable_attribute(:plugins, current_plugins.uniq)
self.registered_plugins |= [name]
end

# Returns registered plugins
def registered_plugins
read_inheritable_attribute(:plugins) || []
end
end

def plugins
Expand Down
15 changes: 7 additions & 8 deletions lib/netzke/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module Netzke
module Services
extend ActiveSupport::Concern

included do
# Returns all endpoints as a hash
class_attribute :endpoints
self.endpoints = {}
end

module ClassMethods
# Defines an endpoint - a connection point between the client side of a component and its server side. For example:
#
Expand Down Expand Up @@ -44,11 +50,6 @@ def endpoint(name, options = {}, &block)
end
end

# Returns all endpoints as a hash
def endpoints
read_inheritable_attribute(:endpoints) || {}
end

def api(*api_points) #:nodoc:
::ActiveSupport::Deprecation.warn("Using the 'api' call has no longer effect. Define endpoints instead.", caller)
end
Expand All @@ -57,9 +58,7 @@ def api(*api_points) #:nodoc:

# Registers an endpoint
def register_endpoint(ep, options)
current_endpoints = read_inheritable_attribute(:endpoints) || {}
current_endpoints.merge!(ep => options)
write_inheritable_attribute(:endpoints, current_endpoints)
self.endpoints = self.endpoints.dup.merge(ep => options)
end

end
Expand Down

0 comments on commit 465b729

Please sign in to comment.