lib/jsonapionify/api/attribute.rb in jsonapionify-0.0.1.pre vs lib/jsonapionify/api/attribute.rb in jsonapionify-0.9.0

- old
+ new

@@ -1,10 +1,14 @@ +require 'unstrict_proc' + module JSONAPIonify::Api class Attribute + using UnstrictProc attr_reader :name, :type, :description, :read, :write, :required def initialize(name, type, description, read: true, write: true, required: false, example: nil) + raise ArgumentError, 'required attributes must be writable' if required && !write unless type.is_a? JSONAPIonify::Types::BaseType raise TypeError, "#{type} is not a valid JSON type" end @name = name @type = type @@ -18,10 +22,17 @@ def ==(other) self.class == other.class && self.name == other.name end + def options_json + { + name: name, + required: required + } + end + def required? !!@required end def optional? @@ -34,13 +45,13 @@ def write? !!@write end - def example + def example(*args) case @example when Proc - type.dump @example.call + type.dump @example.unstrict.call(*args) when nil type.dump type.sample(name) else type.dump @example end