Class: Datasource::Serializer

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

Defined Under Namespace

Classes: TemplatePart

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*scopes) ⇒ Serializer

Returns a new instance of Serializer.



87
88
89
90
91
92
# File 'lib/datasource/serializer.rb', line 87

def initialize(*scopes)
  @scopes = scopes
  if @scopes.size != self.class.datasource_count
    fail ArgumentError, "#{self.class.name} needs #{self.class.datasource_count} scopes, you provided #{@scopes.size}"
  end
end

Class Attribute Details

.datasource_countObject

Returns the value of attribute datasource_count.



25
26
27
# File 'lib/datasource/serializer.rb', line 25

def datasource_count
  @datasource_count
end

.templateObject

Returns the value of attribute template.



25
26
27
# File 'lib/datasource/serializer.rb', line 25

def template
  @template
end

Class Method Details

.array(&block) ⇒ Object



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

def array(&block)
  with_new_cursor(:array, &block)
end

.attribute(name) ⇒ Object



81
82
83
84
# File 'lib/datasource/serializer.rb', line 81

def attribute(name)
  fail "No datasource selected - use \"select_datasource Klass\" first." unless template && @cursor.type == :datasource
  @cursor.select << name
end

.attributes(*attributes) ⇒ Object



77
78
79
# File 'lib/datasource/serializer.rb', line 77

def attributes(*attributes)
  attributes.each { |name| attribute name }
end

.datasource(ds) ⇒ Object



72
73
74
75
# File 'lib/datasource/serializer.rb', line 72

def datasource(ds)
  self.datasource_count += 1
  @cursor = with_new_cursor(:datasource, ds) { @cursor }
end

.hash(&block) ⇒ Object



56
57
58
# File 'lib/datasource/serializer.rb', line 56

def hash(&block)
  with_new_cursor(:hash, &block)
end

.inherited(base) ⇒ Object



27
28
29
30
# File 'lib/datasource/serializer.rb', line 27

def inherited(base)
  base.datasource_count = 0
  @cursor = base.template = nil
end

.key(name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/datasource/serializer.rb', line 60

def key(name)
  fail "Cannot use key outside hash." unless template && @cursor.type == :hash
  @cursor = @cursor.tap do
    @cursor = @cursor.value[name.to_s] = TemplatePart.new(@cursor)
    yield
  end
end

.with_new_cursor(type, value = nil, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/datasource/serializer.rb', line 32

def with_new_cursor(type, value = nil, &block)
  result = nil
  new_cursor = TemplatePart.new(@cursor, type, value)
  @cursor = @cursor.tap do
    if template.nil?
      self.template = @cursor = new_cursor
    elsif @cursor.type == :datasource
      @cursor = @cursor.parent
      return with_new_cursor(type, value, &block)
    elsif @cursor.type == :array
      @cursor = new_cursor
      @cursor.parent.push(@cursor)
    elsif @cursor.type.nil?
      # replace cursor
      @cursor.type = type
      @cursor.set_default_value(value)
    else
      fail "Invalid use of #{type}."
    end
    result = block.call
  end
  result
end

Instance Method Details

#as_jsonObject



94
95
96
# File 'lib/datasource/serializer.rb', line 94

def as_json
  parse_template_part(self.class.template)
end