azahara_schema 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4390fbc505e535b317928c28dae0a13d99ec7997
4
- data.tar.gz: 8fab75c50ea5623a6f29f812d6575a7e03177da4
3
+ metadata.gz: fce09b13b68a342872733bbcc7fe3b18907a62af
4
+ data.tar.gz: 67f2b89be27832d8881b9f19409991bd63801331
5
5
  SHA512:
6
- metadata.gz: e318854db84d703d99076e572909ccc09480b632f62bcc0f8306eb35e00371e803b1ae52b8be64f0b1a90bc0628802b614b2613d7e87744e00d5b6bc9245e483
7
- data.tar.gz: dde84cf9dc5573525fc7b2b2f740b162ba339fc8a686f1c906d3873e782af4b8e26b154df91a81ca9288e75cc6b2ad54af994e11e4eda0debb289a17a6dd07ea
6
+ metadata.gz: 4e38cac517db7b62ce49c2c02d94a886dc970d4765743cee6288901b06e007a81aad43feaa9a7b4307a41ae8bd16e8dbeb974515475807d10c1bf21b897b7561
7
+ data.tar.gz: 49677a40199bcbf7e699351ca131225348d48fe82a96a503ccbd0045c23f72362f55de3f4d35b792687fd55f17059f4f731accbeba1abd1356ac3ca8603c074c
data/README.md CHANGED
@@ -28,6 +28,9 @@ $ gem install azahara_schema
28
28
  ```
29
29
 
30
30
  ## Usage
31
+
32
+ Detailed developers documentation can be found at [Documentation](https://git.servis.justice.cz/libraries/azahara_schema/wikis/home).
33
+
31
34
  Default usage is only for Rails.
32
35
 
33
36
  You basically need just this:
@@ -0,0 +1,24 @@
1
+ # Author:: Ondřej Ezr (mailto:oezr@msp.justice.cz)
2
+ # Copyright:: Copyright (c) 2017 Ministry of Justice
3
+ # License:: Distributes under license Open Source Licence pro Veřejnou Správu a Neziskový Sektor v.1
4
+
5
+ module AzaharaSchema
6
+ class AggregationAttribute < Attribute
7
+
8
+ attr_reader :attribute
9
+
10
+ def initialize(model, attribute)
11
+ @attribute = attribute
12
+ super(model, 'sum:'+attribute.name, attribute.type)
13
+ end
14
+
15
+ def arel_field
16
+ attribute.arel_field.sum
17
+ end
18
+
19
+ def value(parent)
20
+ attribute.attribute.add_join( parent.send(attribute.association.name) ).sum(attribute.arel_field).to_f
21
+ end
22
+
23
+ end
24
+ end
@@ -51,6 +51,10 @@ module AzaharaSchema
51
51
  true
52
52
  end
53
53
 
54
+ def aggregable?
55
+ format.aggregable?
56
+ end
57
+
54
58
  def value(record)
55
59
  record.public_send(name)
56
60
  end
@@ -1,6 +1,7 @@
1
1
  require 'azahara_schema/field_format'
2
2
  require 'azahara_schema/attribute'
3
3
  require 'azahara_schema/association_attribute'
4
+ require 'azahara_schema/aggregation_attribute'
4
5
  require 'azahara_schema/attribute_name'
5
6
  require 'azahara_schema/outputs'
6
7
  require 'azahara_schema/output'
@@ -47,12 +47,20 @@ module AzaharaSchema
47
47
  def available_operators
48
48
  ['=']
49
49
  end
50
+
51
+ def aggregable?
52
+ false
53
+ end
50
54
  end
51
55
 
52
56
  class NumberFormat < Base
53
57
  def available_operators
54
58
  super.concat(['>=','<=', '><'])
55
59
  end
60
+
61
+ def aggregable?
62
+ true
63
+ end
56
64
  end
57
65
 
58
66
  class IntegerFormat < NumberFormat
@@ -177,7 +177,10 @@ module AzaharaSchema
177
177
  end
178
178
  available_associations.each do |asoc_schema|
179
179
  asoc_schema.available_attributes.each do |asoc_attribute|
180
- @available_attributes << AssociationAttribute.new(model, asoc_schema, asoc_attribute)
180
+ next if asoc_attribute.is_a?(AggregationAttribute)
181
+ added_attribute = AssociationAttribute.new(model, asoc_schema, asoc_attribute)
182
+ @available_attributes << added_attribute
183
+ @available_attributes << AggregationAttribute.new(model, added_attribute) if asoc_attribute.aggregable?
181
184
  end
182
185
  end
183
186
  end
@@ -206,10 +209,19 @@ module AzaharaSchema
206
209
  options
207
210
  end
208
211
 
212
+ def entity_as_json(entity, options=nil)
213
+ attr_hash = entity.as_json(options)
214
+ # TODO serializable_add_includes(options) do |association, records, opts|
215
+ columns.each do |col|
216
+ attr_hash[col.name] = col.available_values.detect{|l, v| v == attr_hash[col.name] }.try(:[], 0) if col.type == 'love'
217
+ attr_hash[col.name] = col.value(entity) if col.is_a?(AzaharaSchema::AggregationAttribute)
218
+ end
219
+ attr_hash
220
+ end
221
+
209
222
  def as_json(options={})
210
- res_ary = entities.as_json(build_json_options!(options))
211
- columns.each{|col| res_ary.each{|attr_hash| attr_hash[col.name] = col.available_values.detect{|l, v| v == attr_hash[col.name] }.try(:[], 0) } if col.type == 'love' }
212
- res_ary
223
+ build_json_options!(options)
224
+ entities.collect{|entity| entity_as_json(entity, options) }
213
225
  end
214
226
 
215
227
 
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azahara_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondřej Ezr
@@ -65,6 +65,7 @@ files:
65
65
  - config/locales/cs.yml
66
66
  - config/routes.rb
67
67
  - lib/azahara_schema.rb
68
+ - lib/azahara_schema/aggregation_attribute.rb
68
69
  - lib/azahara_schema/association_attribute.rb
69
70
  - lib/azahara_schema/attribute.rb
70
71
  - lib/azahara_schema/attribute_name.rb