Module: Labelized::Support

Defined in:
lib/labelized/support.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_params(*params) ⇒ Object



9
10
11
12
13
14
# File 'lib/labelized/support.rb', line 9

def self.extract_params(*params)
  labels = params.to_a.flatten.compact
  options = labels[-1].kind_of?(Hash) ? labels.slice!(-1) : {}
  labels = labels.map(&:to_sym)
  [labels, options]
end

.singular?(str) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/labelized/support.rb', line 5

def self.singular?(str)
  str.to_s.pluralize != str.to_s && str.to_s.singularize == str.to_s
end

Instance Method Details

#setup_label_scope(scopes) ⇒ Object



25
26
27
28
29
# File 'lib/labelized/support.rb', line 25

def setup_label_scope(scopes)
  self.class_eval do
    scope :label_scope, lambda {|labeled| where(*scopes.collect{|s| {s => labeled.send(s)}}.flatten)}
  end
end

#setup_labelized(params) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/labelized/support.rb', line 16

def setup_labelized(params)
  label_sets, options = Support.extract_params(params)
  scopes = [options[:scope]].flatten
  
  self.setup_labelized_options(options) 
  self.setup_labelized_label_set_names(label_sets) 
  self.setup_label_scope(scopes)
end

#setup_labelized_label_set_names(names) ⇒ Object



36
37
38
39
# File 'lib/labelized/support.rb', line 36

def setup_labelized_label_set_names(names)
  write_inheritable_attribute(:labelized_label_set_names, names)
  class_inheritable_reader(:labelized_label_set_names)
end

#setup_labelized_options(options) ⇒ Object



31
32
33
34
# File 'lib/labelized/support.rb', line 31

def setup_labelized_options(options)
  write_inheritable_attribute(:labelized_options, options)
  class_inheritable_reader(:labelized_options)
end