0
0
Fork 0

Add Rubella::Input::Base

Add a base class for Inputs so we have an interface for this.
Use this interface in Rubella::Input::JSON.
This commit is contained in:
Stormwind 2015-02-24 21:45:57 +01:00
parent 600443ddd4
commit cf426e2a48
4 changed files with 50 additions and 9 deletions

44
lib/rubella/input/base.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -100,4 +100,4 @@ module Rubella
end
end
end
end

View File

@ -35,4 +35,4 @@ module Rubella
end
end
end
end