Class: ActiveMappers::Base
- Inherits:
-
Object
- Object
- ActiveMappers::Base
- Defined in:
- lib/active_mappers.rb
Constant Summary collapse
- @@renderers =
{}
- @@inheritance_column =
{}
Class Method Summary collapse
- .acts_as_polymorph(**options) ⇒ Object
- .all(collection, options = {}) ⇒ Object
- .attributes(*params) ⇒ Object
- .default_options ⇒ Object
- .delegate(*params) ⇒ Object
- .each(&block) ⇒ Object
- .evaluate_scopes(args, options) ⇒ Object
- .inheritance_column(val) ⇒ Object
- .inherited(subclass) ⇒ Object
- .one(resource, options = {}) ⇒ Object
- .polymorphic(key, **options) ⇒ Object
- .relation(key, mapper = nil, **options) ⇒ Object
- .render_with_root(args, options = {}) ⇒ Object
- .scope(*params, &block) ⇒ Object
- .with(args, options = {}) ⇒ Object
Class Method Details
.acts_as_polymorph(**options) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/active_mappers.rb', line 78 def self.acts_as_polymorph(**) each do |resource| mapper = KeyTransformer.resource_to_mapper(resource, self) mapper.with(resource, .merge()) rescue NameError raise NotImplementedError, 'No mapper found for this type of resource' end end |
.all(collection, options = {}) ⇒ Object
143 144 145 |
# File 'lib/active_mappers.rb', line 143 def self.all(collection, = {}) collection.map { |el| one(el, ) }.compact end |
.attributes(*params) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/active_mappers.rb', line 27 def self.attributes(*params) each do |resource| h = {} params.each do |param| h[param] = resource.try(param) end h end end |
.default_options ⇒ Object
179 180 181 |
# File 'lib/active_mappers.rb', line 179 def self. { rootless: true, fallback_on_missing_scope: true } end |
.delegate(*params) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_mappers.rb', line 37 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
87 88 89 90 |
# File 'lib/active_mappers.rb', line 87 def self.each(&block) # puts "[l. #{__LINE__}] [#{name}] each" @@renderers[name] = (@@renderers[name] || []) << block end |
.evaluate_scopes(args, options) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/active_mappers.rb', line 105 def self.evaluate_scopes(args, ) # puts "[l. #{__LINE__}] [#{name}] evaluate_scopes #{options}" class_to_call = begin "::#{name}Scope#{options[:scope].capitalize}".constantize rescue if [:fallback_class] [:fallback_class] elsif [:fallback_on_missing_scope] .delete :scope self else raise("ActiveMappers [#{name}] No scope named #{options[:scope]} found") end end # puts "[l.#{__LINE__}] evaluate_scopes class_to_call -> #{class_to_call}" return class_to_call.with(args, .merge(initial_mapper: self)) end |
.inheritance_column(val) ⇒ Object
23 24 25 |
# File 'lib/active_mappers.rb', line 23 def self.inheritance_column(val) @@inheritance_column[name] = val end |
.inherited(subclass) ⇒ Object
19 20 21 |
# File 'lib/active_mappers.rb', line 19 def self.inherited(subclass) Handlers::Inheritance.new(subclass, self).handle end |
.one(resource, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_mappers.rb', line 147 def self.one(resource, = {}) # puts "[l.#{__LINE__}] [#{name}] ONE - options: #{options.inspect} - inheritance_column: #{@@inheritance_column[name]}" return nil unless resource if @@inheritance_column[name] && ![:fallback_class] main_mapper = KeyTransformer.resource_to_mapper(resource, self) # puts "[l.#{__LINE__}] [#{name}] ONE - main_mapper #{main_mapper}" mapper = [:scope] ? (KeyTransformer.resource_to_mapper(resource, self, [:scope]) rescue main_mapper) : main_mapper # puts "[l.#{__LINE__}] [#{name}] ONE - mapper #{mapper}" if name != mapper&.name return mapper.with(resource, .merge(rootless: true, fallback_class: [:initial_mapper])) end end return {} if @@renderers[name].nil? # Mapper is empty # base_mapper = name.rpartition('::').first # renderers = ancestors.select { |it| it.name.start_with?(base_mapper) }.map do |ancestor| # puts "---> #{ancestor.name}" # @@renderers[ancestor.name].map do |renderer| # renderer.call(resource, options[:context]) # end.reduce(&:merge) # end.reduce(&:merge) renderers = @@renderers[name].map do |renderer| renderer.call(resource, [:context]) end.reduce(&:merge) KeyTransformer.format_keys(renderers) end |
.polymorphic(key, **options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_mappers.rb', line 66 def self.polymorphic(key, **) each do |resource, context| [:context] = context if polymorphic_resource = resource.send("#{key}_type") resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{polymorphic_resource}Mapper".constantize { key => resource_mapper.with(resource.send(key), .merge()) } else {} end end end |
.relation(key, mapper = nil, **options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_mappers.rb', line 49 def self.relation(key, mapper = nil, **) path = [:optional_path] || key each do |resource| mapper_to_use = if mapper mapper else relation_class_name = resource.class&.reflect_on_association([:optional_path] || key)&.class_name raise "undefined relation : #{key.to_s}" if (mapper.nil? && relation_class_name.nil?) KeyTransformer.resource_class_to_mapper(relation_class_name.dup, self) end 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), .merge()) } end end |
.render_with_root(args, options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/active_mappers.rb', line 132 def self.render_with_root(args, = {}) resource_name = [: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 |
.scope(*params, &block) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/active_mappers.rb', line 123 def self.scope(*params, &block) # puts "[l.#{__LINE__}] [#{name}] CREATING SCOPE CLASSES (name: #{name} | params: #{params.inspect}) ===> ::#{name}Scope#{params.first.capitalize}" 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
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/active_mappers.rb', line 92 def self.with(args, = {}) # puts "[l. #{__LINE__}] WITH - #{name} - #{options}" if [:scope].present? && !name.include?('Scope') return evaluate_scopes(args, ) end response = if [:rootless] args.respond_to?(:each) ? all(args, ) : one(args, ) else render_with_root(args, ) end response end |