muesli 0.0.1 → 0.0.2

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: b816fa770232aa2466afb9c0dcd6ed54249da728
4
- data.tar.gz: 72cbf469228b2a2f342368fe3214f135a2136c33
3
+ metadata.gz: b5207efed312b06072f78b5df9076b88d2e939ed
4
+ data.tar.gz: 2a7d581ca91a43f62830a37d10f1104231d66c9d
5
5
  SHA512:
6
- metadata.gz: 979a7a099df75a1b489ba8393761f942532262e0695535e56320045f225b798cf5ce819c7abe2a5f04e7cc0a444108047e4fc90fb3845f8bcc92e8b2a2387672
7
- data.tar.gz: 02854ccde3af338061a836971a34199f67ffe36bebefce21239bd1076cbda83fd65761417cd28759e19826ab19ff29eea4c5ab63feb036a3340611bffe2c7e5c
6
+ metadata.gz: 3c38e9e896d58770795d967257f907a6361cd56620cc95a41944151c1e44073e82805f754e2ab4663c1697f403bb4c7dd20a7c7d15419d7bcbbbfc8968c49cb2
7
+ data.tar.gz: e29e41ca99af7f301ca6abdc3bbdbf66d4f4f699ce5c194ff57e90b62913e2b343bb2da2e50dfb0da50bb38e70da5de4cfd5d5fd2ffb6e110b1cd9bad49d45d4
@@ -0,0 +1,28 @@
1
+ module Muesli
2
+ module Adapters
3
+ module CanCan
4
+ attr_accessor :ability
5
+
6
+ def for_user(user)
7
+ self.user = user
8
+ self.ability = Ability.new(user)
9
+
10
+ return self
11
+ end
12
+
13
+ protected
14
+
15
+ def can?(*args)
16
+ return false unless user
17
+
18
+ ability.can? *args
19
+ end
20
+
21
+ def cannot?(*args)
22
+ return true unless user
23
+
24
+ ability.cannot *args
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module Muesli
2
+ module AttributeSerializers
3
+ class Base
4
+ def initialize(attribute_value)
5
+ @value = attribute_value
6
+ end
7
+
8
+ def serialize
9
+ @value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Muesli
2
+ module AttributeSerializers
3
+ class DateTime < Time
4
+ def initialize
5
+ super
6
+
7
+ @value = @value.to_time
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Muesli
2
+ module AttributeSerializers
3
+ class DateTime < Time
4
+ def initialize
5
+ super
6
+
7
+ @value = @value.to_time
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Muesli
2
+ module AttributeSerializers
3
+ module Moped
4
+ module BSON
5
+ class ObjectId < Muesli::AttributeSerializers::Base
6
+ def serialize
7
+ return @value.to_s
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Muesli
2
+ module AttributeSerializers
3
+ class Time < Base
4
+ def serialize
5
+ return @attribute_value.iso8601
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module Muesli
2
+ module Serializers
3
+ class Base
4
+ class << self
5
+ attr_accessor :whitelisted_attributes
6
+ end
7
+
8
+ attr_accessor :model, :user
9
+
10
+ def initialize(model)
11
+ self.model = model
12
+ end
13
+
14
+ def serialize
15
+ return serialize_from_model(self.class.whitelisted_attributes || [])
16
+ end
17
+
18
+ protected
19
+
20
+ def serialize_from_model(attribute_list)
21
+ serialize_attributes(model, attribute_list)
22
+ end
23
+
24
+ def serialize_attributes(attribute_source, attribute_list)
25
+ return attribute_list.reduce({}) do |memo, attr|
26
+ memo[attr] = serialize_value( attribute_source.send(attr) )
27
+ memo
28
+ end
29
+ end
30
+
31
+ def serialize_value(value)
32
+ if Muesli::AttributeSerializers.const_defined?(value.class.to_s, false)
33
+ return Muesli::AttributeSerializers.const_get(value.class.to_s, false).new(value).serialize
34
+ else
35
+ return Muesli::AttributeSerializers::Base.new(value).serialize
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/muesli.rb CHANGED
@@ -1,2 +1,7 @@
1
- require 'muesli/base_serializer'
2
- require 'muesli/cancan'
1
+ require 'muesli/attribute_serializers/base'
2
+ require 'muesli/attribute_serializers/time'
3
+ require 'muesli/attribute_serializers/date'
4
+ require 'muesli/attribute_serializers/date_time'
5
+ require 'muesli/attribute_serializers/moped_bson_objectid'
6
+ require 'muesli/serializers/base'
7
+ require 'muesli/adapters/cancan'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: muesli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Connor
@@ -18,8 +18,13 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/muesli.rb
21
- - lib/muesli/base_serializer.rb
22
- - lib/muesli/cancan.rb
21
+ - lib/muesli/adapters/cancan.rb
22
+ - lib/muesli/attribute_serializers/base.rb
23
+ - lib/muesli/attribute_serializers/date.rb
24
+ - lib/muesli/attribute_serializers/date_time.rb
25
+ - lib/muesli/attribute_serializers/moped_bson_objectid.rb
26
+ - lib/muesli/attribute_serializers/time.rb
27
+ - lib/muesli/serializers/base.rb
23
28
  homepage: https://github.com/onyxrev/muesli
24
29
  licenses:
25
30
  - MIT
@@ -1,52 +0,0 @@
1
- module Muesli
2
- class BaseSerializer
3
- @whitelisted_attributes = []
4
-
5
- class << self
6
- attr_accessor :whitelisted_attributes
7
- end
8
-
9
- attr_accessor :model, :user
10
-
11
- def initialize(model)
12
- self.model = model
13
- end
14
-
15
- def to_hash
16
- return serialize_attributes(self.class.whitelisted_attributes)
17
- end
18
-
19
- protected
20
-
21
- def serialize_attributes(attribute_list)
22
- return attribute_list.reduce({}) do |memo, attr|
23
- memo[attr] = serialize_value( model.send(attr) )
24
- memo
25
- end
26
- end
27
-
28
- def serialize_value(value)
29
- if [ Date, DateTime, Time ].include? value.class
30
- return serialize_date(value)
31
- end
32
-
33
- return value
34
- end
35
-
36
- def normalize_dates_to_time(date)
37
- return date.to_time if date.is_a? Date
38
- return date.to_time if date.is_a? DateTime
39
- return date if date.is_a? Time
40
-
41
- return nil
42
- end
43
-
44
- def serialize_date(date)
45
- date = normalize_dates_to_time(date)
46
-
47
- return nil unless date
48
-
49
- return date.iso8601
50
- end
51
- end
52
- end
data/lib/muesli/cancan.rb DELETED
@@ -1,26 +0,0 @@
1
- module Muesli
2
- module CanCan
3
- attr_accessor :ability
4
-
5
- def for_user(user)
6
- self.user = user
7
- self.ability = Ability.new(user)
8
-
9
- return self
10
- end
11
-
12
- protected
13
-
14
- def can?(*args)
15
- return false unless user
16
-
17
- ability.can? *args
18
- end
19
-
20
- def cannot?(*args)
21
- return true unless user
22
-
23
- ability.cannot *args
24
- end
25
- end
26
- end