class Yuba::ViewModel

Attributes

_args[R]

Public Class Methods

new(**args) click to toggle source
# File lib/yuba/view_model.rb, line 22
def initialize(**args)
  @_args = args
  validate_arguments
  define_accessors
end
property(name, options = {}) click to toggle source

You can register property to the class. Those registered by property need to be passed as arguments to the `initialize` except when `optional: true` is attached. You get `ArgumentError` if you don't pass `property` to `initialize`. Property is default to private. This means you can use it in internal the instance. If you it as public, use `public: true` option.

property :name, public: true
property :email, optional: true
# File lib/yuba/view_model.rb, line 17
def property(name, options = {})
  self._properties = _properties.merge(name.to_sym => options)
end

Private Instance Methods

define_accessors() click to toggle source
# File lib/yuba/view_model.rb, line 40
def define_accessors
  _args.each do |key, value|
    public_method = _properties[key.to_sym][:public]
    define_singleton_method key do
      value
    end
    self.singleton_class.class_eval { private key.to_sym } unless public_method
  end
end
validate_arguments() click to toggle source
# File lib/yuba/view_model.rb, line 32
def validate_arguments
  _args.each_key do |key|
    if !_properties.has_key?(key.to_sym) && !_properties.dig(key.to_sym, :optional)
      raise ArgumentError, "missing 'property :#{key}' in #{self.class.name} class"
    end
  end
end