Module: Sequel::Plugins::Canonical::ClassMethods

Defined in:
lib/cortex_reaver/support/canonical.rb

Constant Summary collapse

CANONICAL_NAME_ATTR =

The canonical name attribute

:name
CANONICAL_INFERENCE_ATTR =

The attribute we infer the canonical name from, if not set.

:title

Instance Method Summary collapse

Instance Method Details

#canonical_inference_attr(attr = nil) ⇒ Object

Sets the attribute we infer the canonical name from to attr, or gets that attr if nil.



70
71
72
73
74
75
76
# File 'lib/cortex_reaver/support/canonical.rb', line 70

def canonical_inference_attr(attr = nil)
  if attr
    @canonical_inference_attr = attr.to_sym
  else
    @canonical_inference_attr || CANONICAL_INFERENCE_ATTR
  end
end

#canonical_name_attr(attr = nil) ⇒ Object

Sets the canonical name attribute to attr. Returns it if nil.



79
80
81
82
83
84
85
# File 'lib/cortex_reaver/support/canonical.rb', line 79

def canonical_name_attr(attr = nil)
  if attr
   @canonical_name_attr = attr.to_sym
  else
    @canonical_name_attr || CANONICAL_NAME_ATTR
  end
end

#canonicalize(string, opts = {}) ⇒ Object

Canonicalize a string. Optionally, ignore conflicts with the record with opts



23
24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
# File 'lib/cortex_reaver/support/canonical.rb', line 23

def canonicalize(string, opts={})
  # Lower case, remove special chars, and replace with hyphens.
  proper = formalize string

  # If proper is blank, just return it at this point.
  if proper.blank?
    return proper
  end

  # Numeric suffix to append
  suffix = nil

  # Get similar names from the class
  similar = similar_canonical_names(proper, opts)

  # Get reserved names from the class
  reserved_canonical_names.each do |name|
    if name =~ /^#{proper}(-\d+)?$/
      similar << name
    end
  end

  # Extract numeric suffices
  suffices = {}
  similar.each do |name|
    suffices[name[/\d+$/].to_i] = true
  end

  # Compute suffix
  unless suffices.empty?
    i = 1
    while suffices.include? i
      i += 1
    end
    suffix = i
  end
 
  # Apply suffix
  if suffix
    proper + '-' + suffix.to_s
  else
    proper
  end
end

#formalize(string) ⇒ Object

Lower case, remove special chars, and replace with hyphens.



88
89
90
# File 'lib/cortex_reaver/support/canonical.rb', line 88

def formalize(string)
  string.downcase.gsub(/'"/, '').gsub(/[^a-z0-9_]/, '-').squeeze('-')[0..250].sub(/-$/, '')
end

#reserved_canonical_namesObject

Canonical names which cannot be reserved.



13
14
15
# File 'lib/cortex_reaver/support/canonical.rb', line 13

def reserved_canonical_names
  @reserved_canonical_names ||= []
end

#reserved_canonical_names=(names) ⇒ Object



17
18
19
# File 'lib/cortex_reaver/support/canonical.rb', line 17

def reserved_canonical_names=(names)
  @reserved_canonical_names = names
end

#similar_canonical_names(proper, opts = {}) ⇒ Object

Canonicalize only in the context of our parent’s namespace. Takes a proper canonical name to check for conflicts with, and an optional id to ignore conflicts with



95
96
97
98
99
100
101
102
103
104
# File 'lib/cortex_reaver/support/canonical.rb', line 95

def similar_canonical_names(proper, opts={})
  id = opts[:id]
  similar = []
  if filter(canonical_name_attr => proper).exclude(:id => id).limit(1).count > 0
    # This name already exists, and it's not ours.
    similar << proper
    similar += filter(canonical_name_attr.like(/^#{proper}\-[0-9]+$/)).map(canonical_name_attr)
  end
  similar
end