Class: Disposable::Twin

Inherits:
Object
  • Object
show all
Extended by:
Representable::Feature, Uber::InheritableAttr
Includes:
Accessors, ModelReaders, Option, Setup, ToS
Defined in:
lib/disposable/twin/sync.rb,
lib/disposable.rb,
lib/disposable/twin.rb,
lib/disposable/twin/save.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/representer.rb

Overview

#sync!

1. assign scalars to model (respecting virtual, excluded attributes)
2. call sync! on nested

Note: #sync currently implicitly saves AR objects with collections

Defined Under Namespace

Modules: Accessors, Builder, Changed, Coercion, Composition, Default, Expose, ModelReaders, Option, Persisted, Save, Setup, Struct, Sync, ToS Classes: Collection, Decorator, PropertyProcessor, Schema, Twinner

Instance Attribute Summary

Attributes included from ModelReaders

#mapper, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Option

included

Methods included from ToS

#to_s

Methods included from Setup

#initialize

Class Method Details

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



70
71
72
# File 'lib/disposable/twin.rb', line 70

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

.from_collection(collection) ⇒ Object



74
75
76
# File 'lib/disposable/twin.rb', line 74

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

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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/disposable/twin.rb', line 48

def property(name, options={}, &block)
  options[:private_name] = options.delete(:from) || name

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

  options[:extend] = options[:twin] # e.g. property :album, twin: Album.

  representer_class.property(name, options, &block).tap do |definition|
    create_accessors(name, definition)

    if definition[:extend] and !options[:twin]
      # This will soon be replaced with Declarative's API. # DISCUSS: could we use build_inline's api here to inject the name feature?
      nested_twin = definition[:extend].evaluate(nil)
      process_inline!(nested_twin, definition)

      definition.merge!(twin: nested_twin) # DISCUSS: where do we need this?
    end
  end
end

.register_feature(mod) ⇒ Object



41
42
43
# File 'lib/disposable/twin.rb', line 41

def self.register_feature(mod)
  representer_class.representable_attrs[:features][mod] = true
end

Instance Method Details

#schemaObject

Returns an each’able array of all properties defined in this twin. Allows to filter using

* collection: true
* twin:       true
* scalar:     true
* exclude:    ["title", "email"]


35
36
37
# File 'lib/disposable/twin.rb', line 35

def schema
  self.class.representer_class
end