setting_accessors 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ed332f105a50d26ae337675c092e529bf9ce7d0
4
- data.tar.gz: edb4613293773b8bfa27dd9402beaecc2a6dbbd7
3
+ metadata.gz: d0b13cae5880dc3e448d1d26d64ff6bce9149d81
4
+ data.tar.gz: 7b56e7cec309297d20edfb0a90a346c028c98ad9
5
5
  SHA512:
6
- metadata.gz: d87bfe17b531e82471548437ce5affd9a593fe38d32cca6592eeb0ead32ac5974f195cedce90b5923c02d8ca53f932d0770d8215f3107bc07a98efe989e4e140
7
- data.tar.gz: d024b156d3ddc3fa7c94a6895765ebee666b0b9893d890be0fa203c5a49e209997a8ae3987f67073fc64088be50df201d74062727bcc2100c7f372ad82c82370
6
+ metadata.gz: 062e4ad9c0fb336a109aae63f9987715dbfce2d9ac5b2b607a8a4a5f339e0e64a020e98606e76cdb2fdcb615de895a42843307dd50d988165d18faf6919430e3
7
+ data.tar.gz: e140c13822008a93b770ece981ea8be7b75d1b155df28bb5d1fe5f39d6477f7f1cc9fe63f170e081030a79405b3f59cc1bc4917b501bfb40d834c9ba1e2b4c65
@@ -13,7 +13,7 @@ class SettingAccessors::Accessor
13
13
  # Gets a setting's value
14
14
  #
15
15
  def [](key)
16
- @temp_settings[key.to_sym] || SettingAccessors.setting_class.get(key, @record)
16
+ has_key?(key) ? @temp_settings[key.to_sym] : SettingAccessors.setting_class.get(key, @record)
17
17
  end
18
18
 
19
19
  def has_key?(key)
@@ -49,6 +49,9 @@ module SettingAccessors::Integration
49
49
  #create multi-param fields in forms.
50
50
  self.columns_hash[setting_name.to_s] = OpenStruct.new(type: SettingAccessors::Internal.setting_value_type(setting_name, self.new).to_sym)
51
51
 
52
+ #Add the setting's name to the list of setting_accessors for this class
53
+ SettingAccessors::Internal.add_setting_accessor_name(self, setting_name)
54
+
52
55
  #Getter
53
56
  define_method(setting_name) do
54
57
  settings.get_with_fallback(setting_name, fallback)
@@ -76,6 +79,14 @@ module SettingAccessors::Integration
76
79
  end
77
80
  end
78
81
 
82
+ def as_json(*args)
83
+ json = super(*args)
84
+ SettingAccessors::Internal.setting_accessor_names(self.class).each do |setting_name|
85
+ json[setting_name.to_s] = send(setting_name)
86
+ end
87
+ json
88
+ end
89
+
79
90
  def settings
80
91
  @settings_accessor ||= SettingAccessors::Accessor.new(self)
81
92
  end
@@ -69,7 +69,6 @@ module SettingAccessors
69
69
  end
70
70
  end
71
71
 
72
-
73
72
  #
74
73
  # @return [Hash] configuration data regarding this setting
75
74
  #
@@ -104,5 +103,24 @@ module SettingAccessors
104
103
  def self.get_class_setting(klass, setting_name)
105
104
  self.lookup_nested_hash(@@class_settings, klass.to_s, setting_name.to_s)
106
105
  end
106
+
107
+ #
108
+ # Adds the given setting name to the list of used setting accessors
109
+ # in the given class.
110
+ # This is mainly to keep track of all accessors defined in the different classes
111
+ #
112
+ def self.add_setting_accessor_name(klass, setting_name)
113
+ @@setting_accessor_names ||= {}
114
+ @@setting_accessor_names[klass.to_s] ||= []
115
+ @@setting_accessor_names[klass.to_s] << setting_name.to_s
116
+ end
117
+
118
+ #
119
+ # @return [Array<String>] all setting accessor names defined in the given +class+
120
+ #
121
+ def self.setting_accessor_names(klass)
122
+ self.lookup_nested_hash(@@setting_accessor_names, klass.to_s)
123
+ end
124
+
107
125
  end
108
126
  end
@@ -1,3 +1,3 @@
1
1
  module SettingAccessors
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -1,5 +1,20 @@
1
1
  class UserTest < ActiveSupport::TestCase
2
- # test "the truth" do
3
- # assert true
4
- # end
2
+ context 'JSON serialization' do
3
+ setup do
4
+ @user = User.new(:a_string => 'test', :a_number => 42, :a_boolean => false)
5
+ end
6
+
7
+ should 'include the setting accessors' do
8
+ SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
9
+ assert_includes @user.as_json.keys, setting_name
10
+ end
11
+ end
12
+
13
+ should 'contain the correct values' do
14
+ SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
15
+ assert_equal @user.as_json[setting_name.to_s], @user.send(setting_name)
16
+ end
17
+ end
18
+
19
+ end
5
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setting_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Exner