Module: DataMapper::Is::Predefined::ClassMethods

Defined in:
lib/dm-is-predefined/is/predefined.rb

Instance Method Summary collapse

Instance Method Details

#first_or_predefined(conditions = {}, attributes = conditions) ⇒ DataMapper::Resource?

Finds or auto-creates the predefined resource which shares the given attributes.

Parameters:

  • conditions (Hash{Symbol => Object}) (defaults to: {})

    Query conditions.

  • attributes (Hash{Symbol => Object}) (defaults to: conditions)

    The attribute names and values that the predefined resource should shared.

Returns:

  • (DataMapper::Resource, nil)

    The predefined resource.

Since:

  • 0.4.0



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dm-is-predefined/is/predefined.rb', line 168

def first_or_predefined(conditions={},attributes=conditions)
  if (resource = first(conditions))
    return resource
  end

  # if the resource wasn't found, search for matching
  # predefined attributes
  attributes = predefined_attributes.values.find do |attrs|
    attrs.all? do |name,value|
      attributes.has_key?(name) && (attributes[name] == value)
    end
  end

  # create the resource using the predefined attributes
  create(attributes) if attributes
end

#predefine(name, attributes = {}) ⇒ Hash (protected)

Defines a new pre-defined resource for the model.

Parameters:

  • name (Symbol, String)

    The name of the pre-defined resource.

  • attributes (Hash) (defaults to: {})

    The attributes for the pre-defined resource.

Returns:

  • (Hash)

    The attributes that will be assigned to the pre-defined resource.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/dm-is-predefined/is/predefined.rb', line 241

def predefine(name,attributes={})
  name = name.to_sym

  if attributes.empty?
    raise(ArgumentError,"Cannot predefine a resource with no attributes")
  end

  predefined_attributes[name] = attributes

  class_eval %{
    class << self
      define_method(#{name.inspect}) do
        predefined_resource(#{name.inspect})
      end
    end
  }

  return attributes
end

#predefine!(repository_name = self.repository_name) ⇒ Object

Creates the predefined resources.

Parameters:

  • repository_name (Symbol) (defaults to: self.repository_name)

    The repository to perform the upgrade within.

Since:

  • 0.4.0



216
217
218
219
220
221
222
# File 'lib/dm-is-predefined/is/predefined.rb', line 216

def predefine!(repository_name=self.repository_name)
  DataMapper.repository(repository_name) do
    predefined_attributes.each_value do |attributes|
      first_or_create(attributes)
    end
  end
end

#predefinedArray<Symbol>

Returns the names of the predefined resources.

Returns:

  • (Array<Symbol>)

    The names of the predefined resources.

Since:

  • 0.4.0



87
88
89
# File 'lib/dm-is-predefined/is/predefined.rb', line 87

def predefined
  predefined_attributes.keys
end

#predefined?(name) ⇒ Boolean

Determines if a resource was predefined.

Parameters:

  • name (Symbol, String)

    The name of the predefined resource to search for.

Returns:

  • (Boolean)

    Specifies whether the resource was predefined.

Since:

  • 0.4.0



115
116
117
# File 'lib/dm-is-predefined/is/predefined.rb', line 115

def predefined?(name)
  predefined_attributes.has_key?(name.to_sym)
end

#predefined_attributesHash{Symbol => Hash}

All pre-defined resources of the model.

Returns:

  • (Hash{Symbol => Hash})

    The Hash of pre-defined resources and their attributes.



73
74
75
# File 'lib/dm-is-predefined/is/predefined.rb', line 73

def predefined_attributes
  @predefined_attributes ||= {}
end

#predefined_namesObject

Deprecated.

Will be removed in 1.0.0.

See Also:

Since:

  • 0.2.1



98
99
100
# File 'lib/dm-is-predefined/is/predefined.rb', line 98

def predefined_names
  predefined
end

#predefined_resource(name) ⇒ DataMapper::Resource

Finds or auto-creates the pre-defined resource with the given name.

Parameters:

  • name (Symbol, String)

    The name of the pre-defined resource.

  • extra_attributes (Hash{Symbol => Object})

    Additional attributes to add to the predefined resource.

Returns:

  • (DataMapper::Resource)

    The pre-defined resource.

Raises:

  • (UnknownResource)

    Indicates that there are no predefined attributes for the resource with the given name.

Since:

  • 0.2.1



140
141
142
143
144
145
146
147
148
# File 'lib/dm-is-predefined/is/predefined.rb', line 140

def predefined_resource(name)
  name = name.to_sym

  unless predefined?(name)
    raise(UnknownResource,"The resource '#{name}' was not predefined")
  end

  return first_or_create(predefined_attributes[name])
end

#predefined_resource_with(query = {}) ⇒ Object

Deprecated.

Will be removed in 1.0.0. Use #first_or_predefined instead.

Raises:

  • (UnknownResource)

    Could not find a predefined resource that shared all of the desired attributes.

Since:

  • 0.2.1



197
198
199
200
201
202
203
204
# File 'lib/dm-is-predefined/is/predefined.rb', line 197

def predefined_resource_with(query={})
  unless (resource = first_or_predefined(query))
    # no pre-existing or predefined resource matching the query
    raise(UnknownResource,"Could not find a predefined resource which shared the given attributes")
  end

  return resource
end