Sha256: 1fc2fe762ca20a61e33d2fb7e51f29f26c79e965130fc8e99c7e7904aa20db8b

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Chronicle
  module Models
    class ModelFactory
      attr_reader :id

      def initialize(type_id:, properties: [])
        @type_id = type_id
        @properties = properties
      end

      def generate
        attribute_info = @properties.map do |property|
          generate_attribute_info(property)
        end

        type_id = @type_id

        Class.new(Chronicle::Models::Base) do
          attribute_info.each do |a|
            attribute(a[:name], a[:type])
          end

          @type_id = type_id
        end
      end

      private

      def generate_attribute_info(property)
        range = build_type(property)
        type = if property.many?
                 Chronicle::Schema::Types::Array.of(range)
               else
                 range
               end

        type = type.optional.default(nil) unless property.required?
        type = type.meta(required: property.required?, many: property.many?)

        {
          name: property.id_snakecase.to_sym,
          type:
        }
      end

      def build_type(property)
        type_values = []

        full_range_identifiers = property.full_range_identifiers

        type_values << Chronicle::Schema::Types::Params::Time if full_range_identifiers.include? :DateTime
        type_values << Chronicle::Schema::Types::String if %i[Text URL Distance Duration Energy
                                                              Mass].intersect?(full_range_identifiers)

        type_values << Chronicle::Schema::Types::Params::Float if full_range_identifiers.include? :Float
        type_values << Chronicle::Schema::Types::Params::Integer if full_range_identifiers.include? :Integer
        type_values << Chronicle::Models.schema_type(full_range_identifiers)

        type_values.reduce do |memo, type|
          memo | type
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chronicle-core-0.3.2 lib/chronicle/models/model_factory.rb
chronicle-core-0.3.1 lib/chronicle/models/model_factory.rb