settings_on_rails 0.2.2 → 0.3.0

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: b3d2546e49211ff3f4772301ea5d8ddad7760e57
4
- data.tar.gz: 03eb8027cd870ae60d475a1972d15f328df244b2
3
+ metadata.gz: 0b83c9ecc5fb868616dcd1708d3f5b1c6efec936
4
+ data.tar.gz: 8a1e64c5f65a24383fab856200b48d4acc0bdb67
5
5
  SHA512:
6
- metadata.gz: 5b3381b4f813100a6ed5e9adc50de7ec2b165308f6a8738551d679414663d98c84998e27b77d5878fcce28787691212a5616a51079b6e0548adf6f163088ca71
7
- data.tar.gz: ebaba09d122cdb86617146c60e08b6ebd974bbf9057d0497e4d476bd27a4877c946861a699d67a30d11120ec437604b558edaf2be605b5980bb4bbb14d63089e
6
+ metadata.gz: 0331b4b06617abc42b1b30bb5ad595bfc3db21b121ff8b795a57e32750c6e29e6b2499a79b7dc41d3d9266befbdca74e0788e7f7eb8d0663578606f3f6e24b29
7
+ data.tar.gz: c91c95f9240c83ec946573063658cc3137f80b31af6444818dfa1ecdb5eb317f588363fa01589730ff369992193faf77c9e2ca3f94b33cefa45ff025f403f80d
data/Gemfile CHANGED
@@ -4,4 +4,4 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'coveralls', require: false
7
- gem 'byebug'
7
+
data/README.md CHANGED
@@ -35,7 +35,7 @@ end
35
35
 
36
36
  Set settings
