class Disposable::Twin

Constants

INVALID_PROPERTY_NAMES

Public Class Methods

collection(name, options={}, &block) click to toggle source
# File lib/disposable/twin.rb, line 56
def collection(name, options={}, &block)
  property(name, options.merge(collection: true), &block)
end
default_nested_class() click to toggle source
# File lib/disposable/twin.rb, line 32
def default_nested_class
  Twin
end
definition_class() click to toggle source
# File lib/disposable/twin.rb, line 16
def self.definition_class
  Definition
end
from_collection(collection) click to toggle source

TODO: remove.

# File lib/disposable/twin.rb, line 64
def from_collection(collection)
  collection.collect { |model| new(model) }
end
inherited(subclass) click to toggle source
# File lib/disposable/twin.rb, line 20
def self.inherited(subclass)
  # no super here!
  heritage.(subclass) do |cfg|
    cfg[:args].last.merge!(_inherited: true) if cfg[:method] == :property
  end
end
property(name, options={}, &block) click to toggle source

TODO: move to Declarative, as in Representable and Reform.

Calls superclass method
# File lib/disposable/twin.rb, line 37
def property(name, options={}, &block)
  if INVALID_PROPERTY_NAMES.include?(name)
    raise InvalidPropertyNameError.new("#{name} is used internally and cannot be used as property name")
  end

  options[:private_name] ||= options.delete(:from) || name
  is_inherited = options.delete(:_inherited)

  if options.delete(:virtual)
    options[:writeable] = options[:readable] = false
  end

  options[:nested] = options.delete(:twin) if options[:twin]

  super(name, options, &block).tap do |definition| # super is Declarative::Schema::property.
    create_accessors(name, definition) unless is_inherited
  end
end

Private Class Methods

create_accessors(name, definition) click to toggle source
# File lib/disposable/twin.rb, line 70
def create_accessors(name, definition)
  mod = Module.new do
    define_method(name)       { @fields[name.to_s] }
    # define_method(name)       { read_property(name) }
    define_method("#{name}=") { |value| write_property(name, value, definition) }
  end
  include mod
end

Public Instance Methods

schema() click to toggle source
# File lib/disposable/twin.rb, line 27
def schema
  Definition::Each.new(self.class.definitions) # TODO: change this interface.
end