Class: Legatus::Directive

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/legatus/directive.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.callbacksObject (readonly)

Returns the value of attribute callbacks.



9
10
11
# File 'lib/legatus/directive.rb', line 9

def callbacks
  @callbacks
end

.modelsObject (readonly)

Returns the value of attribute models.



9
10
11
# File 'lib/legatus/directive.rb', line 9

def models
  @models
end

.propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/legatus/directive.rb', line 9

def properties
  @properties
end

.transactionsObject (readonly)

Returns the value of attribute transactions.



9
10
11
# File 'lib/legatus/directive.rb', line 9

def transactions
  @transactions
end

.validationsObject (readonly)

Returns the value of attribute validations.



9
10
11
# File 'lib/legatus/directive.rb', line 9

def validations
  @validations
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



69
70
71
# File 'lib/legatus/directive.rb', line 69

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



69
70
71
# File 'lib/legatus/directive.rb', line 69

def params
  @params
end

#propsObject (readonly)

Returns the value of attribute props.



69
70
71
# File 'lib/legatus/directive.rb', line 69

def props
  @props
end

Class Method Details

.callbackObject



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

.propsObject



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

#cleanObject



93
94
95
# File 'lib/legatus/directive.rb', line 93

def clean
  self.reqs(self.props, self.props.keys)
end

#executeObject



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

Returns:

  • (Boolean)


85
86
87
# File 'lib/legatus/directive.rb', line 85

def invalid?
  return !@errors.empty?
end

#loadObject



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

#persistObject



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

Returns:

  • (Boolean)


81
82
83
# File 'lib/legatus/directive.rb', line 81

def valid?
  return @errors.empty?
end

#validateObject



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