Class: Disposable::Twin

Inherits:
Object
  • Object
show all
Extended by:
Declarative::Schema, Property::Unnest
Includes:
Accessors, ModelReaders, Setup
Defined in:
lib/disposable.rb,
lib/disposable/twin.rb,
lib/disposable/twin/save.rb,
lib/disposable/twin/sync.rb,
lib/disposable/twin/setup.rb,
lib/disposable/twin/struct.rb,
lib/disposable/twin/builder.rb,
lib/disposable/twin/collection.rb,
lib/disposable/twin/composition.rb,
lib/disposable/twin/definitions.rb,
lib/disposable/twin/property/hash.rb,
lib/disposable/twin/property/struct.rb

Defined Under Namespace

Modules: Accessors, Builder, Changed, Coercion, Composition, Default, Expose, ModelReaders, Parent, Persisted, Property, Save, Setup, Struct, Sync Classes: Collection, Definition, InvalidPropertyNameError, PropertyProcessor, Twinner

Constant Summary collapse

INVALID_PROPERTY_NAMES =
%i[class].freeze

Instance Attribute Summary

Attributes included from ModelReaders

#mapper, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Property::Unnest

included, unnest

Methods included from Setup

#initialize

Class Method Details

.collection(name, options = {}, &block) ⇒ Object



56
57
58
# File 'lib/disposable/twin.rb', line 56

def collection(name, options={}, &block)
  property(name, options.merge(collection: true), &block)
end

.default_nested_classObject



32
33
34
# File 'lib/disposable/twin.rb', line 32

def default_nested_class
  Twin
end

.definition_classObject



16
17
18
# File 'lib/disposable/twin.rb', line 16

def self.definition_class
  Definition
end

.from_collection(collection) ⇒ Object

TODO: remove.



64
65
66
# File 'lib/disposable/twin.rb', line 64

def from_collection(collection)
  collection.collect { |model| new(model) }
end

.inherited(subclass) ⇒ Object



20
21
22
23
24
25
# 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) ⇒ Object

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Method Details

#schemaObject



27
28
29
# File 'lib/disposable/twin.rb', line 27

def schema
  Definition::Each.new(self.class.definitions) # TODO: change this interface.
end