Permalink
Browse files

some rails flavor

  • Loading branch information...
shell committed Feb 6, 2012
1 parent e5a5b5a commit 3d119271c41c519b4b43af6f72293a01f71ab01f
Showing with 37 additions and 40 deletions.
  1. +2 −4 lib/a_a_n/association_as_name.rb
  2. +35 −36 lib/a_a_n/keeper.rb
@@ -1,4 +1,4 @@
-###
+#
# Module that allows assing attributes by <tt>name</tt>(or any other method)
#
# Example:
@@ -17,9 +17,7 @@
# end
module AAN
module AssociationAsName
- def self.included(base)
- base.extend(ClassMethods)
- end
+ extend ActiveSupport::Concern
module ClassMethods
def acts_as_aan &block
View
@@ -1,50 +1,49 @@
module AAN
- class Keeper
- cattr_reader :current_model
-
- class << self
- def nullify_aliased_methods_for model, assoc
- aliased_methods = []
- AAN::Keeper.aliases_for(model, assoc).each do |aliased_method|
- aliased_methods << "@#{aliased_method} = nil"
- end
- aliased_methods.join("\n")
- end
+ module Keeper
+ mattr_reader :current_model
- def structure
- @@structures ||= {}
- end
+ extend self
- def [](*args)
- (structure[args.first] ||= {})
+ def nullify_aliased_methods_for model, assoc
+ aliased_methods = []
+ AAN::Keeper.aliases_for(model, assoc).each do |aliased_method|
+ aliased_methods << "@#{aliased_method} = nil"
end
+ aliased_methods.join("\n")
+ end
- def associations model, &block
- @@current_model = model
- instance_eval &block
- end
+ def structure
+ @@structures ||= {}
+ end
- def association(name, &block)
- sub_structure = block.call
- sub_structure.each do |element|
- if element.is_a? Hash
- element.symbolize_keys!
- element = element.to_a.flatten
- elsif
- element = [element.to_sym, "#{name}_#{element}".to_sym]
- end
- (AAN::Keeper[current_model][name] ||= []) << element
- end
- end
+ def [](*args)
+ (structure[args.first] ||= {})
+ end
- protected
+ def associations model, &block
+ @@current_model = model
+ instance_eval &block
+ end
- def aliases_for model, assoc
- structure[model][assoc].collect do |params|
- params.last
+ def association(name, &block)
+ sub_structure = block.call
+ sub_structure.each do |element|
+ if element.is_a? Hash
+ element.symbolize_keys!
+ element = element.to_a.flatten
+ elsif
+ element = [element.to_sym, "#{name}_#{element}".to_sym]
end
+ (AAN::Keeper[current_model][name] ||= []) << element
end
end
+ protected
+
+ def aliases_for model, assoc
+ structure[model][assoc].collect do |params|
+ params.last
+ end
+ end
end
end

0 comments on commit 3d11927

Please sign in to comment.