Class: Mio::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/mio/model.rb,
lib/mio/errors.rb,
lib/mio/model/s3.rb,
lib/mio/model/rename.rb,
lib/mio/model/extract.rb,
lib/mio/model/variant.rb,
lib/mio/model/workflow.rb,
lib/mio/model/hotfolder.rb,
lib/mio/model/groovy_script.rb,
lib/mio/model/import_action.rb,
lib/mio/model/workflow/node.rb,
lib/mio/model/account_property.rb,
lib/mio/model/extract_resource.rb,
lib/mio/model/message_template.rb,
lib/mio/model/groovy_script_wait.rb,
lib/mio/model/wait_groovy_script.rb,
lib/mio/model/add_to_group_action.rb,
lib/mio/model/metadata_definition.rb,
lib/mio/model/workflow/transition.rb,
lib/mio/model/email_message_action.rb,
lib/mio/model/groovy_script_decision.rb,
lib/mio/model/launch_workflow_action.rb,
lib/mio/model/metadatadefinition/option.rb,
lib/mio/model/metadatadefinition/definition.rb,
lib/mio/model/launchworkflow/launch_workflow.rb,
lib/mio/model/place_holder_group_asset_action.rb,
lib/mio/model/launchworkflow/workflow_variable.rb

Defined Under Namespace

Classes: AccountProperty, AddToGroupAction, DataTypeError, DataValueError, DateVariableInvalid, EmailMessageAction, EmptyField, Extract, ExtractResource, GroovyScript, GroovyScriptDecision, GroovyScriptWait, Hotfolder, ImportAction, LaunchWorkflowAction, MessageTemplate, MetadataDefinition, MissingField, NoSuchField, NoSuchResource, ObjectVariableInvalid, PlaceHolderGroupAssetAction, Rename, S3, Variant, WaitGroovyScript, Workflow

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, args) ⇒ Model

Returns a new instance of Model.



40
41
42
43
44
# File 'lib/mio/model.rb', line 40

def initialize client, args
  @client = client
  @args   = args
  @search = Mio::Search.new @client
end

Class Attribute Details

.fieldsObject

Returns the value of attribute fields.



5
6
7
# File 'lib/mio/model.rb', line 5

def fields
  @fields
end

.resource_nameObject

Returns the value of attribute resource_name.



5
6
7
# File 'lib/mio/model.rb', line 5

def resource_name
  @resource_name
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/mio/model.rb', line 3

def args
  @args
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/mio/model.rb', line 3

def client
  @client
end

#searchObject (readonly)

Returns the value of attribute search.



3
4
5
# File 'lib/mio/model.rb', line 3

def search
  @search
end

Class Method Details

.field(key, type, desc, default = nil, matcher = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/mio/model.rb', line 10

def field key, type, desc, default=nil, matcher=nil
  @fields ||= []
  @fields << {name: key,
              type: type,
              default: default,
              matcher: matcher,
              desc: desc,}

end

.mappingsObject



29
30
31
32
33
34
35
36
37
# File 'lib/mio/model.rb', line 29

def mappings
  m = {}
  ObjectSpace.each_object(Class).each do |k|
    if k < Mio::Model
      m[ k.to_s.split('::').last.downcase ] = k
    end
  end
  m
end

.nested(val = nil) ⇒ Object Also known as: nested?



20
21
22
23
24
25
26
# File 'lib/mio/model.rb', line 20

def nested val=nil
  if val.nil?
    @nested_value || false
  else
    @nested_value = val
  end
end

.set_resource(r) ⇒ Object



6
7
8
# File 'lib/mio/model.rb', line 6

def set_resource r
  @resource_name = r.to_s
end

Instance Method Details

#configureObject



67
68
69
70
71
# File 'lib/mio/model.rb', line 67

def configure
  @client.configure self.class.resource_name,
                    @object['id'],
                    config_hash
end

#createObject



63
64
65
# File 'lib/mio/model.rb', line 63

def create
  @client.create self.class.resource_name, create_hash
end

#goObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mio/model.rb', line 46

def go
  unless look_up
    @object = create
  else
    @object = look_up

    # We can't edit a running resource
    set_start :stop
  end

  configure if self.respond_to? :config_hash
  set_enable
  set_start

  return @object
end

#set_enable(a = nil) ⇒ Object Also known as: disable!, enable!



73
74
75
76
77
78
79
80
81
82
# File 'lib/mio/model.rb', line 73

def set_enable a=nil
  if a.nil?
    action = @args.enable == :true ? 'enable' : 'disable'
  else
    action = a.to_s
  end
  @client.action self.class.resource_name,
                 @object['id'],
                 {action: action}
end

#set_start(a = nil) ⇒ Object Also known as: stop!, start!



86
87
88
89
90
91
92
93
94
95
# File 'lib/mio/model.rb', line 86

def set_start a=nil
  if a.nil?
    action = @args.start == :true ? 'start' : 'stop'
  else
    action = a.to_s
  end
  @client.action self.class.resource_name,
                 @object['id'],
                 {action: action}
end

#validateObject Also known as: valid?



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mio/model.rb', line 99

def validate
  testable = self.args.dup.to_h

  self.class.fields.each do |f|
    unless testable.key? f[:name]
      raise Mio::Model::MissingField, "Missing field #{f[:name]} to #{self}"
    end

    extracted_field = testable.delete f[:name]
    unless extracted_field.is_a? f[:type]
      raise Mio::Model::DataTypeError, "#{f[:name]} should be of type #{f[:type]} for #{self}"
    end

    #unless f[:matcher].nil? or extracted_field.to_s.match(f[:matcher])
    if !f[:matcher].nil? && extracted_field.to_s.match(f[:matcher]).nil?
      raise Mio::Model::DataValueError, "#{self} #{f[:name]} value '#{extracted_field}' does not match #{f[:matcher]}"
    end
  end

  testable.keys.each do |k|
    raise Mio::Model::NoSuchField, "#{k} for #{self}"
  end
  true
end