Class: Legatus::Directive
- Inherits:
-
Object
- Object
- Legatus::Directive
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/legatus/directive.rb
Class Attribute Summary collapse
-
.callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
.models ⇒ Object
readonly
Returns the value of attribute models.
-
.properties ⇒ Object
readonly
Returns the value of attribute properties.
-
.transactions ⇒ Object
readonly
Returns the value of attribute transactions.
-
.validations ⇒ Object
readonly
Returns the value of attribute validations.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
Class Method Summary collapse
- .callback ⇒ Object
- .chain(obj, invocations) ⇒ Object
- .model(mname, &block) ⇒ Object
- .permit(attributes, subschema = nil) ⇒ Object
- .props ⇒ Object
- .transaction(&block) ⇒ Object
- .validate(*models) ⇒ Object
Instance Method Summary collapse
- #clean ⇒ Object
- #execute ⇒ Object
- #extract(*props) ⇒ Object
-
#initialize(params) ⇒ Directive
constructor
A new instance of Directive.
- #invalid? ⇒ Boolean
- #load ⇒ Object
- #persist ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(params) ⇒ Directive
Returns a new instance of Directive.
71 72 73 74 75 76 77 78 79 |
# File 'lib/legatus/directive.rb', line 71 def initialize(params) @params = params @errors = {} @props = {} self.class.properties.each do |pname, chain| @props[pname] = chain.apply(params) end end |
Class Attribute Details
.callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
9 10 11 |
# File 'lib/legatus/directive.rb', line 9 def callbacks @callbacks end |
.models ⇒ Object (readonly)
Returns the value of attribute models.
9 10 11 |
# File 'lib/legatus/directive.rb', line 9 def models @models end |
.properties ⇒ Object (readonly)
Returns the value of attribute properties.
9 10 11 |
# File 'lib/legatus/directive.rb', line 9 def properties @properties end |
.transactions ⇒ Object (readonly)
Returns the value of attribute transactions.
9 10 11 |
# File 'lib/legatus/directive.rb', line 9 def transactions @transactions end |
.validations ⇒ Object (readonly)
Returns the value of attribute validations.
9 10 11 |
# File 'lib/legatus/directive.rb', line 9 def validations @validations end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
69 70 71 |
# File 'lib/legatus/directive.rb', line 69 def errors @errors end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
69 70 71 |
# File 'lib/legatus/directive.rb', line 69 def params @params end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
69 70 71 |
# File 'lib/legatus/directive.rb', line 69 def props @props end |
Class Method Details
.callback ⇒ Object
63 64 65 66 |
# File 'lib/legatus/directive.rb', line 63 def callback @callbacks ||= {} @callbacks.merge!(yield) end |
.chain(obj, invocations) ⇒ Object
24 25 26 |
# File 'lib/legatus/directive.rb', line 24 def chain(obj, invocations) return Chain.new(invocations).apply(obj) end |
.model(mname, &block) ⇒ Object
48 49 50 51 |
# File 'lib/legatus/directive.rb', line 48 def model(mname, &block) @models ||= {} @models[mname] = block end |
.permit(attributes, subschema = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/legatus/directive.rb', line 28 def permit(attributes, subschema=nil) lambda do |parent| result = parent.permit(attributes) return result if subschema.nil? result.tap do |whitelisted| subschema.each do |key, allowed| child = parent[key] next if child.nil? if child.is_a?(Array) whitelisted[:"#{key}_attributes"] = child.map { |c| c.permit(allowed) } else whitelisted[:"#{key}_attributes"] = child.permit(allowed) end end end end end |
.props ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/legatus/directive.rb', line 15 def props schema = yield @properties = {} schema.each do |key, invocations| @properties[key] = Chain.new(invocations) end end |
.transaction(&block) ⇒ Object
58 59 60 61 |
# File 'lib/legatus/directive.rb', line 58 def transaction(&block) @transactions ||= [] @transactions << block end |
.validate(*models) ⇒ Object
53 54 55 56 |
# File 'lib/legatus/directive.rb', line 53 def validate(*models) @validations ||= [] @validations.concat(models) end |
Instance Method Details
#clean ⇒ Object
93 94 95 |
# File 'lib/legatus/directive.rb', line 93 def clean self.reqs(self.props, self.props.keys) end |
#execute ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/legatus/directive.rb', line 127 def execute return ( self.valid? and self.executed?(:clean) and self.valid? and self.executed?(:load) and self.valid? and self.executed?(:validate) and self.valid? and self.executed?(:persist) ) end |
#extract(*props) ⇒ Object
89 90 91 |
# File 'lib/legatus/directive.rb', line 89 def extract(*props) props.map { |prop| self.send(prop) } end |
#invalid? ⇒ Boolean
85 86 87 |
# File 'lib/legatus/directive.rb', line 85 def invalid? return !@errors.empty? end |
#load ⇒ Object
97 98 99 100 101 |
# File 'lib/legatus/directive.rb', line 97 def load self.valid? and self.class.models.each do |mname, loader| self.send(:"#{mname}=", Flexcon.dispatch(self, loader)) end end |
#persist ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/legatus/directive.rb', line 117 def persist return nil if self.invalid? self.class.transactions.each do |handler| uow = UnitOfWork.new handler.call(uow, self) uow.commit end if self.class.transactions.present? end |
#valid? ⇒ Boolean
81 82 83 |
# File 'lib/legatus/directive.rb', line 81 def valid? return @errors.empty? end |
#validate ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/legatus/directive.rb', line 103 def validate return ( self.valid? and self.class.validations.each do |mname| self.check(mname => self.send(mname)) end ) if self.class.validations.present? return ( self.valid? and self.class.models.each do |mname, loader| self.check(mname => self.send(mname)) end ) if self.class.models.present? end |