Module: SelectableAttr::Base::ClassMethods

Defined in:
lib/selectable_attr/base.rb

Constant Summary collapse

DEFAULT_SELECTABLE_ATTR_NAME_PATTERN =
/(_cd$|_code$|_cds$|_codes$)/

Instance Method Summary collapse

Instance Method Details

#attr_enumeable_base(*args, &block) ⇒ Object



133
134
135
# File 'lib/selectable_attr/base.rb', line 133

def attr_enumeable_base(*args, &block)
  @base_name_processor = block
end

#create_enum(&block) ⇒ Object



157
158
159
160
161
# File 'lib/selectable_attr/base.rb', line 157

def create_enum(&block)
  result = Enum.new
  result.instance_eval(&block)
  result
end

#define_accessor(context) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/selectable_attr/base.rb', line 175

def define_accessor(context)
  attr = context[:attr]
  return unless (instance_methods & [attr, "#{attr}="]).empty?
  if context[:attr_accessor]
    if context[:default]
      if respond_to?(:attr_accessor_with_default)
        attr_accessor_with_default(attr, context[:default])
      else
        instance_var_name = "@#{attr}"
        attr_writer(attr)
        define_method(attr) do
          value = instance_variable_get(instance_var_name)
          instance_variable_set(instance_var_name, value = context[:default]) unless value
          value
        end
      end
    else
      attr_accessor(attr)
    end
  else
    if context[:default]
      SelectableAttr.logger.warn(":default option ignored for #{attr}")
    end
  end
end

#define_enum(context) ⇒ Object



163
164
165
166
167
# File 'lib/selectable_attr/base.rb', line 163

def define_enum(context)
  base_name = context[:base_name]
  const_name = "#{base_name.upcase}_ENUM"
  const_set(const_name, context[:enum]) unless const_defined?(const_name)
end

#define_enum_array_instance_methods(context) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/selectable_attr/base.rb', line 252

def define_enum_array_instance_methods(context)
  attr = context[:attr]
  base_name = context[:base_name]
  # ActiveRecord::Baseから継承している場合は、基本カラムに対応するメソッドはない
  self.module_eval(<<-"EOS")
    def #{base_name}_ids
      #{base_name}_hash_array_selected.map{|hash|hash[:id]}
    end
    def #{base_name}_ids=(ids)
      ids = ids.split(',') if ids.is_a?(String)
      ids = ids ? ids.map(&:to_s) : []
      update_#{base_name}_hash_array{|hash|ids.include?(hash[:id].to_s)}
    end
    def #{base_name}_hash_array
      self.class.#{base_name}_to_hash_array(self.class.#{base_name}_enum, #{attr})
    end
    def #{base_name}_hash_array=(hash_array)
      self.#{attr} = self.class.hash_array_to_#{base_name}(self.class.#{base_name}_enum, hash_array)
    end
    def #{base_name}_hash_array_selected
      #{base_name}_hash_array.select{|hash|!!hash[:select]}
    end
    def update_#{base_name}_hash_array(&block)
      hash_array = #{base_name}_hash_array.map do |hash|
        hash.merge(:select => yield(hash))
      end
      self.#{base_name}_hash_array = hash_array
    end
    def #{base_name}_keys
      #{base_name}_hash_array_selected.map{|hash|hash[:key]}
    end
    def #{base_name}_keys=(keys)
      update_#{base_name}_hash_array{|hash|keys.include?(hash[:key])}
    end
    def #{base_name}_selection
      #{base_name}_hash_array.map{|hash|!!hash[:select]}
    end
    def #{base_name}_selection=(selection)
      idx = -1
      update_#{base_name}_hash_array{|hash| idx += 1; !!selection[idx]}
    end
    def #{base_name}_names
      #{base_name}_hash_array_selected.map{|hash|hash[:name]}
    end
    def #{base_name}_entries
      ids = #{base_name}_ids
      self.class.#{base_name}_enum.select{|entry|ids.include?(entry.id)}
    end
  EOS
end

#define_enum_class_methods(context) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/selectable_attr/base.rb', line 201

def define_enum_class_methods(context)
  base_name = context[:base_name]
  enum = context[:enum]
  mod = Module.new
  mod.module_eval do
    define_method("#{base_name}_enum"){enum}
    define_method("#{base_name}_hash_array"){enum.to_hash_array}
    define_method("#{base_name}_entries"){enum.entries}
    define_method("#{base_name}_options"){|*ids_or_keys|enum.options(*ids_or_keys)}
    define_method("#{base_name}_ids"){|*ids_or_keys| enum.ids(*ids_or_keys)}
    define_method("#{base_name}_keys"){|*ids_or_keys|enum.keys(*ids_or_keys)}
    define_method("#{base_name}_names"){|*ids_or_keys|enum.names(*ids_or_keys)}
    define_method("#{base_name}_key_by_id"){|id|enum.key_by_id(id)}
    define_method("#{base_name}_id_by_key"){|key|enum.id_by_key(key)}
    define_method("#{base_name}_name_by_id"){|id|enum.name_by_id(id)}
    define_method("#{base_name}_name_by_key"){|key|enum.name_by_key(key)}
    define_method("#{base_name}_entry_by_id"){|id|enum.entry_by_id(id)}
    define_method("#{base_name}_entry_by_key"){|key|enum.entry_by_key(key)}
  end
  if convertors = ENUM_ARRAY_METHODS[context[:convert_with] || :none]
    mod.module_eval do
      define_method("#{base_name}_to_hash_array", convertors[:to_hash_array])
      define_method("hash_array_to_#{base_name}", convertors[:to_attr_value])
    end
  end
  self.extend(mod)
