Module: Roar::Representer::JSON::CollectionJSON::ClassMethods

Includes:
SharedClassMethodsBullshit
Defined in:
lib/roar/representer/json/collection_json.rb

Defined Under Namespace

Modules: PropertyWithRenderNil, SharedClassMethodsBullshit

Instance Method Summary collapse

Methods included from SharedClassMethodsBullshit

#href, #representable_attrs

Instance Method Details

#items(options = {}, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/roar/representer/json/collection_json.rb', line 108

def items(options={}, &block)
  collection :items, { :extend => lambda {|*| representable_attrs.collection_representers[:items] } }.merge!(options)

  mod = representable_attrs.collection_representers[:items] = Module.new do
    include Roar::Representer::JSON
    include Roar::Representer::Feature::Hypermedia
    include Roar::Representer::JSON::CollectionJSON::DataMethods
    extend SharedClassMethodsBullshit

    module_exec(&block)

    # TODO: share with main module!
    property :__href, :as => :href
    def __href
      compile_links_for(representable_attrs.collection_representers[:href].link_configs).first.href
    end
    def __href=(v)
      @__href = Roar::Representer::Feature::Hypermedia::Hyperlink.new(:href => v)
    end
    def href
      @__href
    end
  end
end

#queries(&block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/roar/representer/json/collection_json.rb', line 94

def queries(&block)
  mod = representable_attrs.collection_representers[:queries] = Module.new do
    include Roar::Representer::JSON
    include Roar::Representer::Feature::Hypermedia
    
    module_exec(&block)

    def to_hash(*)
      hash = super
      hash["links"] # TODO: make it easier to render collection of links.
    end
  end
end

#template(&block) ⇒ Object

TODO: provide automatic copying from the ItemRepresenter here.



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
# File 'lib/roar/representer/json/collection_json.rb', line 63

def template(&block)
  mod = representable_attrs.collection_representers[:object_template] = Module.new do
    include Roar::Representer::JSON
    include Roar::Representer::JSON::CollectionJSON::DataMethods
    
    extend PropertyWithRenderNil

    module_exec(&block)

    #self.representation_wrap = :template
    def from_hash(hash, *args)  # overridden in :template representer.
      super(hash["template"])
    end
  end

  representable_attrs.collection_representers[:template] = Module.new do
    include Roar::Representer::JSON
    include mod

    #self.representation_wrap = false

    # DISCUSS: currently we skip real deserialization here and just store the :data hash.
    def from_hash(hash, *args)
      replace(hash["data"])
    end
  end
end

#template_representerObject



90
91
92
# File 'lib/roar/representer/json/collection_json.rb', line 90

def template_representer
  representable_attrs.collection_representers[:object_template]
end

#version(v) ⇒ Object



133
134
135
# File 'lib/roar/representer/json/collection_json.rb', line 133

def version(v)
  representable_attrs.collection_representers[:version] = v
end