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_polymorphObject



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

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

.all(collection) ⇒ Object



80
81
82
# File 'lib/active_mappers.rb', line 80

def self.all(collection)
  collection.map { |el| one(el) }
end

.attributes(*params) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/active_mappers.rb', line 11

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

.delegate(*params) ⇒ Object



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

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



57
58
59
# File 'lib/active_mappers.rb', line 57

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

.one(resource) ⇒ Object



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

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

  KeyTransformer.format_keys(renderers)
end

.polymorphic(key) ⇒ Object



41
42
43
44
45
46
# File 'lib/active_mappers.rb', line 41

def self.polymorphic(key)
  each do |resource|
    resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{resource.send("#{key}_type")}Mapper".constantize
    { key => resource_mapper.with(resource.send(key), rootless: true) }
  end
end

.relation(key, mapper = nil, optional_path = nil) ⇒ Object



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

def self.relation(key, mapper = nil, optional_path = nil)
  path = optional_path || key
  each do |resource|
    mapper_to_use = mapper || KeyTransformer.resource_to_mapper(resource.send(key), self)
    { key => mapper_to_use.with(path.to_s.split('.').inject(resource, :try), rootless: true) }
  end
end

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



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

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) }
  else
    { resource_name.to_s.singularize.to_sym => one(args) }
  end
end

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



61
62
63
64
65
66
67
# File 'lib/active_mappers.rb', line 61

def self.with(args, options = {})
  if options[:rootless]
    args.respond_to?(:each) ? all(args) : one(args)
  else
    render_with_root(args, options)
  end
end