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, Composition, 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



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

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

.process_inline!(mod, definition) ⇒ Object

DISCUSS: this method might disappear or change pretty soon.



97
98
# File 'lib/disposable/twin.rb', line 97

def self.process_inline!(mod, definition)
end

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

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



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

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

  representer_class.property(name, options, &block).tap do |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) } # TODO: this is more like prototyping.
    end
    include mod

    # property -> build_inline(representable_attrs.features)
    if definition[:extend]
      nested_twin = definition[:extend].evaluate(nil)
      process_inline!(nested_twin, definition)
      # DISCUSS: could we use build_inline's api here to inject the name feature?

      definition.merge!(:twin => nested_twin)
    end
  end
end

.register_feature(mod) ⇒ Object



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

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"]


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

def schema
  self.class.representer_class
end