end

#define_enum_instance_methods(context) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/selectable_attr/base.rb', line 229

def define_enum_instance_methods(context)
  attr = context[:attr]
  base_name = context[:base_name]
  instance_methods = <<-EOS
    def #{base_name}_key
      self.class.#{base_name}_key_by_id(#{attr})
    end
    def #{base_name}_key=(key)
      self.#{attr} = self.class.#{base_name}_id_by_key(key)
    end
    def #{base_name}_name
      self.class.#{base_name}_name_by_id(#{attr})
    end
    def #{base_name}_entry
      self.class.#{base_name}_entry_by_id(#{attr})
    end
    def #{base_name}_entry
      self.class.#{base_name}_entry_by_id(#{attr})
    end
  EOS
  self.module_eval(instance_methods)
end

#enum(*args, &block) ⇒ Object Also known as: single_selectable_attr, selectable_attr



75
76
77
78
79
80
81
# File 'lib/selectable_attr/base.rb', line 75

def enum(*args, &block)
  process_definition(block, *args) do |enum, context|
    self.single_selectable_attrs << context[:attr].to_s
    define_enum_class_methods(context)
    define_enum_instance_methods(context)
  end
end

#enum_array(*args, &block) ⇒ Object Also known as: multi_selectable_attr



86
87
88
89
90
91
92
93
94
# File 'lib/selectable_attr/base.rb', line 86

def enum_array(*args, &block)
  base_options = args.last.is_a?(Hash) ? args.pop : {}
  args << base_options # .update({:attr_accessor => false})
  process_definition(block, *args) do |enum, context|
    self.multi_selectable_attrs << context[:attr].to_s
    define_enum_class_methods(context)
    define_enum_array_instance_methods(context)
  end
end

#enum_base_name(attr) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/selectable_attr/base.rb', line 137

def enum_base_name(attr)
  if @base_name_processor
    @base_name_processor.call(attr).to_s
  else
    attr.to_s.gsub(selectable_attr_name_pattern, '')
  end
end

#enum_for(attr) ⇒ Object



169
170
171
172
173
# File 'lib/selectable_attr/base.rb', line 169

def enum_for(attr)
  base_name = enum_base_name(attr)
  name = "#{base_name.upcase}_ENUM"
  const_defined?(name) ? const_get(name) : nil
end

#has_attr(attr) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/selectable_attr/base.rb', line 119

def has_attr(attr)
  return true if self.method_defined?(attr)
  return false unless self.respond_to?(:columns)
  if self.respond_to?(:connection) and self.respond_to?(:connected?)
    begin
      self.connection unless self.connected?
    rescue Exception
      return nil if !self.connected?
    end
  end
  (respond_to?(:table_exists?) && self.table_exists?) ?
    (self.columns || []).any?{|col|col.name.to_s == attr.to_s} : false
end

#multi_selectable_attrsObject



65
66
67
68
# File 'lib/selectable_attr/base.rb', line 65

def multi_selectable_attrs
  @multi_selectable_attrs_hash ||= {};
  @multi_selectable_attrs_hash[self] ||= []
end

#process_definition(block, *args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/selectable_attr/base.rb', line 97

def process_definition(block, *args)
  base_options = args.last.is_a?(Hash) ? args.pop : {}
  enum = base_options[:enum] || create_enum(&block)
  args.each do |attr|
    context = {
      :enum => enum,
      :attr_accessor => !has_attr(attr),
      :attr => attr,
      :base_name => enum_base_name(attr)
    }.update(base_options)
    define_enum(context)
    define_accessor(context)
    yield(enum, context)
    unless enum.i18n_scope
      paths = [:selectable_attrs] + self.name.to_s.split('::').map{|s| s.to_sym}
      paths << attr.to_sym
      enum.i18n_scope(*paths)
    end
  end
  enum
end

#selectable_attr_name_patternObject Also known as: enum_name_pattern



147
148
149
# File 'lib/selectable_attr/base.rb', line 147

def selectable_attr_name_pattern
  @selectable_attr_name_pattern ||= DEFAULT_SELECTABLE_ATTR_NAME_PATTERN
end

#selectable_attr_name_pattern=(value) ⇒ Object Also known as: enum_name_pattern=



152
153
154
# File 'lib/selectable_attr/base.rb', line 152

def selectable_attr_name_pattern=(value)
  @selectable_attr_name_pattern = value
end

#selectable_attr_type_for(attr) ⇒ Object



70
71
72
73
# File 'lib/selectable_attr/base.rb', line 70

def selectable_attr_type_for(attr)
  single_selectable_attrs.include?(attr.to_s) ? :single :
  multi_selectable_attrs.include?(attr.to_s) ? :multi : nil
end

#single_selectable_attrsObject



60
61
62
63
# File 'lib/selectable_attr/base.rb', line 60

def single_selectable_attrs
  @single_selectable_attrs_hash ||= {};
  @single_selectable_attrs_hash[self] ||= []
end