Class: Anodator::InputSpec
- Inherits:
-
Object
- Object
- Anodator::InputSpec
- Defined in:
- lib/anodator/input_spec.rb
Constant Summary collapse
- CALCULATION_HOLDER_REGEXP =
/\[\[([^\]]+)\]\]/
Instance Method Summary collapse
- #[](target_expression) ⇒ Object
-
#check_calculation_expression(calculation) ⇒ Object
return all holders specs.
- #clear_source ⇒ Object
-
#initialize(spec_items = []) ⇒ InputSpec
constructor
A new instance of InputSpec.
- #source=(source) ⇒ Object
- #spec_item_at(index) ⇒ Object
- #spec_item_at_by_name(name) ⇒ Object
- #spec_item_at_by_number(number) ⇒ Object
- #spec_item_by_expression(target_expression) ⇒ Object
- #value_at(index) ⇒ Object
- #value_at_by_name(name) ⇒ Object
- #value_at_by_number(number) ⇒ Object
Constructor Details
#initialize(spec_items = []) ⇒ InputSpec
Returns a new instance of InputSpec.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/anodator/input_spec.rb', line 12 def initialize(spec_items = []) @spec_items = [] @source = nil @number_dict = { } @name_dict = { } unless spec_items.is_a? Array raise ArgumentError.new("initialized by Array by Hash(key is :name and :number") end spec_items.each do |spec_item_values| if spec_item_values.keys.include?(:number) && spec_item_values.keys.include?(:name) if spec_item_values.keys.include?(:type) && !spec_item_values[:type].nil? push_spec_items(InputSpecItem.new(spec_item_values[:number], spec_item_values[:name], spec_item_values[:type])) else push_spec_items(InputSpecItem.new(spec_item_values[:number], spec_item_values[:name])) end end end end |
Instance Method Details
#[](target_expression) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/anodator/input_spec.rb', line 125 def [](target_expression) raise SourceDataNotProvidedError.new if @source.nil? if target_expression.is_a? Fixnum return value_at(target_expression) elsif /^CALC::(.+)$/.match target_expression return value_by_calculation($1) else begin return value_at_by_number(target_expression) rescue UnknownTargetExpressionError return value_at_by_name(target_expression) end end end |
#check_calculation_expression(calculation) ⇒ Object
return all holders specs
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/anodator/input_spec.rb', line 188 def check_calculation_expression(calculation) if /(@|require|load|;)/.match calculation return ArgumentError.new("Invalid calcuation expression '#{calcuation}'") end calculation.scan(CALCULATION_HOLDER_REGEXP).flatten.map do |target_expression| spec_item_by_expression(target_expression) next target_expression end end |
#clear_source ⇒ Object
60 61 62 |
# File 'lib/anodator/input_spec.rb', line 60 def clear_source @source = nil end |
#source=(source) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/anodator/input_spec.rb', line 52 def source=(source) if source.respond_to? :[] @source = source else raise ArgumentError.new("source should respond to :[] method") end end |
#spec_item_at(index) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/anodator/input_spec.rb', line 141 def spec_item_at(index) if @spec_items[index].nil? raise UnknownTargetExpressionError.new("accessed by index '#{index}'") else @spec_items[index].dup end end |
#spec_item_at_by_name(name) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/anodator/input_spec.rb', line 157 def spec_item_at_by_name(name) if @name_dict.keys.include?(name) return @name_dict[name][:item].dup else raise UnknownTargetExpressionError.new("accessed by name '#{name}'") end end |
#spec_item_at_by_number(number) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/anodator/input_spec.rb', line 149 def spec_item_at_by_number(number) if @number_dict.keys.include?(number) return @number_dict[number][:item].dup else raise UnknownTargetExpressionError.new("accessed by number '#{number}'") end end |
#spec_item_by_expression(target_expression) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/anodator/input_spec.rb', line 173 def spec_item_by_expression(target_expression) if target_expression.is_a? Fixnum return spec_item_at(target_expression) elsif /^CALC::(.+)$/.match target_expression return spec_items_by_calculation($1) else begin return spec_item_at_by_number(target_expression) rescue UnknownTargetExpressionError return spec_item_at_by_name(target_expression) end end end |
#value_at(index) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/anodator/input_spec.rb', line 64 def value_at(index) raise SourceDataNotProvidedError.new if @source.nil? if @spec_items[index].nil? raise UnknownTargetExpressionError.new("accessed by index '#{index}'") else return @source[index].to_s end end |
#value_at_by_name(name) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/anodator/input_spec.rb', line 84 def value_at_by_name(name) raise SourceDataNotProvidedError.new if @source.nil? if @name_dict.keys.include?(name) return value_at(@name_dict[name][:index]) else raise UnknownTargetExpressionError.new("accessed by name '#{name}'") end end |
#value_at_by_number(number) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/anodator/input_spec.rb', line 74 def value_at_by_number(number) raise SourceDataNotProvidedError.new if @source.nil? if @number_dict.keys.include?(number) return value_at(@number_dict[number][:index]) else raise UnknownTargetExpressionError.new("accessed by number '#{number}'") end end |