1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
require 'test_helper'
# Include Inherit Module And Decorator Test
class SchemaTest < Minitest::Spec
module Genre
include Representable
property :genre
end
module LinkFeature
def self.included(base)
base.extend(Link)
end
module Link
def link
end
end
end
module Module
include Representable::Hash
feature LinkFeature
property :title
property :label do # extend: LabelModule
property :name
link # feature
property :location do
property :city
link # feature.
end
end
property :album, :extend => lambda { raise "don't manifest me!" } # this is not an inline decorator, don't manifest it.
include Genre # Schema::Included::included is called!
end
class WithLocationStreetRepresenter < Representable::Decorator
include Representable::Hash
feature LinkFeature
property :title
property :label do # extend: LabelModule
property :name
link # feature
property :location do
property :city
link # feature.
end
end
end
describe "3-level deep with features" do
let(:label) { OpenStruct.new(:name => "Epitaph", :location => OpenStruct.new(:city => "Sanfran", :name => "DON'T SHOW ME!")) }
# Module does correctly include features in inlines.
it { band.extend(Module).to_hash.must_equal({"label"=>{"name"=>"Epitaph", "location"=>{"city"=>"Sanfran"}}, "genre"=>"Punkrock"}) }
# Decorator does correctly include features in inlines.
it { Decorator.new(band).to_hash.must_equal({"label"=>{"name"=>"Epitaph", "location"=>{"city"=>"Sanfran"}}, "genre"=>"Punkrock"}) }
end
class Decorator < Representable::Decorator
feature Representable::Hash
include Module
end
# puts Decorator.representable_attrs[:definitions].inspect
let(:label) { OpenStruct.new(:name => "Fat Wreck", :city => "San Francisco", :employees => [OpenStruct.new(:name => "Mike")], :location => OpenStruct.new(:city => "Sanfran")) }
let(:band) { OpenStruct.new(:genre => "Punkrock", :label => label) }
# it { FlatlinersDecorator.new( OpenStruct.new(label: OpenStruct.new) ).
# to_hash.must_equal({}) }
it do
Decorator.new(band).to_hash.must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "location"=>{"city"=>"Sanfran"}}})
end
class InheritDecorator < Representable::Decorator
include Representable::Hash
include Module
property :label, inherit: true do # decorator.rb:27:in `initialize': superclass must be a Class (Module given)
property :city
property :location, :inherit => true do
property :city
end
end
end
it do
InheritDecorator.new(band).to_hash.must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "city"=>"San Francisco", "location"=>{"city"=>"Sanfran"}}})
end
class InheritFromDecorator < InheritDecorator
property :label, inherit: true do
collection :employees do
property :name
end
end
end
it do
InheritFromDecorator.new(band).to_hash.must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "city"=>"San Francisco", "employees"=>[{"name"=>"Mike"}], "location"=>{"city"=>"Sanfran"}}})
end
end
class ApplyTest < Minitest::Spec
class AlbumDecorator < Representable::Decorator
include Representable::Hash
property :title
property :hit do
property :title
end
collection :songs do
property :title
end
property :band do # yepp, people do crazy stuff like that.
property :label do
property :name
end
end
end
end
|