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



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

def self.acts_as_polymorph
  each do |resource|
    mapper = "::#{resource.class}Mapper".constantize
    mapper.with(resource, rootless: true)
  rescue NameError
    raise NotImplementedError, 'No mapper found for this type of resource'
  end
end

.all(collection) ⇒ Object



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

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



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

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

.one(resource) ⇒ Object



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

def self.one(resource)
  renderers = @@renderers[name].map do |renderer|
    renderer.call(resource)
  end.reduce(&:merge)

  KeyTransformer.format_keys(renderers)
end

.polymorphic(key) ⇒ Object



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

def self.polymorphic(key)
  each do |resource|
    resource_mapper = "::#{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
40
# File 'lib/active_mappers.rb', line 33

def self.relation(key, mapper = nil, optional_path = nil)
  path = optional_path || key

  each do |resource|
    mapper ||= "::#{resource.send(key).class.name}Mapper".constantize
    { key => mapper.with(path.to_s.split('.').inject(resource, :try), rootless: true) }
  end
end

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



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

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



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

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