Module: LazyLazer::ClassMethods

Defined in:
lib/lazy_lazer.rb

Overview

The methods to extend the class with.

Instance Method Summary collapse

Instance Method Details

#inherited(klass) ⇒ void

This method returns an undefined value.

Copies parent properties into subclasses.



28
29
30
# File 'lib/lazy_lazer.rb', line 28

def inherited(klass)
  klass.instance_variable_set(:@lazer_metadata, .dup)
end

#lazer_metadataHash<Symbol, Hash>



33
34
35
# File 'lib/lazy_lazer.rb', line 33

def 
  
end

#property(name, *bool_options, **options) ⇒ Symbol

Define a property.

Examples:

class MyModel
  include LazyLazer

  property :id, :required
  property :timestamp, with: ->(i) { Time.at(i) }
  property :created_at, default: ->() { Time.now }
  property :camel_case, from: :camelCase
end

Options Hash (**options):

  • :required (Boolean) — default: false

    whether existence of this property should be checked on model creation

  • :nil (Boolean) — default: false

    shortcut for default: nil

  • :default (Object, Proc)

    the default value to return if not provided

  • :from (Symbol) — default: name

    the key in the source object to get the property from

  • :with (Proc, Symbol, nil)

    an optional transformation to apply to the value



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lazy_lazer.rb', line 58

def property(name, *bool_options, **options)
  bool_options.each_with_object(options) { |sym, hsh| hsh[sym] = true }
  sym_name = name.to_sym
  [sym_name] = .new(
    source_key: options.fetch(:from, sym_name),
    required: !!options[:required],
    runtime_required: !options.key?(:default) && !options[:nil],
    transform: options[:with],
    default: options[:default]
  )
  define_method(sym_name) { read_attribute(sym_name) }
end