Permalink
Browse files

Whitespace is a renewable resource, but let's use it wisely, folks.

  • Loading branch information...
therealadam committed Feb 17, 2010
1 parent ff786ad commit 61c39da65ae93f04b6579e8ec399aa7b9a3086c1
Showing with 63 additions and 57 deletions.
  1. +63 −57 lib/attribute_mapper.rb
View
@@ -37,73 +37,79 @@ def map_attribute(attribute, options)
end
private
- def add_accessor_for(attribute, mapping)
- class_eval(<<-EVAL)
- class << self
- def #{attribute.to_s.pluralize}
- #{mapping.inspect}
- end
+
+ def add_accessor_for(attribute, mapping)
+ class_eval(<<-EVAL)
+ class << self
+ def #{attribute.to_s.pluralize}
+ #{mapping.inspect}
end
- EVAL
end
+ EVAL
+ end
- def add_predicates_for(attribute, names)
- names.each do |name|
- class_eval(<<-RUBY)
- def #{name}?
- self.#{attribute} == :#{name}
- end
- RUBY
- end
+ def add_predicates_for(attribute, names)
+ names.each do |name|
+ class_eval(<<-RUBY)
+ def #{name}?
+ self.#{attribute} == :#{name}
+ end
+ RUBY
end
+ end
- def add_options_helper_for(attribute, mapping)
- class_eval(<<-EVAL, __FILE__, __LINE__)
- def #{attribute}_options(sort_by_keys=true)
- options = self.class.#{attribute.to_s.pluralize}.sort { |l, r|
- sort_by_keys ? l.first.to_s <=> r.first.to_s : l.last <=> r.last
- }.map { |f|
- [f[0].to_s.humanize, f[0]]
- }
- self.#{attribute}.present? ? [options, {:selected => self.#{attribute}}] : [options]
- end
- EVAL
+ def add_options_helper_for(attribute, mapping)
+ class_eval(<<-EVAL, __FILE__, __LINE__)
+ def #{attribute}_options(sort_by_keys=true)
+ options = self.class.#{attribute.to_s.pluralize}.sort { |l, r|
+ sort_by_keys ? l.first.to_s <=> r.first.to_s : l.last <=> r.last
+ }.map { |f|
+ [f[0].to_s.humanize, f[0]]
+ }
+ self.#{attribute}.present? ? [options, {:selected => self.#{attribute}}] : [options]
end
+ EVAL
+ end
- def override(*args)
- override_getters *args
- override_setters *args
- end
-
- def override_getters(attribute)
- class_eval(<<-EVAL)
- def #{attribute}
- self.class.#{attribute.to_s.pluralize}.invert[read_attribute(:#{attribute})]
- end
- EVAL
- end
-
- def override_setters(attribute)
- class_eval(<<-EVAL)
- def #{attribute}=(raw_value)
- value = resolve_value_of :#{attribute}, raw_value
- write_attribute(:#{attribute}, value)
- end
- EVAL
- end
-
- def verify_existence_of(attribute)
- raise ArgumentError, "`#{attribute}' is not an attribute of `#{self}'" unless column_names.include?(attribute.to_s)
- end
+ def override(*args)
+ override_getters *args
+ override_setters *args
+ end
+
+ def override_getters(attribute)
+ class_eval(<<-EVAL)
+ def #{attribute}
+ self.class.#{attribute.to_s.pluralize}.invert[read_attribute(:#{attribute})]
+ end
+ EVAL
+ end
+
+ def override_setters(attribute)
+ class_eval(<<-EVAL)
+ def #{attribute}=(raw_value)
+ value = resolve_value_of :#{attribute}, raw_value
+ write_attribute(:#{attribute}, value)
+ end
+ EVAL
+ end
+
+ def verify_existence_of(attribute)
+ raise ArgumentError, "`#{attribute}' is not an attribute of `#{self}'" unless column_names.include?(attribute.to_s)
+ end
+
end
module InstanceMethods
+
private
- def resolve_value_of(attribute, raw_value)
- check_value = raw_value.is_a?(String) ? raw_value.to_sym : raw_value
- mapping = self.class.send(attribute.to_s.pluralize)
- raise ArgumentError, "`#{check_value}' not present in attribute mapping `#{mapping.inspect}'" unless mapping.to_a.flatten.include? check_value
- mapping[check_value] || check_value
- end
+
+ def resolve_value_of(attribute, raw_value)
+ check_value = raw_value.is_a?(String) ? raw_value.to_sym : raw_value
+ mapping = self.class.send(attribute.to_s.pluralize)
+ raise ArgumentError, "`#{check_value}' not present in attribute mapping `#{mapping.inspect}'" unless mapping.to_a.flatten.include? check_value
+ mapping[check_value] || check_value
+ end
+
end
+
end

0 comments on commit 61c39da

Please sign in to comment.