lib/rollout/feature.rb in rollout-2.5.0 vs lib/rollout/feature.rb in rollout-2.6.0

- old
+ new

@@ -3,16 +3,17 @@ class Rollout class Feature attr_accessor :groups, :users, :percentage, :data attr_reader :name, :options - def initialize(name, string = nil, opts = {}) - @options = opts - @name = name + def initialize(name, rollout:, state: nil, options: {}) + @name = name + @rollout = rollout + @options = options - if string - raw_percentage, raw_users, raw_groups, raw_data = string.split('|', 4) + if state + raw_percentage, raw_users, raw_groups, raw_data = state.split('|', 4) @percentage = raw_percentage.to_f @users = users_from_string(raw_users) @groups = groups_from_string(raw_groups) @data = raw_data.nil? || raw_data.strip.empty? ? {} : JSON.parse(raw_data) else @@ -46,16 +47,16 @@ @users = users_from_string('') @percentage = 0 @data = {} end - def active?(rollout, user) + def active?(user) if user id = user_id(user) user_in_percentage?(id) || user_in_active_users?(id) || - user_in_active_group?(user, rollout) + user_in_active_group?(user) else @percentage == 100 end end @@ -70,10 +71,18 @@ users: @users, data: @data, } end + def deep_clone + c = self.clone + c.instance_variable_set('@rollout', nil) + c = Marshal.load(Marshal.dump(c)) + c.instance_variable_set('@rollot', @rollout) + c + end + private def user_id(user) if user.is_a?(Integer) || user.is_a?(String) user.to_s @@ -96,12 +105,12 @@ else user_id(user) end end - def user_in_active_group?(user, rollout) + def user_in_active_group?(user) @groups.any? do |g| - rollout.active_in_group?(g, user) + @rollout.active_in_group?(g, user) end end def serialize_data return '' unless @data.is_a? Hash