Skip to content

Commit

Permalink
Merge pull request #21 from contentful/modifying_nil_bugfix
Browse files Browse the repository at this point in the history
Modifying nil bugfix
  • Loading branch information
pxlpnk committed Aug 28, 2014
2 parents 26a889e + fedb434 commit bc71b6c
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 109 deletions.
22 changes: 2 additions & 20 deletions lib/contentful/management/content_type.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*-
require_relative 'resource'
require_relative 'field'
require_relative 'content_type_entry_methods_factory'

module Contentful
module Management
Expand Down Expand Up @@ -184,26 +185,7 @@ def fields
# Returns a Contentful::Management::Entry.
# See README for details.
def entries
entries = nil

entries.instance_exec(self) do |content_type|

define_singleton_method(:all) do
Contentful::Management::Entry.all(content_type.space.id, content_type: content_type.id)
end

define_singleton_method(:create) do |params|
Entry.create(content_type, params)
end

define_singleton_method(:new) do
dynamic_entry_class = content_type.client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
dynamic_entry = dynamic_entry_class.new
dynamic_entry.content_type = content_type
dynamic_entry
end
end
entries
Contentful::Management::ContentTypeEntryMethodsFactory.new(self)
end

private
Expand Down
28 changes: 28 additions & 0 deletions lib/contentful/management/content_type_entry_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Contentful
module Management
class ContentTypeEntryMethodsFactory

attr_reader :content_type

def initialize(content_type)
@content_type = content_type
end

def all(params = {})
Entry.all(content_type.space.id, params.merge(content_type: content_type.id))
end

def create(attributes)
Entry.create(content_type, attributes)
end

def new
dynamic_entry_class = content_type.client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
dynamic_entry = dynamic_entry_class.new
dynamic_entry.content_type = content_type
dynamic_entry
end

end
end
end
2 changes: 1 addition & 1 deletion lib/contentful/management/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Locale
# Gets a collection of locales.
# Takes an id of a space.
# Returns a Contentful::Management::Array of Contentful::Management::Locale.
def self.all(space_id = nil)
def self.all(space_id = nil, parameters = {})
request = Request.new("/#{ space_id }/locales")
response = request.get
result = ResourceBuilder.new(response, { 'Locale' => Locale }, {})
Expand Down
102 changes: 14 additions & 88 deletions lib/contentful/management/space.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# -*- encoding: utf-8 -*-
require_relative 'resource'
require_relative 'locale'
require_relative 'space_locale_methods_factory'
require_relative 'content_type'
require_relative 'space_content_type_methods_factory'
require_relative 'asset'
require_relative 'space_asset_methods_factory'
require_relative 'entry'
require_relative 'space_entry_methods_factory'
require_relative 'webhook'
require_relative 'space_webhook_methods_factory'

module Contentful
module Management
Expand Down Expand Up @@ -89,114 +94,35 @@ def destroy
# Allows listing all content types of space, creating new and finding one by id.
# See README for details.
def content_types
content_types = nil

content_types.instance_exec(self) do |space|

define_singleton_method(:all) do |attributes = {}|
ContentType.all(space.id, attributes)
end

define_singleton_method(:create) do |params|
ContentType.create(space.id, params)
end

define_singleton_method(:find) do |content_type_id|
ContentType.find(space.id, content_type_id)
end

define_singleton_method(:new) do
ct = ContentType.new
ct.sys[:space] = space
ct
end

end
content_types
SpaceContentTypeMethodsFactory.new(self)
end

# Allows manipulation of locales in context of the current space
# Allows listing all locales of space, creating new and finding one by id.
# See README for details.
def locales
locales = nil

locales.instance_exec(self) do |space|
define_singleton_method(:all) do
Locale.all(space.id)
end

define_singleton_method(:create) do |params|
Locale.create(space.id, params)
end

define_singleton_method(:find) do |locale_id|
Locale.find(space.id, locale_id)
end
end

