Sha256: 6fc7367c2bf706bec4162da92c5549fd6e0b3088b40ea010ee4102ee201798c8

Contents?: true

Size: 1.91 KB

Versions: 51

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

51 entries across 51 versions & 3 rubygems

Version Path
fog-maestrodev-1.20.0.20140305101839 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.20.0.20140305101305 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20140212012611 lib/fog/cloudsigma/nested_model.rb
fog-1.20.0 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20140110004459 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20140110003812 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20140109202555 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20140107192102 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20140107142106 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.19.0.20131219203941 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131219193542 lib/fog/cloudsigma/nested_model.rb
fog-1.19.0 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131219033443 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131219032002 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131219030716 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131219022322 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131218202447 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131209091424 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131209090811 lib/fog/cloudsigma/nested_model.rb
fog-maestrodev-1.18.0.20131206115947 lib/fog/cloudsigma/nested_model.rb