mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Getting started: attributes.
Super super basic collection of attributes. Nothing fancy.
This commit is contained in:
parent
a45b5eeda3
commit
729a823868
1
Rakefile
1
Rakefile
@ -3,6 +3,7 @@ require "bundler/gem_tasks"
|
|||||||
require 'rake/testtask'
|
require 'rake/testtask'
|
||||||
|
|
||||||
Rake::TestTask.new do |t|
|
Rake::TestTask.new do |t|
|
||||||
|
t.libs << "test"
|
||||||
t.test_files = FileList['test/*_test.rb']
|
t.test_files = FileList['test/*_test.rb']
|
||||||
t.ruby_opts = ['-r./test/test_helper.rb']
|
t.ruby_opts = ['-r./test/test_helper.rb']
|
||||||
t.verbose = true
|
t.verbose = true
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
lib = File.expand_path('../lib', __FILE__)
|
lib = File.expand_path('../lib', __FILE__)
|
||||||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
||||||
require 'active_model_serializers/version'
|
require 'active_model/serializer/version'
|
||||||
|
|
||||||
Gem::Specification.new do |spec|
|
Gem::Specification.new do |spec|
|
||||||
spec.name = "active_model_serializers"
|
spec.name = "active_model_serializers"
|
||||||
spec.version = ActiveModelSerializers::VERSION
|
spec.version = ActiveModel::Serializer::VERSION
|
||||||
spec.authors = ["Steve Klabnik"]
|
spec.authors = ["Steve Klabnik"]
|
||||||
spec.email = ["steve@steveklabnik.com"]
|
spec.email = ["steve@steveklabnik.com"]
|
||||||
spec.summary = %q{Conventions-based JSON generation for Rails.}
|
spec.summary = %q{Conventions-based JSON generation for Rails.}
|
||||||
|
|||||||
21
lib/active_model/serializer.rb
Normal file
21
lib/active_model/serializer.rb
Normal 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
|
||||||
5
lib/active_model/serializer/version.rb
Normal file
5
lib/active_model/serializer/version.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
VERSION = "0.9.0"
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -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
|
|
||||||
|
|||||||
@ -1,3 +0,0 @@
|
|||||||
module ActiveModelSerializers
|
|
||||||
VERSION = "0.9.0"
|
|
||||||
end
|
|
||||||
18
test/attributes_test.rb
Normal file
18
test/attributes_test.rb
Normal 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
12
test/fixtures/poro.rb
vendored
Normal 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
|
||||||
@ -1,8 +1,9 @@
|
|||||||
require "bundler/setup"
|
require "bundler/setup"
|
||||||
|
|
||||||
require "active_model_serializers"
|
|
||||||
require "active_support/json"
|
|
||||||
|
|
||||||
require 'rails'
|
require 'rails'
|
||||||
|
require "active_support/json"
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
|
|
||||||
|
require "active_model_serializers"
|
||||||
|
|
||||||
|
require 'fixtures/poro'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user