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
- .format_errors(errors) ⇒ Object
- .has_many(*args) ⇒ Object
- .has_one(*args) ⇒ Object
- .model ⇒ Object
- .model=(value) ⇒ Object
- .scope(name = :all, &block) ⇒ Object
- .scopes ⇒ Object
Instance Method Summary collapse
- #attributes(*args) ⇒ Object
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
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
.format_errors(errors) ⇒ Object
70 71 72 73 74 |
# File 'lib/pinkman/serializer/base.rb', line 70 def self.format_errors errors e = Hash.new errors.each {|k,v| e[k] = [v].flatten} e end |
.has_many(*args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pinkman/serializer/base.rb', line 44 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: @scope, params: @params) end end end end end |
.has_one(*args) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pinkman/serializer/base.rb', line 57 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: @scope, params: @params) end end end end end |
.model ⇒ Object
36 37 38 |
# File 'lib/pinkman/serializer/base.rb', line 36 def self.model @model || (begin eval(self.to_s.sub('Serializer','')) rescue nil end) end |
.model=(value) ⇒ Object
40 41 42 |
# File 'lib/pinkman/serializer/base.rb', line 40 def self.model= value @model = value end |
.scope(name = :all, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/pinkman/serializer/base.rb', line 22 def self.scope name=:all, &block @scopes ||= {} if block_given? @scopes[name.to_sym] = Pinkman::Serializer::Scope.new(serializer: self) yield(@scopes[name.to_sym]) else @scopes[name.to_sym] end end |
.scopes ⇒ Object
32 33 34 |
# File 'lib/pinkman/serializer/base.rb', line 32 def self.scopes @scopes end |
Instance Method Details
#attributes(*args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pinkman/serializer/base.rb', line 76 def attributes *args hash = super(*args) if scope 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] = object.send(attribute) if object.methods.include?(attribute)} pinkmanscope.read.each {|attribute| hash[attribute] = send(attribute) if self.methods.include?(attribute)} hash[:errors] = self.class.format_errors(errors) if errors.present? and errors.any? hash end end |