Class: Datasource::Serializer::Composite
- Inherits:
-
Object
- Object
- Datasource::Serializer::Composite
- Defined in:
- lib/datasource/serializer/composite.rb
Defined Under Namespace
Classes: TemplatePart
Class Attribute Summary collapse
-
.datasource_count ⇒ Object
Returns the value of attribute datasource_count.
-
.template ⇒ Object
Returns the value of attribute template.
Class Method Summary collapse
- .array(&block) ⇒ Object
- .attribute(name) ⇒ Object
- .attributes(*attributes) ⇒ Object
- .datasource(ds) ⇒ Object
- .hash(&block) ⇒ Object
- .inherited(base) ⇒ Object
- .key(name) ⇒ Object
- .with_new_cursor(type, value = nil, &block) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(*scopes) ⇒ Composite
constructor
A new instance of Composite.
Constructor Details
#initialize(*scopes) ⇒ Composite
Returns a new instance of Composite.
88 89 90 91 92 93 |
# File 'lib/datasource/serializer/composite.rb', line 88 def initialize(*scopes) @scopes = scopes if @scopes.size != self.class.datasource_count fail ArgumentError, "#{self.class.name} needs #{@scopes.size} scopes, you provided #{self.class.datasource_count}" end end |
Class Attribute Details
.datasource_count ⇒ Object
Returns the value of attribute datasource_count.
26 27 28 |
# File 'lib/datasource/serializer/composite.rb', line 26 def datasource_count @datasource_count end |
.template ⇒ Object
Returns the value of attribute template.
26 27 28 |
# File 'lib/datasource/serializer/composite.rb', line 26 def template @template end |
Class Method Details
.array(&block) ⇒ Object
69 70 71 |
# File 'lib/datasource/serializer/composite.rb', line 69 def array(&block) with_new_cursor(:array, &block) end |
.attribute(name) ⇒ Object
82 83 84 85 |
# File 'lib/datasource/serializer/composite.rb', line 82 def attribute(name) fail "No datasource selected - use \"select_datasource Klass\" first." unless @cursor.type == :datasource @cursor.select << name end |
.attributes(*attributes) ⇒ Object
78 79 80 |
# File 'lib/datasource/serializer/composite.rb', line 78 def attributes(*attributes) attributes.each { |name| attribute name } end |
.datasource(ds) ⇒ Object
73 74 75 76 |
# File 'lib/datasource/serializer/composite.rb', line 73 def datasource(ds) self.datasource_count += 1 @cursor = with_new_cursor(:datasource, ds) { @cursor } end |
.hash(&block) ⇒ Object
57 58 59 |
# File 'lib/datasource/serializer/composite.rb', line 57 def hash(&block) with_new_cursor(:hash, &block) end |
.inherited(base) ⇒ Object
28 29 30 31 |
# File 'lib/datasource/serializer/composite.rb', line 28 def inherited(base) base.datasource_count = 0 @cursor = base.template = nil end |
.key(name) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/datasource/serializer/composite.rb', line 61 def key(name) fail "Cannot use key outside hash." unless @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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/datasource/serializer/composite.rb', line 33 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_json ⇒ Object
95 96 97 |
# File 'lib/datasource/serializer/composite.rb', line 95 def as_json parse_template_part(self.class.template) end |