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
new_cfoundry-4.9.4 lib/cfoundry/v2/model_magic/attribute.rb
new_cfoundry-4.9.3 lib/cfoundry/v2/model_magic/attribute.rb
ncfoundry-4.9.4 lib/cfoundry/v2/model_magic/attribute.rb
ncfoundry-4.9.3 lib/cfoundry/v2/model_magic/attribute.rb
ncfoundry-4.9.2 lib/cfoundry/v2/model_magic/attribute.rb
new_cfoundry-4.9.2 lib/cfoundry/v2/model_magic/attribute.rb
new_cfoundry-4.9.1 lib/cfoundry/v2/model_magic/attribute.rb
new_cfoundry-4.9.0 lib/cfoundry/v2/model_magic/attribute.rb
new_cfoundry-4.8.3 lib/cfoundry/v2/model_magic/attribute.rb
new_cfoundry-4.8.2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.7.2.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.7.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.7.1.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.7.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.6.3.rc3 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.6.3.rc2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.6.3.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.6.2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.6.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-4.6.0 lib/cfoundry/v2/model_magic/attribute.rb