Sha256: 753192b57c9591d9323239c3f2766e9fa592e6a83b758aa4989506a59744b303

Contents?: true

Size: 1.07 KB

Versions: 12

Compression:

Stored size: 1.07 KB

Contents

module Boolean; end
class TrueClass; include Boolean; end
class FalseClass; include Boolean; end

module TryApi
  class Base
    def self.typesafe_accessor(name, type, options={})

      define_method(name) do
        instance_variable_get("@#{name}")
      end

      define_method("#{name}=") do |value|
        if value.is_a?(type) || value.nil?
          value = options[:default] if value.nil? && !options[:default].nil?
          instance_variable_set("@#{name}", value)
        else
          raise TryApi::ArgumentError.new
        end
      end
    end

    def to_json
      result = {}

      self.instance_variables.each do |i|
        value = self.instance_variable_get(i)
        if value.instance_of?(Array)
          result[i.to_s.gsub('@', '')] = value.map(&:to_json)
        else
          if i == :@project

          else
            result[i.to_s.gsub('@', '')] = value
          end
        end
      end

      result.merge!({id: self.id})
      result.with_indifferent_access
    end

    @@instance_count = 0

    def id
      @id ||= @@instance_count += 1
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
try_api-0.0.17 app/models/try_api/base.rb
try_api-0.0.16 app/models/try_api/base.rb
try_api-0.0.15 app/models/try_api/base.rb
try_api-0.0.14 app/models/try_api/base.rb
try_api-0.0.13 app/models/try_api/base.rb
try_api-0.0.12 app/models/try_api/base.rb
try_api-0.0.11 app/models/try_api/base.rb
try_api-0.0.10 app/models/try_api/base.rb
try_api-0.0.9 app/models/try_api/base.rb
try_api-0.0.8 app/models/try_api/base.rb
try_api-0.0.7 app/models/try_api/base.rb
try_api-0.0.6 app/models/try_api/base.rb