Module: DataMapper::Is::Predefined::ClassMethods
- Defined in:
- lib/dm-is-predefined/is/predefined.rb
Instance Method Summary collapse
-
#first_or_predefined(conditions = {}, attributes = conditions) ⇒ DataMapper::Resource?
Finds or auto-creates the predefined resource which shares the given attributes.
-
#predefine(name, attributes = {}) ⇒ Hash
protected
Defines a new pre-defined resource for the model.
-
#predefine!(repository_name = self.repository_name) ⇒ Object
Creates the predefined resources.
-
#predefined ⇒ Array<Symbol>
Returns the names of the predefined resources.
-
#predefined?(name) ⇒ Boolean
Determines if a resource was predefined.
-
#predefined_attributes ⇒ Hash{Symbol => Hash}
All pre-defined resources of the model.
-
#predefined_names ⇒ Object
deprecated
Deprecated.
Will be removed in 1.0.0.
-
#predefined_resource(name) ⇒ DataMapper::Resource
Finds or auto-creates the pre-defined resource with the given name.
-
#predefined_resource_with(query = {}) ⇒ Object
deprecated
Deprecated.
Will be removed in 1.0.0. Use #first_or_predefined instead.
Instance Method Details
#first_or_predefined(conditions = {}, attributes = conditions) ⇒ DataMapper::Resource?
Finds or auto-creates the predefined resource which shares the given attributes.
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.
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.
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 |
#predefined ⇒ Array<Symbol>
Returns the names of the predefined resources.
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.
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_attributes ⇒ Hash{Symbol => Hash}
All pre-defined resources of the model.
73 74 75 |
# File 'lib/dm-is-predefined/is/predefined.rb', line 73 def predefined_attributes @predefined_attributes ||= {} end |
#predefined_names ⇒ Object
Will be removed in 1.0.0.
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.
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
Will be removed in 1.0.0. Use #first_or_predefined instead.
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 |