Module: StronglyTyped::Model

Defined in:
lib/strongly_typed/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
9
10
11
# File 'lib/strongly_typed/model.rb', line 6

def self.included(klass)
  klass.class_eval do
    extend StronglyTyped::Attributes
    include StronglyTyped::Coercible
  end
end

Instance Method Details

#initialize(hsh) ⇒ Object #initialize(values) ⇒ Object

Entity constructor delegator

Examples:

class Person
  include StronglyTyped::Model

  attribute :id, Integer
  attribute :slug, String
end

Person.new(id: 1, slug: 'elgalu')
#=> #<Person:0x00c98 @id=1, @slug="elgalu">
leo.id   #=> 1
leo.slug #=> "elgalu"
class Person
  include StronglyTyped::Model

  attribute :id, Integer
  attribute :slug, String
end

Person.new(1, 'elgalu')
#=> #<Person:0x00c99 @id=1, @slug="elgalu">
leo.id   #=> 1
leo.slug #=> "elgalu"

Overloads:

  • #initialize(hsh) ⇒ Object

    Accepts key values from a hash

  • #initialize(values) ⇒ Object

    Accepts values from ordered arguments

Raises:

  • (ArgumentError)
  • (NameError)

    if tries to assign a non-existing member

  • (ArgumentError)

    if the arity doesn’t match



27
28
29
30
31
32
33
34
35
# File 'lib/strongly_typed/model.rb', line 27

def initialize(*args)
  raise ArgumentError, "Need arguments to build your tiny model" if args.empty?

  if args.size == 1 && args.first.kind_of?(Hash)
    initialize_from_hash(args)
  else
    initialize_from_ordered_args(args)
  end
end