Class: Fabrication::Support

Inherits:
Object
  • Object
show all
Defined in:
lib/fabrication/support.rb

Class Method Summary collapse

Class Method Details

.class_for(class_or_to_s) ⇒ Object



8
9
10
11
12
13
# File 'lib/fabrication/support.rb', line 8

def class_for(class_or_to_s)
  class_name = variable_name_to_class_name(class_or_to_s)
  constantize(class_name)
rescue NameError => e
  raise Fabrication::UnfabricatableError.new(class_or_to_s, e)
end

.constantize(camel_cased_word) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fabrication/support.rb', line 15

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  Object.const_get(camel_cased_word) if names.empty?
  names.shift if names.size > 1 && names.first.empty?
  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)

        const
      end
      constant.const_get(name, false)
    end
  end
end

.extract_options!(args) ⇒ Object



38
39
40
# File 'lib/fabrication/support.rb', line 38

def extract_options!(args)
  args.last.is_a?(::Hash) ? args.pop : {}
end

.fabricatable?(name) ⇒ Boolean



4
5
6
# File 'lib/fabrication/support.rb', line 4

def fabricatable?(name)
  Fabrication.manager[name] || class_for(name)
end

.find_definitionsObject



54
55
56
57
58
# File 'lib/fabrication/support.rb', line 54

def find_definitions
  puts 'DEPRECATION WARNING: Fabrication::Support.find_definitions has been replaced ' \
       'by Fabrication.manager.load_definitions and will be removed in 3.0.0.'
  Fabrication.manager.load_definitions
end

.hash_classObject



60
61
62
# File 'lib/fabrication/support.rb', line 60

def hash_class
  @hash_class ||= defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess : Hash
end

.singularize(string) ⇒ Object



64
65
66
67
68
# File 'lib/fabrication/support.rb', line 64

def singularize(string)
  string.singularize
rescue StandardError
  string.end_with?('s') ? string[0..-2] : string
end

.underscore(string) ⇒ Object



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

def underscore(string)
  string.gsub(/::/, '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .downcase
end

.variable_name_to_class_name(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fabrication/support.rb', line 42

def variable_name_to_class_name(name)
  name_string = name.to_s

  if name_string.respond_to?(:camelize)
    name_string.camelize
  else
    name_string.gsub(%r{/(.?)}) do
      "::#{Regexp.last_match(1).upcase}"
    end.gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase }
  end
end