Module: Exposant::Model::ClassMethods

Defined in:
lib/exposant/model.rb

Instance Method Summary collapse

Instance Method Details

#exposant(obj, variant = nil, type = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/exposant/model.rb', line 34

def exposant(obj, variant = nil, type = nil)
  obj.extend(ExposantMethods)
  obj.model_klass = self

  if type.present? && type != :exposant
    raise 'Type must be a symbol' unless type.is_a?(Symbol)

    obj.singleton_class.class_eval do
      define_method type do |var = nil|
        exposant(var, type)
      end
    end
  end

  obj.exposant(variant, type)
end

#exposant_class(variant = nil, type = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/exposant/model.rb', line 51

def exposant_class(variant = nil, type = nil)
  type_name = (type || exposant_type).to_s.camelcase
  klass = if @exposant_class.present?
            @exposant_class
          else
            name.dup.concat(type_name)
          end

  klass = klass
          .split('::')
          .tap { |arr| arr.last.prepend(variant&.to_s&.downcase&.capitalize || '') }
          .join('::')

  raise "Missing exposant #{klass}" unless const_defined?(klass)

  klass.constantize
end

#exposant_typeObject



30
31
32
# File 'lib/exposant/model.rb', line 30

def exposant_type
  @exposant_type || :exposant
end

#has_exposant(name: nil, type: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/exposant/model.rb', line 11

def has_exposant(name: nil, type: nil)
  @exposant_class = name
  @exposant_type = type

  if type.present? && type != :exposant
    raise 'Type must be a symbol' unless type.is_a?(Symbol)

    define_method type do |variant = nil|
      exposant(variant, type)
    end

    define_singleton_method type do |obj, variant = nil|
      exposant(obj, variant, type)
    end
  end

  self
end