locales
SpaceLocaleMethodsFactory.new(self)
end

# Allows manipulation of assets in context of the current space
# Allows listing all assets of space, creating new and finding one by id.
# See README for details.
def assets
assets = nil

assets.instance_exec(self) do |space|
define_singleton_method(:all) do |attributes = {}|
Asset.all(space.id,attributes)
end

define_singleton_method(:find) do |asset_id|
Asset.find(space.id, asset_id)
end

define_singleton_method(:create) do |params|
Asset.create(space.id, params)
end

define_singleton_method(:new) do
asset = Asset.new
asset.sys[:space] = space
asset
end
end
assets
SpaceAssetMethodsFactory.new(self)
end

# Allows manipulation of entries in context of the current space
# Allows listing all entries of space and finding one by id.
# Allows listing all entries for space and finding one by id.
# See README for details.
def entries
entries = nil

entries.instance_exec(self) do |space|
define_singleton_method(:all) do |attributes = {}|
Entry.all(space.id, attributes)
end

define_singleton_method(:find) do |entry_id|
Entry.find(space.id, entry_id)
end
end
entries
SpaceEntryMethodsFactory.new(self)
end

# Allows manipulation of webhooks in context of the current space
# Allows listing all webhooks for space and finding one by id.
# See README for details.
def webhooks
webhooks = nil

webhooks.instance_exec(self) do |space|
define_singleton_method(:all) do |attributes = {}|
Webhook.all(space.id, attributes)
end

define_singleton_method(:find) do |webhook_id|
Webhook.find(space.id, webhook_id)
end
end
webhooks
SpaceWebhookMethodsFactory.new(self)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/contentful/management/space_asset_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative 'space_association_methods_factory'

module Contentful
module Management
class SpaceAssetMethodsFactory

include Contentful::Management::SpaceAssociationMethodsFactory

end
end
end
38 changes: 38 additions & 0 deletions lib/contentful/management/space_association_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Contentful
module Management
module SpaceAssociationMethodsFactory

attr_reader :space

def initialize(space)
@space = space
end

def all(params = {})
associated_class.all(space.id, params)
end

def find(id)
associated_class.find(space.id, id)
end

def create(attributes)
associated_class.create(space.id, attributes)
end

def new
object = associated_class.new
object.sys[:space] = space
object
end

def associated_class
class_name = /\A(.+)Space(.+)MethodsFactory\z/.match(self.class.name).captures.join
class_name.split('::').inject(Object) do |mod, class_name|
mod.const_get(class_name)
end
end

end
end
end
11 changes: 11 additions & 0 deletions lib/contentful/management/space_content_type_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative 'space_association_methods_factory'

module Contentful
module Management
class SpaceContentTypeMethodsFactory

include Contentful::Management::SpaceAssociationMethodsFactory

end
end
end
19 changes: 19 additions & 0 deletions lib/contentful/management/space_entry_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative 'space_association_methods_factory'

module Contentful
module Management
class SpaceEntryMethodsFactory

include Contentful::Management::SpaceAssociationMethodsFactory

def create(attributes)
fail 'Not supported'
end

def new
fail 'Not supported'
end

end
end
end
15 changes: 15 additions & 0 deletions lib/contentful/management/space_locale_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative 'space_association_methods_factory'

module Contentful
module Management
class SpaceLocaleMethodsFactory

include Contentful::Management::SpaceAssociationMethodsFactory

def new
fail 'Not supported'
end

end
end
end
19 changes: 19 additions & 0 deletions lib/contentful/management/space_webhook_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative 'space_association_methods_factory'

module Contentful
module Management
class SpaceWebhookMethodsFactory

include Contentful::Management::SpaceAssociationMethodsFactory

def create(attributes)
fail 'Not supported'
end

def new
fail 'Not supported'
end

end
end
end

0 comments on commit bc71b6c

Please sign in to comment.