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
-
#add_dsl_function(command_module) ⇒ Object
add module methods to DSL methods.
-
#command_attr(*attrs) {|command, attribute, passed| ... } ⇒ Object
declare command attributes to be able to use in DSL.
-
#command_attrs ⇒ Hash
A Hash from attribute name to its value.
-
#declare_command_name(mth_name, parent_cls = Patriot::Command::CommandGroup, cmd_cls = self) ⇒ Object
declare DSL method name for defining the command.
-
#private_command_attr(*attrs) ⇒ Object
declare command attributes for only internal use.
-
#serde_attrs ⇒ Array
A array of attribute names which should be inclued in Job.
-
#validate_attr(*attrs) { ... } ⇒ Object
validate attriubte value.
-
#validate_existence(*attrs) ⇒ Object
check whether the attribute value is not nil.
-
#validation_logics ⇒ Hash
A hash from attribute name to validation logic (Process).
-
#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.
-
#volatile_attrs ⇒ Array
A list of volatile attribute names.
Instance Method Details
#add_dsl_function(command_module) ⇒ Object
add module methods to DSL methods
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
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_attrs ⇒ Hash
Returns 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
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
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_attrs ⇒ Array
Returns 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
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
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_logics ⇒ Hash
Returns 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
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_attrs ⇒ Array
Returns 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 |