Skip to content

Commit 729a823

Browse files
committed
Getting started: attributes.
Super super basic collection of attributes. Nothing fancy.
1 parent a45b5ee commit 729a823

File tree

9 files changed

+67
-12
lines changed

9 files changed

+67
-12
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require "bundler/gem_tasks"
33
require 'rake/testtask'
44

55
Rake::TestTask.new do |t|
6+
t.libs << "test"
67
t.test_files = FileList['test/*_test.rb']
78
t.ruby_opts = ['-r./test/test_helper.rb']
89
t.verbose = true

active_model_serializers.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# coding: utf-8
22
lib = File.expand_path('../lib', __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'active_model_serializers/version'
4+
require 'active_model/serializer/version'
55

66
Gem::Specification.new do |spec|
77
spec.name = "active_model_serializers"
8-
spec.version = ActiveModelSerializers::VERSION
8+
spec.version = ActiveModel::Serializer::VERSION
99
spec.authors = ["Steve Klabnik"]
1010
spec.email = ["[email protected]"]
1111
spec.summary = %q{Conventions-based JSON generation for Rails.}

lib/active_model/serializer.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module ActiveModel
2+
class Serializer
3+
class << self
4+
attr_accessor :_attributes
5+
end
6+
7+
def self.inherited(base)
8+
base._attributes = []
9+
end
10+
11+
def self.attributes(*attrs)
12+
@_attributes.concat attrs
13+
end
14+
15+
attr_accessor :object
16+
17+
def initialize(object)
18+
@object = object
19+
end
20+
end
21+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ActiveModel
2+
class Serializer
3+
VERSION = "0.9.0"
4+
end
5+
end

lib/active_model_serializers.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require "active_model_serializers/version"
1+
require "active_model"
2+
require "active_model/serializer/version"
3+
4+
require "active_model/serializer"
25

3-
module ActiveModelSerializers
4-
# Your code goes here...
5-
end

lib/active_model_serializers/version.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/attributes_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class AttributesTest < Minitest::Test
6+
def setup
7+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8+
@profile_serializer = ProfileSerializer.new(@profile)
9+
end
10+
11+
def test_attributes_definition
12+
assert_equal([:name, :description],
13+
@profile_serializer.class._attributes)
14+
end
15+
end
16+
end
17+
end
18+

test/fixtures/poro.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Model
2+
def initialize(hash={})
3+
@attributes = hash
4+
end
5+
end
6+
7+
class Profile < Model
8+
end
9+
10+
class ProfileSerializer < ActiveModel::Serializer
11+
attributes :name, :description
12+
end

test/test_helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
require "bundler/setup"
22

3-
require "active_model_serializers"
3+
require 'rails'
44
require "active_support/json"
5+
require 'minitest/autorun'
56

6-
require 'rails'
7+
require "active_model_serializers"
78

8-
require 'minitest/autorun'
9+
require 'fixtures/poro'

0 commit comments

Comments
 (0)