Module: ActiveForm::Mixins::LoaderMethods::ClassMethods

Defined in:
lib/active_form/mixins/loader_methods.rb

Instance Method Summary collapse

Instance Method Details

#build(definition_name, *args, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/active_form/mixins/loader_methods.rb', line 20

def build(definition_name, *args, &block)
  if klass = self.get(definition_name) 
    return instance(definition_name, klass, *args, &block)
  end
  nil
end

#const_missing(type) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/active_form/mixins/loader_methods.rb', line 61

def const_missing(type)
  begin
    load(type) && get(type)        
  rescue NotFoundException
  rescue  
    super
  end
end

#exists?(type) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/active_form/mixins/loader_methods.rb', line 44

def exists?(type)
  load(type) rescue nil unless loaded?(type)
  loaded?(type)
end

#get(type, &block) ⇒ Object Also known as: modify



27
28
29
30
31
32
# File 'lib/active_form/mixins/loader_methods.rb', line 27

def get(type, &block)
  load(type) rescue nil unless loaded?(type)
  klass = self.const_get(type_classname(type)) rescue nil
  klass.module_eval(&block) if klass && block_given?
  klass
end

#instance(klass, *args, &block) ⇒ Object



16
17
18
# File 'lib/active_form/mixins/loader_methods.rb', line 16

def instance(klass, *args, &block)
  raise ActiveForm::StubException  
end

#load(type) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
# File 'lib/active_form/mixins/loader_methods.rb', line 35

def load(type)
  self.load_paths.reverse.each do |dir|
    loadable_file = ::File.join(dir, "#{type_filename(type)}.rb")
    require loadable_file if ::File.exists?(loadable_file)
  end
  raise NotFoundException unless self.loaded?(type)
  true
end

#loaded?(type) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_form/mixins/loader_methods.rb', line 49

def loaded?(type)
  self.const_defined?(type_classname(type))
end

#register(elem_class) ⇒ Object



12
13
14
# File 'lib/active_form/mixins/loader_methods.rb', line 12

def register(elem_class)
  raise ActiveForm::StubException  
end

#type_classname(type) ⇒ Object



57
58
59
# File 'lib/active_form/mixins/loader_methods.rb', line 57

def type_classname(type)
  type.to_s.camelize
end

#type_filename(type) ⇒ Object



53
54
55
# File 'lib/active_form/mixins/loader_methods.rb', line 53

def type_filename(type)
  type.to_s.camelize.underscore
end