module Jat::DSLClassMethods
Public Instance Methods
attribute(name, **opts, &block)
click to toggle source
# File lib/jat.rb, line 92 def attribute(name, **opts, &block) add_attribute(name: name, opts: opts, block: block) end
call()
click to toggle source
# File lib/jat.rb, line 64 def call self end
id(key: nil, &block)
click to toggle source
# File lib/jat.rb, line 84 def id(key: nil, &block) raise Error, "Key and block can't be provided together" if key && block raise Error, 'Key or block must be provided' if !key && !block block ||= proc { |obj| obj.public_send(key) } define_method(:id, &block) end
relationship(name, serializer:, **opts, &block)
click to toggle source
# File lib/jat.rb, line 96 def relationship(name, serializer:, **opts, &block) opts[:serializer] = serializer add_attribute(name: name, opts: opts, block: block) end
to_h(object, context = {})
click to toggle source
# File lib/jat.rb, line 68 def to_h(object, context = {}) new.to_h(object, context) end
to_str(object, context = {})
click to toggle source
# File lib/jat.rb, line 72 def to_str(object, context = {}) new.to_str(object, context) end
type(new_type = nil)
click to toggle source
# File lib/jat.rb, line 76 def type(new_type = nil) return @type || raise(Error, "#{self} has no defined type") unless new_type new_type = new_type.to_sym define_method(:type) { new_type } @type = new_type end
Private Instance Methods
add_attribute(params)
click to toggle source
# File lib/jat.rb, line 103 def add_attribute(params) opts = Opts.new(self, params) Attribute.new(opts).tap do |attribute| attributes << attribute add_method(attribute) clear end end
add_method(attribute)
click to toggle source
# File lib/jat.rb, line 113 def add_method(attribute) block = attribute.block return unless block name = attribute.original_name # Warning-free method redefinition remove_method(name) if method_defined?(name) define_method(name, &block) end