class Primalize::Single
Attributes
object[R]
Public Class Methods
_attributes(**attrs)
click to toggle source
# File lib/primalize/single.rb, line 16 def _attributes **attrs @attributes ||= if self.equal? Primalize::Single {} else superclass._attributes.dup end add_attributes attrs @attributes end
add_attributes(attrs)
click to toggle source
# File lib/primalize/single.rb, line 28 def add_attributes attrs return if attrs.none? @attributes.merge! attrs attrs.each do |attr, type| define_method attr do type.coerce(object.public_send(attr)) end end end
all(*types, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 84 def all *types, &coerce All.new(types, &coerce) end
any(*types, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 80 def any *types, &coerce Any.new(types, &coerce) end
array(*types, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 52 def array *types, &coerce Array.new(types, &coerce) end
attributes(**attrs)
click to toggle source
# File lib/primalize/single.rb, line 12 def attributes **attrs _attributes **attrs end
boolean(&coerce)
click to toggle source
# File lib/primalize/single.rb, line 48 def boolean &coerce enum(true, false, &coerce) end
enum(*values, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 72 def enum *values, &coerce Enum.new(values, &coerce) end
float(&coerce)
click to toggle source
# File lib/primalize/single.rb, line 60 def float &coerce Float.new(&coerce) end
inspect()
click to toggle source
# File lib/primalize/single.rb, line 108 def inspect "#{name}(#{attributes.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ') })" end
integer(&coerce)
click to toggle source
# File lib/primalize/single.rb, line 40 def integer &coerce Integer.new(&coerce) end
match(matcher, &coercion)
click to toggle source
# File lib/primalize/single.rb, line 88 def match matcher, &coercion Match.new(matcher, &coercion) end
new(object)
click to toggle source
# File lib/primalize/single.rb, line 115 def initialize object raise ArgumentError, "#{self.class.inspect} cannot serialize `nil'" if object.nil? @object = object end
number(&coerce)
click to toggle source
# File lib/primalize/single.rb, line 64 def number &coerce Number.new(&coerce) end
object(**types, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 56 def object **types, &coerce Object.new(types, &coerce) end
optional(*types, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 68 def optional *types, &coerce Optional.new(types, &coerce) end
primalize(primalizer, &coerce)
click to toggle source
# File lib/primalize/single.rb, line 92 def primalize primalizer, &coerce Primalizer.new(primalizer, &coerce) end
string(&coerce)
click to toggle source
# File lib/primalize/single.rb, line 44 def string &coerce String.new(&coerce) end
timestamp(&coerce)
click to toggle source
# File lib/primalize/single.rb, line 76 def timestamp &coerce Timestamp.new(&coerce) end
type_mismatch_handler()
click to toggle source
# File lib/primalize/single.rb, line 100 def type_mismatch_handler if @type_mismatch_handler @type_mismatch_handler else superclass.type_mismatch_handler end end
type_mismatch_handler=(handler)
click to toggle source
# File lib/primalize/single.rb, line 96 def type_mismatch_handler= handler @type_mismatch_handler = handler end
Public Instance Methods
call()
click to toggle source
# File lib/primalize/single.rb, line 120 def call self.class._attributes.each_with_object({}) do |(attr, type), hash| value = public_send(attr) hash[attr] = if type === value value else self.class.type_mismatch_handler.call( self.class, attr, type, value, ) end end end
csv_headers()
click to toggle source
# File lib/primalize/single.rb, line 143 def csv_headers self.class._attributes.keys.map(&:to_s) end
to_csv()
click to toggle source
# File lib/primalize/single.rb, line 147 def to_csv hash = call CSV.generate { |csv| csv << hash.values } end
to_json(options=nil)
click to toggle source
CONVERSION
# File lib/primalize/single.rb, line 139 def to_json(options=nil) call.to_json(options) end