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-2.3.4 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.3 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.3.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.2.0rc3 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.2.0.rc2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.2.0.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.1.2.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.1.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.1.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.0.2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.0.1.rc1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-2.0.0 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-1.5.3 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-1.5.2 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-1.5.1 lib/cfoundry/v2/model_magic/attribute.rb
cfoundry-1.5.0 lib/cfoundry/v2/model_magic/attribute.rb