Class: Hypa::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/hypa.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Collection

Returns a new instance of Collection.



63
64
65
66
# File 'lib/hypa.rb', line 63

def initialize(&block)
  @actions = []
  block.call(self) if block_given?
end

Instance Method Details

#action(&block) ⇒ Object



72
73
74
# File 'lib/hypa.rb', line 72

def action(&block)
  @actions << Action.new(&block)
end

#render(data) ⇒ Object

TODO: Refactoring



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hypa.rb', line 81

def render(data)
  attributes = @schema.attributes.map { |a| a.name }

  items = data.map do |d|
    item = {}
    attributes.each { |a| item[a] = d[a] }
    item
  end
  
  self.to_hash.merge(items: items)
end

#schema(&block) ⇒ Object



68
69
70
# File 'lib/hypa.rb', line 68

def schema(&block)
  @schema = AttributeSet.new(&block)
end

#to_hashObject



76
77
78
# File 'lib/hypa.rb', line 76

def to_hash
  { schema: @schema.to_hash, actions: @actions.map { |a| a.to_hash } }
end