Sha256: e7d58ff21317d9e8b1f0dc9bb8446519bc48244fc2e33ae0f8703bcfc603f395

Contents?: true

Size: 1.06 KB

Versions: 78

Compression:

Stored size: 1.06 KB

Contents

module CFoundry::V2::ModelMagic
  module Attribute
    def attribute(name, type, opts = {})
      attributes[name] = opts
      json_name = opts[:at] || name

      default = opts[:default]

      if has_default = opts.key?(:default)
        defaults[name] = default
      end

      #
      # def ATTRIBUTE
      #
      define_method(name) do
        return @cache[name] if @cache.key?(name)
        return nil unless persisted?

        @cache[name] =
          if manifest[:entity].key?(json_name)
            manifest[:entity][json_name]
          else
            default
          end
      end

      #
      # def ATTRIBUTE=
      #
      define_method(:"#{name}=") do |val|
        unless has_default && val == default
          CFoundry::Validator.validate_type(val, type)
        end

        @cache[name] = val

        @manifest ||= {}
        @manifest[:entity] ||= {}

        old = @manifest[:entity][json_name]
        @changes[name] = [old, val] if old != val
        @manifest[:entity][json_name] = val

        @diff[json_name] = val
      end
    end
  end
end

Version data entries

78 entries across 78 versions & 3 rubygems

Version Path
cfoundry-4.1.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.4.rc2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.4.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.3 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.2.rc5 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.2.rc4 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.2.rc3 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.2.rc2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.2.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.0.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-3.0.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-3.0.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.4.1.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.4.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.7.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.6 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.6.rc2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.6.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.5 lib/cfoundry/v2/model_magic/attribute.rb