Class: Aduki::AttrFinder
- Inherits:
-
Object
- Object
- Aduki::AttrFinder
- Defined in:
- lib/aduki/attr_finder.rb
Class Method Summary collapse
- .attr_finder_text(finder, id, name, klass) ⇒ Object
- .attr_finders_text(finder, id, *args) ⇒ Object
- .camelize(str) ⇒ Object
- .hashify_args(a) ⇒ Object
- .one2many_attr_finder_text(finder, id, name, options = { }) ⇒ Object
- .pluralize(str) ⇒ Object
- .singularize(str) ⇒ Object
Class Method Details
.attr_finder_text(finder, id, name, klass) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/aduki/attr_finder.rb', line 36 def self.attr_finder_text finder, id, name, klass id_method = "#{name}_#{id}" <<EVAL remove_method :#{id_method} if method_defined?(:#{id_method}) remove_method :#{id_method}= if method_defined?(:#{id_method}=) remove_method :#{name} if method_defined?(:#{name}) remove_method :#{name}= if method_defined?(:#{name}=) attr_reader :#{id_method} def #{id_method}= x @#{id_method}= x @#{name} = nil end def #{name} @#{name} ||= #{klass}.#{finder}(@#{id_method}) unless @#{id_method}.nil? || @#{id_method} == '' @#{name} end def #{name}= x @#{name} = x @#{id_method} = x ? x.#{id} : nil end EVAL end |
.attr_finders_text(finder, id, *args) ⇒ Object
30 31 32 33 34 |
# File 'lib/aduki/attr_finder.rb', line 30 def self.attr_finders_text finder, id, *args hashify_args(args).map { |name, klass| attr_finder_text finder, id, name, klass }.join("\n") end |
.camelize(str) ⇒ Object
3 |
# File 'lib/aduki/attr_finder.rb', line 3 def self.camelize str ; str.to_s.camelize ; end |
.hashify_args(a) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/aduki/attr_finder.rb', line 18 def self.hashify_args a return a.first if a.first.is_a? Hash a.inject({ }) { |hash, arg| if arg.is_a?(Hash) hash.merge arg else hash[arg] = camelize arg.to_s hash end } end |
.one2many_attr_finder_text(finder, id, name, options = { }) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/aduki/attr_finder.rb', line 63 def self.one2many_attr_finder_text finder, id, name, ={ } singular = singularize name.to_s klass = [:class_name] || camelize(singular) id_method = "#{singular}_#{pluralize id}" <<EVAL remove_method :#{id_method} if method_defined?(:#{id_method}) remove_method :#{id_method}= if method_defined?(:#{id_method}=) remove_method :#{name} if method_defined?(:#{name}) remove_method :#{name}= if method_defined?(:#{name}=) attr_reader :#{id_method} def #{id_method}= x @#{id_method} = x @#{name} = nil end def #{name} @#{name} ||= #{klass}.#{finder} @#{id_method} unless @#{id_method}.nil? @#{name} end def #{name}= x @#{id_method} = x ? x.map(&:#{id}) : nil @#{name} = x end EVAL end |
.pluralize(str) ⇒ Object
5 |
# File 'lib/aduki/attr_finder.rb', line 5 def self.pluralize str ; str.to_s.pluralize ; end |
.singularize(str) ⇒ Object
4 |
# File 'lib/aduki/attr_finder.rb', line 4 def self.singularize str ; str.to_s.singularize ; end |