Module: Patriot::Command::CommandMacro

Included in:
Base
Defined in:
lib/patriot/command/command_macro.rb

Overview

macros to be used for implmentation of command classes

Instance Method Summary collapse

Instance Method Details

#add_dsl_function(command_module) ⇒ Object

add module methods to DSL methods

Parameters:

  • command_module (Module)

    module defines methods for the use in DSL



8
9
10
# File 'lib/patriot/command/command_macro.rb', line 8

def add_dsl_function(command_module)
  class_eval{ include command_module } 
end

#command_attr(*attrs) {|command, attribute, passed| ... } ⇒ Object

declare command attributes to be able to use in DSL

Parameters:

  • attrs (String|Hash)

    attribute names or a Hash from attribute name to its default value

Yields:

  • block to preprocess attribute values

Yield Parameters:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/patriot/command/command_macro.rb', line 31

def command_attr(*attrs, &blk)
  @command_attrs = {} if @command_attrs.nil?
  default_values = {} 
  if attrs.size == 1 && attrs[0].is_a?(Hash)
    default_values = attrs[0] 
    attrs = default_values.keys
  end
  attrs.each do |a|
    raise "a reserved word #{a} is used as parameter name" if Patriot::Command::COMMAND_CLASS_KEY == a
    raise "#{a} is already defined" if self.instance_methods.include?(a)
    @command_attrs[a] = default_values[a] 
    define_method(a) do |*args|
      raise "illegal size of arguments (#{a} with #{args.inspect})" unless args.size == 1
      val = args[0]
      if block_given?
        yield(self, a, val) 
      else
        self.param a.to_s => val
      end
    end
    attr_writer a
  end
  return attrs
end

#command_attrsHash

Returns a Hash from attribute name to its value.

Returns:

  • (Hash)

    a Hash from attribute name to its value



81
82
83
84
85
86
87
88
# File 'lib/patriot/command/command_macro.rb', line 81

def command_attrs
  super_attrs = {}
  if self.superclass.respond_to?(:command_attrs)
    super_attrs = self.superclass.command_attrs
  end
  return super_attrs if @command_attrs.nil?
  return super_attrs.merge(@command_attrs)
end

#declare_command_name(mth_name, parent_cls = Patriot::Command::CommandGroup, cmd_cls = self) ⇒ Object

declare DSL method name for defining the command

Parameters:

  • mth_name (String)

    the DSL method name

  • parent_cls (Class) (defaults to: Patriot::Command::CommandGroup)

    parent command to which the new command is added

  • cmd_cls (Class) (defaults to: self)


16
17
18
19
20
21
22
23
# File 'lib/patriot/command/command_macro.rb', line 16

def declare_command_name(mth_name, parent_cls=Patriot::Command::CommandGroup, cmd_cls=self)
  parent_cls.class_eval do
    define_method(mth_name) do |&cmd_def|
      cmd = new_command(cmd_cls, &cmd_def)
      add_subcommand(cmd)
    end
  end
end

#private_command_attr(*attrs) ⇒ Object

declare command attributes for only internal use

Parameters:

  • attrs (String)

    attribute name



58
59
60
61
62
# File 'lib/patriot/command/command_macro.rb', line 58

def private_command_attr(*attrs)
  command_attr(*attrs) do |cmd, attr_name, attr_val|
    raise "only internal call is expected for #{attr_name}"
  end
end

#serde_attrsArray

Returns a array of attribute names which should be inclued in Job.

Returns:

  • (Array)

    a array of attribute names which should be inclued in Job



101
102
103
# File 'lib/patriot/command/command_macro.rb', line 101

def serde_attrs
  return command_attrs.keys - volatile_attrs
end

#validate_attr(*attrs) { ... } ⇒ Object

validate attriubte value

Parameters:

  • attrs

    attribute names to be validated

Yields:

  • logic which validates the value

Yield Returns:

  • (Boolean)

    true if the value is valid, otherwise false



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/patriot/command/command_macro.rb', line 109

def validate_attr(*attrs, &blk)
  raise "validation logic not given" unless block_given?
  unless blk.arity == 3
    raise "validation logic arguments should be (cmd, attr_name, attr_val)" 
  end
  @validation_logics = {} if @validation_logics.nil?
  attrs.each do |a|
    raise "#{a} is not command attr" unless command_attrs.include?(a)
    @validation_logics[a] = [] unless @validation_logics.has_key?(a)
    @validation_logics[a] << blk
  end
end

#validate_existence(*attrs) ⇒ Object

check whether the attribute value is not nil

See Also:

  • {validate_attribute_value}


124
125
126
127
128
# File 'lib/patriot/command/command_macro.rb', line 124

def validate_existence(*attrs)
  validate_attr(*attrs) do |cmd, a, v|
    !v.nil?
  end
end

#validation_logicsHash

Returns a hash from attribute name to validation logic (Process).

Returns:

  • (Hash)

    a hash from attribute name to validation logic (Process)



131
132
133
134
135
136
137
138
# File 'lib/patriot/command/command_macro.rb', line 131

def validation_logics
  super_logics = {}
  if self.superclass.respond_to?(:volatile_attrs)
    super_logics = self.superclass.validation_logics 
  end
  return super_logics if @validation_logics.nil?
  return super_logics.merge(@validation_logics)
end

#volatile_attr(*attrs) {|command, attribute, passed| ... } ⇒ Object

declare command attributes to be able to use in DSL values of thes attributes are supposed to be used in Base#do_configure and would not be serialized

Parameters:

  • attrs (String|Hash)

    attribute names or a Hash from attribute name to its default value

Yields:

  • block to preprocess attribute values

Yield Parameters:



72
73
74
75
76
77
78
# File 'lib/patriot/command/command_macro.rb', line 72

def volatile_attr(*attrs, &blk)
  @volatile_attrs = [] if @volatile_attrs.nil?
  attrs = command_attr(*attrs, &blk)
  attrs.each do |a|
    @volatile_attrs << a
  end
end

#volatile_attrsArray

Returns a list of volatile attribute names.

Returns:

  • (Array)

    a list of volatile attribute names



91
92
93
94
95
96
97
98
# File 'lib/patriot/command/command_macro.rb', line 91

def volatile_attrs
  super_attrs = []
  if self.superclass.respond_to?(:volatile_attrs)
    super_attrs = self.superclass.volatile_attrs 
  end
  return super_attrs if @volatile_attrs.nil?
  return @volatile_attrs | super_attrs
end