From cf426e2a4818656c08eff5987cab79b09863f40c Mon Sep 17 00:00:00 2001 From: Stormwind Date: Tue, 24 Feb 2015 21:45:57 +0100 Subject: [PATCH] Add Rubella::Input::Base Add a base class for Inputs so we have an interface for this. Use this interface in Rubella::Input::JSON. --- lib/rubella/input/base.rb | 44 +++++++++++++++++++++++++++++++++++++ lib/rubella/input/json.rb | 11 ++++------ lib/rubella/output/ascii.rb | 2 +- lib/rubella/output/base.rb | 2 +- 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 lib/rubella/input/base.rb diff --git a/lib/rubella/input/base.rb b/lib/rubella/input/base.rb new file mode 100644 index 0000000..b756ebe --- /dev/null +++ b/lib/rubella/input/base.rb @@ -0,0 +1,44 @@ +module Rubella + module Input + + # Gets the rwa data and translate it into a by the Rubella::Weighting::Base + # class readable format. + # This must be an array within subarrays, which have all the same lenght + # and contains only numeric data. + # + # TODO The validation of the data should happen here + # Check if data is array, with subarrays. Check, that all sub array have + # the same size. Check that all the content are numeric values between 0 + # and 100. + # + class Base + attr_reader :data + + # Constructor + # Creates a new Rubella::Input::Base object, from the given data. + # + # @param value string A string, which contains the data + # @return Rubella::Input::Base + # + def self.string(value) + raise NotImplementedError "Please override 'self.string' in your "+ + "concrete implementation" + end + + # Constructor + # Creates a new Rubella::Input::Base object, from the given file. + # Only the file name is required. + # + # @param file_name string The name of the file + # @return Rubella:Input::Base + # + def self.file(file_name) + raise NotImplementedError "Please override 'self.file' in your "+ + "concrete implementation" + end + + + end + + end +end diff --git a/lib/rubella/input/json.rb b/lib/rubella/input/json.rb index e71db0d..99f2143 100644 --- a/lib/rubella/input/json.rb +++ b/lib/rubella/input/json.rb @@ -1,3 +1,4 @@ +require 'rubella/input/base' require 'json' module Rubella @@ -6,13 +7,7 @@ module Rubella # Gets data in JSON formate and translate it into a Ruby readable form to # make it possible to handle the data. # - # TODO The validation of the data should happen here - # Check if data is array, with subarrays. Check, that all sub array have - # the same size. Check that all the content are numeric values between 0 - # and 100. - # - class JSON - attr_reader :data + class JSON < Base # Constructor # This constructer can create a new Rubella::Input::JSON object, but it @@ -21,6 +16,7 @@ module Rubella # # @param json__string string A string, which contains the data as json # @return Rubella::Input::JSON + # def initialize(json_string) @data = ::JSON::load(json_string) end @@ -40,6 +36,7 @@ module Rubella # # @param json_file string The name of the file, which json contains # @return Rubella:Input::JSON + # def self.file(json_file) self.new File.new(json_file, 'r') end diff --git a/lib/rubella/output/ascii.rb b/lib/rubella/output/ascii.rb index cf991f0..01d467b 100644 --- a/lib/rubella/output/ascii.rb +++ b/lib/rubella/output/ascii.rb @@ -100,4 +100,4 @@ module Rubella end end -end \ No newline at end of file +end diff --git a/lib/rubella/output/base.rb b/lib/rubella/output/base.rb index 95d8bcb..1135757 100644 --- a/lib/rubella/output/base.rb +++ b/lib/rubella/output/base.rb @@ -35,4 +35,4 @@ module Rubella end end -end \ No newline at end of file +end