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

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

Instance Method Summary collapse

Instance Method Details

#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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dm-is-predefined/is/predefined.rb', line 107

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

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

  self.predefined_attributes[name.to_sym] = attributes

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

  attributes
end

#predefined_attributesHash{Symbol => Hash} (protected)

All pre-defined resources of the model.

Returns:

  • (Hash{Symbol => Hash})

    The Hash of pre-defined resources and their attributes.



91
92
93
# File 'lib/dm-is-predefined/is/predefined.rb', line 91

def predefined_attributes
  @predefined_attributes ||= {}
end

#predefined_namesArray<Symbol>

Returns the names of the predefined resources.

Returns:

  • (Array<Symbol>)

    The names of the predefined resources.

Since:

  • 0.2.1



22
23
24
# File 'lib/dm-is-predefined/is/predefined.rb', line 22

def predefined_names
  predefined_attributes.keys
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.

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/dm-is-predefined/is/predefined.rb', line 41

def predefined_resource(name)
  name = name.to_sym
  attributes = self.predefined_attributes[name]

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

  self.first_or_create(attributes)
end

#predefined_resource_with(desired_attributes = {}) ⇒ DataMapper::Resource

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

Parameters:

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

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

Returns:

  • (DataMapper::Resource)

    The predefined resource.

Raises:

  • (UnknownResource)

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

Since:

  • 0.2.1



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dm-is-predefined/is/predefined.rb', line 69

def predefined_resource_with(desired_attributes={})
  self.predefined_attributes.each do |name,attributes|
    shares_attributes = desired_attributes.all? do |key,value|
      key = key.to_sym

      attributes.has_key?(key) && (attributes[key] == value)
    end

    return predefined_resource(name) if shares_attributes
  end

  raise(UnknownResource,"Could not find a predefined resource which shared the given attributes",caller)
end