Sha256: a1271a052678b1f877eb5cf780d9db53ba2f5d40c32cf8219304c271c82d21f3

Contents?: true

Size: 1.91 KB

Versions: 45

Compression:

Stored size: 1.91 KB

Contents

module Fog
  module CloudSigma
    class CloudsigmaModel < Fog::Model
      class << self
        def model_attribute_array(name, model, options={})
          attributes_key = options[:aliases] || name
          class_eval <<-EOS, __FILE__, __LINE__
            def #{name}
              #{name}_attrs = attributes[:#{attributes_key}] || []
              refreshed_#{name} = #{name}_attrs.map { |x| #{model}.new(x) }
              attributes[:#{attributes_key}] = refreshed_#{name}.map { |x| x.attributes }

              refreshed_#{name}
            end
            def #{name}=(new_#{name})
              new_#{name} ||= []
              attributes[:#{attributes_key}] = new_#{name}.map { |x| x.kind_of?(Hash) ? x : x.attributes}
            end
          EOS

          @attributes ||= []
          @attributes |= [name]
          for new_alias in [*options[:aliases]]
            aliases[new_alias] = name
          end
        end

        def model_attribute(name, model, options={})
          attributes_key = options[:aliases] || name
          class_eval <<-EOS, __FILE__, __LINE__
            def #{name}
              #{name}_attrs = attributes[:#{attributes_key}]
              if #{name}_attrs
                refreshed_#{name} = #{name}_attrs ?  #{model}.new(#{name}_attrs) : nil
                attributes[:#{attributes_key}] = refreshed_#{name}.attributes

                refreshed_#{name}
              else
                nil
              end
            end
            def #{name}=(new_#{name})
              if new_#{name}
                attributes[:#{attributes_key}] = new_#{name}.kind_of?(Hash) ? new_#{name} : new_#{name}.attributes
              else
                nil
              end
            end
          EOS

          @attributes ||= []
          @attributes |= [name]
          for new_alias in [*options[:aliases]]
            aliases[new_alias] = name
          end
        end
      end
    end
  end
end

Version data entries

45 entries across 43 versions & 6 rubygems

Version Path
fog-2.3.0 lib/fog/cloudsigma/nested_model.rb
fog-ifeel-2.2.0 lib/fog/cloudsigma/nested_model.rb
fog-2.2.0 lib/fog/cloudsigma/nested_model.rb
fog-2.1.0 lib/fog/cloudsigma/nested_model.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/fog-1.42.1/lib/fog/cloudsigma/nested_model.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/fog-1.42.1/lib/fog/cloudsigma/nested_model.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/fog-1.42.1/lib/fog/cloudsigma/nested_model.rb
fog-1.42.1 lib/fog/cloudsigma/nested_model.rb
fog-2.0.0 lib/fog/cloudsigma/nested_model.rb
fog-1.42.0 lib/fog/cloudsigma/nested_model.rb
fog-1.41.0 lib/fog/cloudsigma/nested_model.rb
fog-1.40.0 lib/fog/cloudsigma/nested_model.rb
fog-1.39.0 lib/fog/cloudsigma/nested_model.rb
fog-1.38.0 lib/fog/cloudsigma/nested_model.rb
fog-1.37.0 lib/fog/cloudsigma/nested_model.rb
fog-1.36.0 lib/fog/cloudsigma/nested_model.rb
fog-1.35.0 lib/fog/cloudsigma/nested_model.rb
fog-2.0.0.pre.0 lib/fog/cloudsigma/nested_model.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/fog-1.32.0/lib/fog/cloudsigma/nested_model.rb
fog-1.34.0 lib/fog/cloudsigma/nested_model.rb