Sha256: a560b0da2aa0e97befa32c153e3e729b9191759e6e06ba719459b41b7e59ffe8

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'active_conformity/conformity_set_validator'
module ActiveConformity
  class Conformable < ActiveRecord::Base
    validates :conformity_set, conformity_set: true

    def add_conformity_set(incoming_set={})
      self.conformity_set = JSON.parse(self.conformity_set) if self.conformity_set.is_a?(String)
      conformity_set = JSON.parse(incoming_set) rescue incoming_set
      conformity_set = self.conformity_set.deep_merge(incoming_set) rescue conformity_set
      self.conformity_set = conformity_set.to_json
    end

    def conformity_set
      if super.is_a? String
        JSON.parse(super).deep_symbolize_keys! rescue super
      else
        super
      end
    end

    def remove_coformity_rule(attr)
      conformity_set = JSON.parse(self.conformity_set) rescue self.conformity_set
      conformity_set.delete(attr) do
        raise "no rule found for #{attr.to_s}"
      end
      self.conformity_set = conformity_set.to_json
    end

    def remove_rules
      self.conformity_set = {}.to_json
    end

    def remove_rules!
      remove_rules
      save!
    end

    def remove_coformity_rule!(attr)
      remove_coformity_rule(attr)
      save!
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_conformity-0.3.0 lib/active_conformity/conformable.rb
active_conformity-0.2.15 lib/active_conformity/conformable.rb
active_conformity-0.2.14 lib/active_conformity/conformable.rb