Sha256: 4d1b2267475e5cbec43146544af8207198b06865fcd6b6292b7d13c911a31dd4

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

module PropertySets
  module PropertySetModel
    module InstanceMethods

      def false?
        [ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
      end

      def true?
        !false?
      end

      def enable
        update_attribute(:value, "1")
        self
      end

      def disable
        update_attribute(:value, "0")
        self
      end

      def protected?
        self.class.protected?(name.to_sym)
      end

      def to_s
        value.to_s
      end

      private

      def validate_format_of_name
        if name.blank?
          errors.add(:name, :blank)
        elsif !name.is_a?(String) || name !~ /^([a-z0-9]+_?)+$/
          errors.add(:name, :invalid)
        end
      end

      def coerce_value
        self.value = value.to_s unless value.nil?
      end

      def owner_class_instance
        send(self.class.owner_class_sym)
      end
    end

    module ClassMethods
      def self.extended(base)
        base.validate      :validate_format_of_name
        base.before_create :coerce_value
      end

      def property(key, options = nil)
        @properties ||= {}
        @properties[key] = options
      end

      def keys
        @properties.keys
      end

      def default(key)
        @properties[key] && @properties[key].key?(:default) ? @properties[key][:default] : nil
      end

      def protected?(key)
        @properties[key] && !!@properties[key][:protected]
      end

      def owner_class=(owner_class)
        @owner_class_sym = owner_class.name.underscore.to_sym
        belongs_to              owner_class_sym
        validates_presence_of   owner_class_sym
        validates_uniqueness_of :name, :scope => owner_class_key_sym
      end

      def owner_assoc=(association)
        @owner_assoc = association
      end

      def owner_assoc
        @owner_assoc
      end

      def owner_class_sym
        @owner_class_sym
      end

      def owner_class_key_sym
        "#{owner_class_sym}_id".to_sym
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
property_sets-0.5.1 lib/property_sets/property_set_model.rb
property_sets-0.5.0 lib/property_sets/property_set_model.rb