lib/remi/cucumber/business_rules.rb in remi-0.2.17 vs lib/remi/cucumber/business_rules.rb in remi-0.2.18

- old
+ new

@@ -186,11 +186,11 @@ [@subjects.keys.first, full_field_name] end end def add_subject(subject_name, subject) - @subjects[subject_name] ||= DataSubject.new(subject) + @subjects[subject_name] ||= DataSubject.new(subject_name, subject) end def add_field(full_field_name) subject_names, field_name = parse_full_field(full_field_name, multi: true) Array(subject_names).each do |subject_name| @@ -202,17 +202,23 @@ raise "Multiple subjects defined: #{keys}" unless @subjects.size == 1 @subjects.values.first end def fields - dfc = DataFieldCollection.new - @subjects.each do |subject_name, subject| - subject.fields.each { |field_name, field| dfc.add_field(subject, field_name) } + Enumerator.new do |enum| + @subjects.each do |subject_name, subject| + subject.fields.each { |field_name, field| enum << field } + end end - dfc end + def full_field_names + @subjects.map do |subject_name, subject| + subject.fields.map { |field_name, field| "#{field.full_name}" } + end.flatten + end + def size @subjects.size end def total_size @@ -220,17 +226,19 @@ end end class DataSubject - def initialize(subject) + def initialize(name, subject) + @name = name @data_obj = subject @fields = DataFieldCollection.new stub_data end + attr_reader :name attr_reader :data_obj def add_field(field_name) @fields.add_field(self, field_name) end @@ -351,11 +359,11 @@ def distribute_values(table) cumulative_dist = cumulative_dist_from_freq_table(table) generated_data = generate_values_from_cumulative_dist(@data_obj.df.size, cumulative_dist) generated_data.each do |field_name, data_array| - vector_name = fields[field_name].name + vector_name = fields[field_name].field_name @data_obj.df[vector_name] = Daru::Vector.new(data_array, index: @data_obj.df.index) end end def freq_by(*field_names) @@ -401,12 +409,17 @@ def names @fields.values.map(&:name) end + def field_names + @fields.values.map(&:field_name) + end + def add_field(subject, field_name) - @fields[field_name] = DataField.new(subject.data_obj, field_name) unless @fields.include? field_name + raise "Attempting to add a field with the same name but different subject - #{subject.name}: #{field_name}" if @fields.include?(field_name) && @fields[field_name].subject.name != subject.name + @fields[field_name] = DataField.new(subject, field_name) unless @fields.include? field_name end def only raise "Multiple subject fields defined: #{keys}" if @fields.size > 1 @fields.values.first @@ -418,24 +431,29 @@ end end class DataField - def initialize(subject, field_name) + def initialize(subject, name) @subject = subject - @field_name = field_name.symbolize(subject.field_symbolizer) + @name = name + @field_name = name.symbolize(subject.data_obj.field_symbolizer) end - def name - @field_name + attr_reader :name + attr_reader :field_name + attr_reader :subject + + def full_name + "#{@subject.name}: #{@name}" end def metadata - @subject.fields[name] + @subject.data_obj.fields[@field_name] end def vector - @subject.df[@field_name] + @subject.data_obj.df[@field_name] end def value v = vector.to_a.uniq raise "Multiple unique values found in subject data for field #{@field_name}" if v.size > 1