Class: Exposant::Base

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
Contextualizable
Defined in:
lib/exposant/base.rb

Instance Attribute Summary

Attributes included from Contextualizable

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contextualizable

#contextualize, #contextualized?

Constructor Details

#initializeBase

Returns a new instance of Base.



6
7
8
9
10
# File 'lib/exposant/base.rb', line 6

def initialize(*)
  super

  extend(Exposant::Collection) if __getobj__.is_a?(Enumerable)
end

Class Method Details

.belongs_to_model(name) ⇒ Object



32
33
34
# File 'lib/exposant/base.rb', line 32

def self.belongs_to_model(name)
  exposed_class(name)
end

.custom_type?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/exposant/base.rb', line 60

def self.custom_type?
  exposant_type != :exposant
end

.exposant_baseObject



68
69
70
# File 'lib/exposant/base.rb', line 68

def self.exposant_base
  @exposant_base = true
end

.exposant_base?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/exposant/base.rb', line 64

def self.exposant_base?
  !!@exposant_base
end

.exposant_type(value = nil) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/exposant/base.rb', line 47

def self.exposant_type(value = nil)
  @exposant_type = value if value.present?
  return @exposant_type if @exposant_type.present?

  return ancestors[1].exposant_type unless ancestors[1] == Exposant::Base

  :exposant
end

.exposant_variantObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/exposant/base.rb', line 72

def self.exposant_variant
  exposed_name = exposed_class.name.demodulize

  variant = name
            .split('::')
            .tap { |arr| arr.last.gsub!(/#{exposed_name}#{type_name}$/, '') }
            .last
            .downcase

  return nil if variant.blank?

  variant.to_sym
end

.exposed_class(value = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/exposant/base.rb', line 36

def self.exposed_class(value = nil)
  @exposed_class = value.constantize if value.present?
  return @exposed_class if @exposed_class.present?

  return ancestors[1].exposed_class unless ancestors[1] == Exposant::Base || ancestors[1].exposant_base?

  klass_name = name.gsub(/#{type_name}$/, '')

  klass_name.constantize
end

.human_attribute_name(*args) ⇒ Object



24
25
26
# File 'lib/exposant/base.rb', line 24

def self.human_attribute_name(*args)
  exposed_class.human_attribute_name(*args)
end

.klassObject



28
29
30
# File 'lib/exposant/base.rb', line 28

def self.klass
  @klass ||= exposed_class
end

.method_missing(name, *args, &block) ⇒ Object



20
21
22
# File 'lib/exposant/base.rb', line 20

def self.method_missing(name, *args, &block)
  klass.send(name, *args, &block)
end

.parent_exposantObject



86
87
88
89
90
# File 'lib/exposant/base.rb', line 86

def self.parent_exposant
  return ancestors[1].parent_exposant unless ancestors[1] == Exposant::Base

  name.constantize
end

.respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/exposant/base.rb', line 16

def self.respond_to_missing?(name, *)
  klass.respond_to?(name) || super
end

.type_nameObject



56
57
58
# File 'lib/exposant/base.rb', line 56

def self.type_name
  exposant_type.to_s.camelcase
end

Instance Method Details

#to_modelObject



12
13
14
# File 'lib/exposant/base.rb', line 12

def to_model
  __getobj__
end