lib/yaks/mapper/config.rb in yaks-0.3.1 vs lib/yaks/mapper/config.rb in yaks-0.4.0.rc1
- old
+ new
@@ -1,79 +1,64 @@
module Yaks
class Mapper
class Config
- include Equalizer.new(:attributes)
+ include Equalizer.new(:type, :attributes, :links, :associations)
- def initialize(attributes = Hamster.list, links = Hamster.list, associations = Hamster.list, profile = nil)
+ attr_reader :links, :associations
+
+ def initialize(type, attributes, links, associations)
+ @type = type
@attributes = attributes
@links = links
@associations = associations
- @profile = profile
- freeze
end
- def new(updates)
+ def updated(updates)
self.class.new(
+ updates.fetch(:type) { type },
updates.fetch(:attributes) { attributes },
updates.fetch(:links) { links },
- updates.fetch(:associations) { associations },
- updates.fetch(:profile) { profile },
+ updates.fetch(:associations) { associations }
)
end
+ def type(type = Undefined)
+ return @type if type.equal?(Undefined)
+ updated(type: type)
+ end
+
def attributes(*attrs)
return @attributes if attrs.empty?
- new(
- attributes: @attributes + attrs.to_list
+ updated(
+ attributes: @attributes + attrs
)
end
def link(rel, template, options = {})
- new(
- links: @links.cons(Link.new(rel, template, options))
+ updated(
+ links: @links + [Link.new(rel, template, options)]
)
end
- def profile(type = Undefined)
- return @profile if type == Undefined
- new(
- profile: type
- )
- end
-
- # key
- # embed_style
- # rel
- # (profile)
-
def has_one(name, options = {})
add_association(HasOne, name, options)
end
def has_many(name, options = {})
add_association(HasMany, name, options)
end
- def add_association(type, name, options = {})
- new(
- associations: @associations.cons(
+ def add_association(type, name, options)
+ updated(
+ associations: @associations + [
type.new(
name,
- options.fetch(:as) { name },
- options.fetch(:mapper) { Undefined },
- options.fetch(:links) { Yaks::List() },
- options.reject {|k,v| [:as, :mapper, :links].include?(k) }
+ options.fetch(:mapper) { Undefined },
+ options.fetch(:rel) { Undefined },
+ options.fetch(:collection_mapper) { Undefined },
)
- )
+ ]
)
- end
-
- def links
- @links
- end
-
- def associations
- @associations
end
end
end
end