Class: ActiveMappers::Base

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

Constant Summary collapse

@@renderers =
{}

Class Method Summary collapse

Class Method Details

.acts_as_polymorph(**options) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/active_mappers.rb', line 68

def self.acts_as_polymorph(**options)
  each do |resource|
    mapper = KeyTransformer.resource_to_mapper(resource, self)
    mapper.with(resource, default_options.merge(options))
  rescue NameError
    raise NotImplementedError, 'No mapper found for this type of resource'
  end
end

.all(collection, context = nil) ⇒ Object



117
118
119
# File 'lib/active_mappers.rb', line 117

def self.all(collection, context = nil)
  collection.map { |el| one(el, context) }.compact
end

.attributes(*params) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/active_mappers.rb', line 22

def self.attributes(*params)
  each do |resource|
    h = {}
    params.each do |param|
      h[param] = resource.try(param)
    end
    h
  end
end

.default_optionsObject



131
132
133
# File 'lib/active_mappers.rb', line 131

def self.default_options
  { rootless: true, fallback_on_missing_scope: true }
end

.delegate(*params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_mappers.rb', line 32

def self.delegate(*params)
  delegator = params.last[:to]
  params.pop
  each do |resource|
    h = {}
    params.each do |param|
      h[param] = delegator.to_s.split('.').inject(resource, :try).try(param)
    end
    h
  end
end

.each(&block) ⇒ Object



77
78
79
# File 'lib/active_mappers.rb', line 77

def self.each(&block)
  @@renderers[name] = (@@renderers[name] || []) << block
end

.evaluate_scopes(args, options) ⇒ Object



92
93
94
95
# File 'lib/active_mappers.rb', line 92

def self.evaluate_scopes(args, options)
  class_to_call = "::#{name}Scope#{options[:scope].capitalize}".constantize rescue (options[:fallback_on_missing_scope] ? self : raise("ActiveMappers [#{name}] No scope named #{options[:scope]} found"))
  return class_to_call.with(args, options.except(:scope))
end

.inherited(subclass) ⇒ Object



18
19
20
# File 'lib/active_mappers.rb', line 18

def self.inherited(subclass)
  Handlers::Inheritance.new(subclass, self).handle
end

.one(resource, context = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/active_mappers.rb', line 121

def self.one(resource, context = nil)
  return nil unless resource
  return {} if @@renderers[name].nil? # Mapper is empty
  renderers = @@renderers[name].map do |renderer|
    renderer.call(resource, context)
  end.reduce(&:merge)

  KeyTransformer.format_keys(renderers)
end

.polymorphic(key, **options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/active_mappers.rb', line 57

def self.polymorphic(key, **options)
  each do |resource|
    if polymorphic_resource = resource.send("#{key}_type")
      resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{polymorphic_resource}Mapper".constantize
      { key => resource_mapper.with(resource.send(key), default_options.merge(options)) }
    else
      {}
    end
  end
end

.relation(key, mapper = nil, **options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_mappers.rb', line 44

def self.relation(key, mapper = nil, **options)
  path = options[:optional_path] || key
  each do |resource|
    relation_class_name = resource.class&.reflect_on_association(options[:optional_path] || key)&.class_name
    raise "undefined relation : #{key.to_s}" if (mapper.nil? && relation_class_name.nil?)

    mapper_to_use = mapper || KeyTransformer.resource_class_to_mapper(relation_class_name, self)
    raise "'#{mapper_to_use.name}' should be a mapper" unless mapper_to_use.ancestors.map(&:to_s).include?("ActiveMappers::Base")

    { key => mapper_to_use.with(path.to_s.split('.').inject(resource, :try), default_options.merge(options)) }
  end
end

.render_with_root(args, options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/active_mappers.rb', line 106

def self.render_with_root(args, options = {})
  resource_name = options[:root]
  resource_name ||= KeyTransformer.apply_on(self.name)

  if args.respond_to?(:each)
    { resource_name.to_s.pluralize.to_sym => all(args, options[:context]) }
  else
    { resource_name.to_s.singularize.to_sym => one(args, options[:context]) }
  end
end

.scope(*params, &block) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/active_mappers.rb', line 97

def self.scope(*params, &block)
  raise "ActiveMappers [#{name}] scope must be a block" if block.nil? || !block.respond_to?(:call)

  params.each do |param|
    block_content = Ruby2Ruby.new.process(RubyParser.new.process(block.source).to_a.last)
    eval("class ::#{name}Scope#{param.capitalize} < ::#{name} ; #{block_content}; end")
  end
end

.with(args, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/active_mappers.rb', line 81

def self.with(args, options = {})
  return evaluate_scopes(args, options) unless options[:scope].nil?

  response = if options[:rootless]
    args.respond_to?(:each) ? all(args, options[:context]) : one(args, options[:context])
  else
    render_with_root(args, options)
  end
  response
end