37
37
  ```ruby
38
- @blog.settings.title = 'My Blog'
38
+ @blog.settings.title = 'My Space'
39
39
  @blog.settings(:theme).background_color = 'blue'
40
40
 
41
41
  @blog.save
@@ -10,7 +10,14 @@ module SettingsOnRails
10
10
  define_method method_name do |*keys|
11
11
  column = Configuration.check!(self)
12
12
 
13
- Settings.new(keys, self, column, method_name)
13
+ settings = Settings.new(keys, self, column, method_name)
14
+ node = settings._current_node
15
+
16
+ if node.nil? || node.is_a?(Hash)
17
+ settings
18
+ else
19
+ node
20
+ end
14
21
  end
15
22
 
16
23
  has_settings(&block)
@@ -7,12 +7,14 @@ module SettingsOnRails
7
7
  # @param [ActiveRecord] klass, the Model who has settings
8
8
  # @param [Symbol] column, the column to store settings
9
9
  def self.init(klass, column)
10
- raise ArgumentError.new("has_settings_on: symbol expected, but got a #{column.class}") unless column.is_a?(Symbol)
10
+ unless column.is_a?(Symbol) || column.is_a?(String)
11
+ raise ArgumentError.new("has_settings_on: symbol or string expected, but got a #{column.class}")
12
+ end
11
13
 
12
14
  klass.class_eval do
13
15
  class_attribute Configuration::NAME_COLUMN, Configuration::DEFAULTS_COLUMN
14
16
 
15
- serialize column, Hash
17
+ serialize column, HashWithIndifferentAccess
16
18
  Configuration::init_defaults_column(self)
17
19
  Configuration::init_name_column(self, column)
18
20
  end
@@ -37,7 +39,7 @@ module SettingsOnRails
37
39
  # init to Hash {} for data attribute in klass if nil
38
40
  def self.init_defaults_column(klass)
39
41
  defaults = klass.send(Configuration::DEFAULTS_COLUMN)
40
- klass.send(Configuration::DEFAULTS_COLUMN.to_s + '=', {}) unless defaults
42
+ klass.send(Configuration::DEFAULTS_COLUMN.to_s + '=', ActiveSupport::HashWithIndifferentAccess.new) unless defaults
41
43
  end
42
44
 
43
45
  def self.init_name_column(klass, column_name)
@@ -25,7 +25,9 @@ module SettingsOnRails
25
25
  options = keys.extract_options!
26
26
  raise ArgumentError.new("has_settings: Option :defaults expected, but got #{options.keys.join(', ')}") unless options.blank? || (options.keys == [:defaults])
27
27
  keys.each do |key_name|
28
- raise ArgumentError.new("has_settings: symbol expected, but got a #{key_name.class}") unless key_name.is_a?(Symbol)
28
+ unless key_name.is_a?(Symbol) || key_name.is_a?(String)
29
+ raise ArgumentError.new("has_settings: symbol or string expected, but got a #{key_name.class}")
30
+ end
29
31
  end
30
32
 
31
33
  options[:defaults].each do |k, v|
@@ -38,7 +40,9 @@ module SettingsOnRails
38
40
  # @param [Symbol] value
39
41
  # @param [Hash] options, options with a default Hash
40
42
  def attr(value, options = {})
41
- raise ArgumentError.new("has_settings: symbol expected, but got a #{value.class}") unless value.is_a?(Symbol)
43
+ unless value.is_a?(Symbol) || value.is_a?(String)
44
+ raise ArgumentError.new("has_settings: symbol expected, but got a #{value.class}")
45
+ end
42
46
  raise ArgumentError.new("has_settings: Option :default expected, but got #{options.keys.join(', ')}") unless options.blank? || (options.keys == [:default])
43
47
 
44
48
  default_value = options[:default]
@@ -6,7 +6,9 @@ module SettingsOnRails
6
6
  # Thus we can differentiate settings(:key).attr and settings(:key, :attr)
7
7
  def initialize(keys, target_obj, target_column_name, parent)
8
8
  keys.each do |key_name|
9
- raise ArgumentError.new("has_settings: symbol expected, but got a #{key_name.class}") unless key_name.is_a?(Symbol)
9
+ unless (key_name.is_a?(Symbol) || key_name.is_a?(String))
10
+ raise ArgumentError.new("has_settings: symbol or string expected, but got a #{key_name.class}")
11
+ end
10
12
  end
11
13
  @keys = keys
12
14
  @target_obj = target_obj
@@ -21,6 +21,11 @@ module SettingsOnRails
21
21
  end
22
22
 
23
23
  def method_missing(method_name, *args, &block)
24
+ if block_given?
25
+ node = current_node || {}
26
+ return node.send(method_name, *args, &block)
27
+ end
28
+
24
29
  if method_name.to_s =~ REGEX_SETTER && args.size == 1
25
30
  _set_value($1, args.first)
26
31
  elsif method_name.to_s =~ REGEX_GETTER && args.size == 0
@@ -31,12 +36,23 @@ module SettingsOnRails
31
36
  end
32
37
  end
33
38
 
39
+ # Just a wrapper of current_node
40
+ def _current_node
41
+ current_node
42
+ end
43
+
34
44
  private
35
45
 
36
46
  def _settings(*keys)
37
47
  raise ArgumentError, 'wrong number of arguments (0 for 1..n)' if keys.size == 0
48
+ settings = Settings.new(keys, @target_object, @column_name, @method_name, self)
49
+ node = settings._current_node
38
50
 
39
- Settings.new(keys, @target_object, @column_name, @method_name, self)
51
+ if node.nil? || node.is_a?(Hash)
52
+ settings
53
+ else
54
+ node
55
+ end
40
56
  end
41
57
 
42
58
  def _get_value(name)
@@ -1,3 +1,3 @@
1
1
  module SettingsOnRails
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: settings_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ALLEN WANG QIANG
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  requirements: []
127
127
  rubyforge_project:
128
- rubygems_version: 2.4.6
128
+ rubygems_version: 2.4.5
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Model specific Hash Preferences/Settings for Rails.