lib/wcc/contentful/model_builder.rb in wcc-contentful-1.0.8 vs lib/wcc/contentful/model_builder.rb in wcc-contentful-1.1.0

- old
+ new

@@ -5,12 +5,15 @@ module WCC::Contentful class ModelBuilder include Helpers - def initialize(types) + attr_reader :namespace + + def initialize(types, namespace: WCC::Contentful::Model) @types = types + @namespace = namespace end def build_models @types.each_with_object([]) do |(_k, v), a| a << build_model(v) @@ -19,16 +22,17 @@ private def build_model(typedef) const = typedef.name - return WCC::Contentful::Model.const_get(const) if WCC::Contentful::Model.const_defined?(const) + ns = namespace + return ns.const_get(const) if ns.const_defined?(const) # TODO: https://github.com/dkubb/ice_nine ? typedef = typedef.deep_dup.freeze - WCC::Contentful::Model.const_set(const, - Class.new(WCC::Contentful::Model) do + ns.const_set(const, + Class.new(namespace) do extend ModelSingletonMethods include ModelMethods include Helpers const_set('ATTRIBUTES', typedef.fields.keys.map(&:to_sym).freeze) @@ -47,9 +51,11 @@ end define_singleton_method(:content_type_definition) do typedef end + + define_singleton_method(:model_namespace) { ns } define_method(:initialize) do |raw, context = nil| ct = content_type_from_raw(raw) if ct != typedef.content_type raise ArgumentError, 'Wrong Content Type - ' \