Getting started: attributes.

Super super basic collection of attributes. Nothing fancy.
This commit is contained in:
Steve Klabnik 2014-07-09 16:16:39 -04:00
parent a45b5eeda3
commit 729a823868
9 changed files with 68 additions and 13 deletions

View File

@ -3,6 +3,7 @@ require "bundler/gem_tasks"
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/*_test.rb']
t.ruby_opts = ['-r./test/test_helper.rb']
t.verbose = true

View File

@ -1,11 +1,11 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'active_model_serializers/version'
require 'active_model/serializer/version'
Gem::Specification.new do |spec|
spec.name = "active_model_serializers"
spec.version = ActiveModelSerializers::VERSION
spec.version = ActiveModel::Serializer::VERSION
spec.authors = ["Steve Klabnik"]
spec.email = ["steve@steveklabnik.com"]
spec.summary = %q{Conventions-based JSON generation for Rails.}

View File

@ -0,0 +1,21 @@
module ActiveModel
class Serializer
class << self
attr_accessor :_attributes
end
def self.inherited(base)
base._attributes = []
end
def self.attributes(*attrs)
@_attributes.concat attrs
end
attr_accessor :object
def initialize(object)
@object = object
end
end
end

View File

@ -0,0 +1,5 @@
module ActiveModel
class Serializer
VERSION = "0.9.0"
end
end

View File

@ -1,5 +1,5 @@
require "active_model_serializers/version"
require "active_model"
require "active_model/serializer/version"
require "active_model/serializer"
module ActiveModelSerializers
# Your code goes here...
end

View File

@ -1,3 +0,0 @@
module ActiveModelSerializers
VERSION = "0.9.0"
end

18
test/attributes_test.rb Normal file
View File

@ -0,0 +1,18 @@
require 'test_helper'
module ActiveModel
class Serializer
class AttributesTest < Minitest::Test
def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile_serializer = ProfileSerializer.new(@profile)
end
def test_attributes_definition
assert_equal([:name, :description],
@profile_serializer.class._attributes)
end
end
end
end

12
test/fixtures/poro.rb vendored Normal file
View File

@ -0,0 +1,12 @@
class Model
def initialize(hash={})
@attributes = hash
end
end
class Profile < Model
end
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description
end

View File

@ -1,8 +1,9 @@
require "bundler/setup"
require "active_model_serializers"
require "active_support/json"
require 'rails'
require "active_support/json"
require 'minitest/autorun'
require "active_model_serializers"
require 'fixtures/poro'