Sha256: b99497214d98f6b2ce70caa892d3f8c83a4dd04ca47369d0a97a4b5bd4de3e16

Contents?: true

Size: 1005 Bytes

Versions: 2

Compression:

Stored size: 1005 Bytes

Contents

module Falcor
  class Factory
    include ClassAttrs
    include DefineDsl

    class_attr :fields
    class_attr :lists
    class_attr :associations

    class << self
      def create_methods(blk)
        instance_eval(&blk)
      end
    end

    def initialize(overrides)
      unless overrides.empty?
        overrides.each do |k,v|
          self.send(:"#{k}=", v)
        end
      end
    end

    def fields
      self.class.fields || []
    end

    def associations
      self.class.associations || []
    end

    def lists
      self.class.lists || []
    end

    def to_json
      json = {}

      fields.each do |field|
        value = self.send(field)
        unless value.nil?
          json[field.to_s] = self.send(field)
        end
      end

      associations.each do |field|
        json[field.to_js] = self.send(field).send(:to_json)
      end

      lists.each do |field|
        json[field.to_s] = self.send(field).map { |m| m.to_json }
      end

      json
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
falcor-0.0.3 lib/falcor/factory.rb
falcor-0.0.2 lib/falcor/factory.rb