Class: Aduki::AttrFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/aduki/attr_finder.rb

Class Method Summary collapse

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}"
  "remove_method :\#{id_method}  if method_defined?(:\#{id_method})\nremove_method :\#{id_method}= if method_defined?(:\#{id_method}=)\nremove_method :\#{name}       if method_defined?(:\#{name})\nremove_method :\#{name}=      if method_defined?(:\#{name}=)\n\nattr_reader :\#{id_method}\n\ndef \#{id_method}= x\n@\#{id_method}= x\n@\#{name} = nil\nend\n\ndef \#{name}\n@\#{name} ||= \#{klass}.\#{finder}(@\#{id_method}) unless @\#{id_method}.nil? || @\#{id_method} == ''\n@\#{name}\nend\n\ndef \#{name}= x\n@\#{name} = x\n@\#{id_method} = x ? x.\#{id} : nil\nend\n"
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, options={ }
  singular = singularize name.to_s
  klass    = options[:class_name] || camelize(singular)
  id_method = "#{singular}_#{pluralize id}"
  "remove_method :\#{id_method}  if method_defined?(:\#{id_method})\nremove_method :\#{id_method}= if method_defined?(:\#{id_method}=)\nremove_method :\#{name}       if method_defined?(:\#{name})\nremove_method :\#{name}=      if method_defined?(:\#{name}=)\n\nattr_reader :\#{id_method}\n\ndef \#{id_method}= x\n@\#{id_method} = x\n@\#{name}      = nil\nend\n\ndef \#{name}\n@\#{name} ||= \#{klass}.\#{finder} @\#{id_method} unless @\#{id_method}.nil?\n@\#{name}\nend\n\ndef \#{name}= x\n@\#{id_method} = x ? x.map(&:\#{id}) : nil\n@\#{name}      = x\nend\n"
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