Sha256: 4416ad38c32f71a6111c25d9a89d9a31a3e528ea506a7e0626839a2002236404

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Billogram
  class RelationBuilder
    attr_reader :resource, :attributes

    def initialize(resource, attributes)
      @resource = resource
      @attributes = attributes
    end

    def call
      resource_relations.each do |type, relation_names|
        build_relation(type, relation_names)
      end
    end

    private

    def relation_class(relation_name)
      Object.const_get("Billogram::#{relation_name.to_s.split('_').map(&:capitalize).join()}")
    end

    def relation_attributes(relation_name)
      attributes.delete(relation_name.to_s)
    end

    def resource_relations
      resource.class.relations
    end

    def build_relation(type, relation_names)
      relation_names.each do |name|
        if type == :one
          build_one_relation(name)
        elsif type == :many
          build_many_relation(name)
        end
      end
    end

    def build_one_relation(name)
      if attrs = relation_attributes(name)
        value = relation_class(name).new attrs
        resource.instance_variable_set("@#{name}", value)
      end
    end

    def build_many_relation(name)
      if attrs = relation_attributes(name)
        singular = name[0..name.length-2]
        value = attrs.map{|item| relation_class(singular).new(item) }
        resource.instance_variable_set("@#{name}", value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
billogram-0.2.0 lib/billogram/relation_builder.rb