muesli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b816fa770232aa2466afb9c0dcd6ed54249da728
4
+ data.tar.gz: 72cbf469228b2a2f342368fe3214f135a2136c33
5
+ SHA512:
6
+ metadata.gz: 979a7a099df75a1b489ba8393761f942532262e0695535e56320045f225b798cf5ce819c7abe2a5f04e7cc0a444108047e4fc90fb3845f8bcc92e8b2a2387672
7
+ data.tar.gz: 02854ccde3af338061a836971a34199f67ffe36bebefce21239bd1076cbda83fd65761417cd28759e19826ab19ff29eea4c5ab63feb036a3340611bffe2c7e5c
@@ -0,0 +1,52 @@
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
@@ -0,0 +1,26 @@
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
data/lib/muesli.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'muesli/base_serializer'
2
+ require 'muesli/cancan'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: muesli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Connor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Provides serialization of models into hashes with attribute whitelisting
14
+ and authorization for passing to views or as an API response.
15
+ email: danconn@danconnor.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/muesli.rb
21
+ - lib/muesli/base_serializer.rb
22
+ - lib/muesli/cancan.rb
23
+ homepage: https://github.com/onyxrev/muesli
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.2.2
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Provides serialization of models into hashes with attribute whitelisting
47
+ and authorization for passing to views or as an API response.
48
+ test_files: []