Sha256: 0506bb914bd646196393ff78206a5f31369fb13012e86759b65bbf3e7f89c65e

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

require "active_support/core_ext/string/inflections.rb"

module Billogram
  class Resource
    class << self
      def relations
        @relations ||= []
      end

      def relation(name, type, class_override: nil)
        relations << Relation.new(name, type, class_override: class_override)
        attr_accessor name
      end

      def build_objects(data)
        case data
        when Hash then new(data)
        when Array then data.map{|item| build_objects(item) }
        else data
        end
      end
    end

    def initialize(attributes = {})
      Hash(attributes).each do |key, value|
        if respond_to?("#{key}=")
          public_send("#{key}=", value)
        else
          warn("#{self.class}: unknown attribute #{key}")
        end
      end

      RelationBuilder.new(self, attributes).call
    end

    def to_json(*args)
      to_hash.to_json(*args)
    end

    def to_hash
      instance_variables
        .each_with_object({}) do |variable, obj|
          value = instance_variable_get(variable)

          case value
          when Resource
            value = value.to_hash
          when Array
            value = value.map(&:to_hash)
          end

          obj[variable[1..-1]] = value
        end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
billogram-0.6.5 lib/billogram/resource.rb
billogram-0.6.4 lib/billogram/resource.rb
billogram-0.6.3 lib/billogram/resource.rb
billogram-0.6.2 lib/billogram/resource.rb
billogram-0.6.1 lib/billogram/resource.rb
billogram-0.6.0 lib/billogram/resource.rb
billogram-0.5.6 lib/billogram/resource.rb
billogram-0.5.5 lib/billogram/resource.rb
billogram-0.5.4 lib/billogram/resource.rb
billogram-0.5.3 lib/billogram/resource.rb
billogram-0.5.2 lib/billogram/resource.rb