Class: Module

Inherits:
Object
  • Object
show all
Includes:
Constructable
Defined in:
lib/constructable/core_ext.rb

Constant Summary

Constants included from Constructable

Constructable::AttributeError

Instance Method Summary collapse

Instance Method Details

#constructable(*args) ⇒ Object

Examples:


class Foo
  constructable :bar, :readable => true, :baz, :readable => true, required: true
end

foo = Foo.new(bar: 5)
# raises AttributeError, ':baz is a required attribute'

foo = Foo.new(baz: 7, bar: 5)

foo.bar
# => 5
foo.baz
# => 7

class ProgrammingLanguage
  constructable :paradigms,
    readable: true,
    required: true,
    validate: ->(value) { value.is_a?(Array) }
end

c = ProgrammingLanguage.new(paradigms: :functional)
#  raises AttributeError, ':paradigms did not pass validation'

ruby = ProgrammingLanguage.new(paradigms: [:object_oriented, :functional])
ruby.paradigms
# => [:object_oriented, :functional]

Parameters:

  • args (Array<[Array<Symbol, Hash>]>)

    an array of symbols or arrays: the name of the attribute and it’s configuration



36
37
38
39
40
# File 'lib/constructable/core_ext.rb', line 36

def constructable(*args)
  @constructor ||= Constructor.new(self)
  @constructor.define_attributes(args)
  return nil
end