Class: Pinkman::Serializer::Base
- Inherits:
-
ActiveModel::Serializer
- Object
- ActiveModel::Serializer
- Pinkman::Serializer::Base
- Defined in:
- lib/pinkman/serializer/base.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
- .define_scopes(*args, &block) ⇒ Object
- .format_errors(errors) ⇒ Object
- .has_many(*args) ⇒ Object
- .has_one(*args) ⇒ Object
- .has_scope?(name) ⇒ Boolean
- .model ⇒ Object
- .model=(value) ⇒ Object
-
.reflections ⇒ Object
assocs.
- .scope(name = :public, &block) ⇒ Object
- .scopes ⇒ Object
-
.table_name ⇒ Object
self.root = false.
Instance Method Summary collapse
- #attributes(*args) ⇒ Object
- #get_assoc_scope(assoc, scope) ⇒ Object
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
- #to_h ⇒ Object
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 17 |
# File 'lib/pinkman/serializer/base.rb', line 12 def initialize *args super(*args) @errors = Base.format_errors(args.first.errors.to_h) if args.first.present? and args.first.errors.present? and args.first.errors.any? @params = OpenStruct.new(args[1][:params]) if args.length > 1 and args[1].is_a?(Hash) and args[1][:params] self end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
9 10 11 |
# File 'lib/pinkman/serializer/base.rb', line 9 def errors @errors end |
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/pinkman/serializer/base.rb', line 10 def params @params end |
Class Method Details
.define_scopes(*args, &block) ⇒ Object
24 25 26 27 28 |
# File 'lib/pinkman/serializer/base.rb', line 24 def self.define_scopes *args, &block args.each do |scope_name| self.scope(scope_name, &block) end end |
.format_errors(errors) ⇒ Object
88 89 90 91 92 |
# File 'lib/pinkman/serializer/base.rb', line 88 def self.format_errors errors e = Hash.new errors.each {|k,v| e[k] = [v].flatten} e end |
.has_many(*args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pinkman/serializer/base.rb', line 62 def self.has_many *args args.each do |attribute| self.class_eval do |c| define_method attribute do reflection = object.class.reflections[attribute.to_s] if reflection Pinkman::Serializer::array(object.send(attribute), scope: get_assoc_scope(attribute, @scope), params: @params) end end end end end |
.has_one(*args) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pinkman/serializer/base.rb', line 75 def self.has_one *args args.each do |attribute| self.class_eval do |c| define_method attribute do reflection = object.class.reflections[attribute.to_s] if reflection reflection.klass.serializer.new(object.send(attribute), scope: get_assoc_scope(attribute, @scope), params: @params) end end end end end |
.has_scope?(name) ⇒ Boolean
46 47 48 |
# File 'lib/pinkman/serializer/base.rb', line 46 def self.has_scope?(name) scopes.has_key?(name.to_sym) end |
.model ⇒ Object
54 55 56 |
# File 'lib/pinkman/serializer/base.rb', line 54 def self.model @model || (begin eval(self.to_s.sub('Serializer','')) rescue nil end) end |
.model=(value) ⇒ Object
58 59 60 |
# File 'lib/pinkman/serializer/base.rb', line 58 def self.model= value @model = value end |
.reflections ⇒ Object
assocs
96 97 98 |
# File 'lib/pinkman/serializer/base.rb', line 96 def self.reflections model.reflections end |
.scope(name = :public, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pinkman/serializer/base.rb', line 30 def self.scope name=:public, &block @scopes ||= {} if block_given? @scopes[name.to_sym] = Pinkman::Serializer::Scope.new(serializer: self, name: name.to_s) yield(@scopes[name.to_sym]) else if @scopes[name.to_sym] @scopes[name.to_sym] elsif Pinkman.configuration.scope_fallback and @scopes[Pinkman.configuration.scope_fallback] @scopes[Pinkman.configuration.scope_fallback] else raise(ArgumentError, (Pinkman.configuration.scope_fallback ? ("Scope '#{name}' and fallback scope '#{Pinkman.configuration.scope_fallback}' were not found in #{self.to_s}.") : ("Scope '#{name}' not found in #{self.to_s}.") )) end end end |
.scopes ⇒ Object
50 51 52 |
# File 'lib/pinkman/serializer/base.rb', line 50 def self.scopes @scopes end |
.table_name ⇒ Object
self.root = false
20 21 22 |
# File 'lib/pinkman/serializer/base.rb', line 20 def self.table_name model.table_name end |
Instance Method Details
#attributes(*args) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pinkman/serializer/base.rb', line 109 def attributes *args hash = super(*args) if scope begin pinkmanscope = self.class.scope(scope) include_all = pinkmanscope.read.include?(:all) model = self.class.model model.column_names.each {|attribute| hash[attribute] = object.send(attribute) } if include_all && model && model.methods.include?(:column_names) pinkmanscope.read.each {|attribute| hash[attribute] = send(attribute) if self.methods.include?(attribute)} pinkmanscope.read.each {|attribute| hash[attribute] ||= object.send(attribute) if object.methods.include?(attribute)} pinkmanscope.read_ghost.each {|attribute| begin hash[attribute] ||= object.send(attribute) rescue nil end } hash[:destroyed] = true if object.destroyed? hash[:errors] = self.class.format_errors(errors) if errors.present? and errors.any? rescue nil end hash end end |
#get_assoc_scope(assoc, scope) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/pinkman/serializer/base.rb', line 100 def get_assoc_scope assoc, scope assoc_scopes = begin self.class.scope(scope).assoc_scopes rescue nil end if assoc_scopes.is_a?(Hash) and assoc_scopes[assoc] assoc_scopes[assoc] else scope end end |
#to_h ⇒ Object
129 130 131 |
# File 'lib/pinkman/serializer/base.rb', line 129 def to_h JSON.parse(to_json) end |