settings_lord 1.0.1 → 1.0.2

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.
data/README.md CHANGED
@@ -12,6 +12,10 @@ add to your Gemfile:
12
12
 
13
13
  gem 'settings_lord'
14
14
 
15
+ after this execute in console:
16
+
17
+ rails g settings_lord
18
+
15
19
  ##Overview
16
20
  You can create settings via class methods:
17
21
 
@@ -1,6 +1,7 @@
1
1
  module SettingsLord
2
2
 
3
3
  ACTIVE_RECORD_COLUMNS = ['name','value','klass','parent_id']
4
+ RESERVED_REFLECTOR_WORDS = [:_name,:_klass,:_new_value,:_reflect_like_namespace,:_parent,:reflect]
4
5
 
5
6
  mattr_accessor :setting_creator
6
7
  mattr_accessor :meta_settings
@@ -19,12 +19,12 @@ class SettingsLord::MetaSettingCollection
19
19
  @klasses_and_namespaces = {}
20
20
  end
21
21
 
22
- def add(meta_option)
23
- return false unless meta_option.is_a? SettingsLord::MetaSetting
22
+ def add(meta_setting)
23
+ return false unless meta_setting.is_a? SettingsLord::MetaSetting
24
24
 
25
- remove_if_exists(meta_option)
26
- fill_klasses_and_namespaces_table(meta_option)
27
- self.collection << meta_option
25
+ remove_if_exists(meta_setting)
26
+ fill_klasses_and_namespaces_table(meta_setting)
27
+ self.collection << meta_setting
28
28
  end
29
29
 
30
30
  def has_klass?(klass_name)
@@ -36,10 +36,10 @@ class SettingsLord::MetaSettingCollection
36
36
  end
37
37
 
38
38
  def set_by(reflection)
39
- reflection.name = remove_set_tag(reflection.name)
39
+ reflection._name = remove_set_tag(reflection._name)
40
40
 
41
41
  if meta_option = find_by_reflection(reflection)
42
- meta_option.update_value(reflection.new_value)
42
+ meta_option.update_value(reflection._new_value)
43
43
  else
44
44
  return nil
45
45
  end
@@ -51,23 +51,23 @@ class SettingsLord::MetaSettingCollection
51
51
 
52
52
  def find_by_reflection(reflection)
53
53
  # we can't search without klass and name
54
- return nil if reflection.klass.blank? || reflection.name.blank?
54
+ return nil if reflection._klass.blank? || reflection._name.blank?
55
55
 
56
56
  # check klass
57
- return nil unless has_klass?(reflection.klass)
57
+ return nil unless has_klass?(reflection._klass)
58
58
 
59
59
  # check namespace if needed
60
- if reflection.reflect_like_namespace
61
- return nil unless klass_has_namespace?(reflection.klass, reflection.parent)
60
+ if reflection._reflect_like_namespace
61
+ return nil unless klass_has_namespace?(reflection._klass, reflection._parent)
62
62
  end
63
63
 
64
- if reflection.reflect_like_namespace
64
+ if reflection._reflect_like_namespace
65
65
  result = @collection.select do |entry|
66
- entry.klass == reflection.klass and entry.name == reflection.name and entry.parent == reflection.parent
66
+ entry.klass == reflection._klass and entry.name == reflection._name and entry.parent == reflection._parent
67
67
  end
68
68
  else
69
69
  result = @collection.select do |entry|
70
- entry.klass == reflection.klass and entry.name == reflection.name
70
+ entry.klass == reflection._klass and entry.name == reflection._name
71
71
  end
72
72
  end
73
73
 
@@ -1,12 +1,25 @@
1
1
  class SettingsLord::Reflector
2
2
  # reflector hold klass/namespace information and reflect on name
3
3
 
4
- attr_accessor :name,:klass,:new_value,:reflect_like_namespace,:parent
4
+ attr_accessor :_name,:_klass,:_new_value,:_reflect_like_namespace,:_parent
5
5
 
6
6
  def initialize(*args)
7
7
  setup_instance_variables!(args)
8
8
  end
9
9
 
10
+ # search for proper MetaOption in MetaOptionCollection and get/set needed value
11
+ def reflect
12
+ should_search = @meta.has_klass?(@_klass) or @meta.klass_has_namespace?(@_klass,@_name)
13
+ return nil unless should_search
14
+
15
+ if is_getter?
16
+ return create_sub_reflection! if should_create_sub_reflection?
17
+ return @meta.get_by(self)
18
+ else
19
+ return @meta.set_by(self)
20
+ end
21
+ end
22
+
10
23
  # method missing will be called only when we return Reflector object
11
24
  # this is default case when user attempt to nested option (option with namespace)
12
25
  # for example:
@@ -22,8 +35,8 @@ class SettingsLord::Reflector
22
35
  # when we return Reflector object this object already get klass and namespace information
23
36
  # we only need to setup called method name and some extra arguments
24
37
  def method_missing(called_name,*args,&block)
25
- @name = called_name.to_sym
26
- @new_value = args.first
38
+ @_name = called_name.to_sym
39
+ @_new_value = args.first
27
40
 
28
41
  result = self.reflect
29
42
 
@@ -38,54 +51,33 @@ class SettingsLord::Reflector
38
51
  end
39
52
  end
40
53
 
41
- # search for proper MetaOption in MetaOptionCollection and get/set needed value
42
- def reflect()
43
- should_search = @meta.has_klass?(@klass) or @meta.klass_has_namespace?(@klass,@name)
44
- return nil unless should_search
54
+ private
45
55
 
46
- if is_getter?
47
- return create_sub_reflection if should_create_sub_reflection?
48
- return @meta.get_by(self)
49
- else
50
- return @meta.set_by(self)
51
- end
56
+ # @name/@_parent/@klass should always be represented as Symbol
57
+ def setup_instance_variables!(args)
58
+ args = args.extract_options!
59
+ @_name = args[:name].to_sym
60
+ @_new_value = args[:new_value]
61
+ @_klass = args[:klass]
62
+ @_klass = args[:klass].model_name.underscore.to_sym if @_klass.is_a? Class
63
+ @_reflect_like_namespace = args[:reflect_like_namespace] || false
64
+ @meta = SettingsLord.meta_settings
52
65
  end
53
66
 
54
- def should_create_sub_reflection?
55
- @reflect_like_namespace == false and @meta.klass_has_namespace?(@klass,@name)
67
+ def is_getter?
68
+ not @_name.to_s.end_with?('=')
56
69
  end
57
70
 
58
- def create_sub_reflection
71
+ def create_sub_reflection!
59
72
  reflection = self.dup
60
- reflection.reflect_like_namespace = true
61
- reflection.parent = reflection.name.to_sym
62
- reflection.name = nil
73
+ reflection._reflect_like_namespace = true
74
+ reflection._parent = reflection._name.to_sym
75
+ reflection._name = nil
63
76
  return reflection
64
77
  end
65
78
 
66
- def is_getter?
67
- not @name.to_s.end_with?('=')
68
- end
69
-
70
- def default_value_called?
71
- !!@name.to_s.match(/[a-zA-Z0-9]_default_value/)
72
- end
73
-
74
- def remove_default_value_tag_from_string
75
- @name.to_s.gsub(/_default_value/,'').to_sym
76
- end
77
-
78
- private
79
-
80
- # @name/@parent/@klass should always be represented as Symbol
81
- def setup_instance_variables!(args)
82
- args = args.extract_options!
83
- @name = args[:name].to_sym
84
- @new_value = args[:new_value]
85
- @klass = args[:klass]
86
- @klass = args[:klass].model_name.underscore.to_sym if @klass.is_a? Class
87
- @reflect_like_namespace = args[:reflect_like_namespace] || false
88
- @meta = SettingsLord.meta_settings
79
+ def should_create_sub_reflection?
80
+ @_reflect_like_namespace == false and @meta.klass_has_namespace?(@_klass,@_name)
89
81
  end
90
82
 
91
83
  end
@@ -27,6 +27,8 @@ class SettingsLord::SettingCreator
27
27
  options.assert_valid_keys(@meta::VALID_KEYS)
28
28
  options = check_and_maintain_options(options)
29
29
 
30
+ check_reserved_words(name)
31
+
30
32
  options[:klass] = @klass # option should know class
31
33
  options[:parent] = @parent.name.to_sym if @parent
32
34
  options[:name] = name
@@ -46,6 +48,14 @@ class SettingsLord::SettingCreator
46
48
  return options
47
49
  end
48
50
 
51
+ def check_reserved_words(name)
52
+ SettingsLord::RESERVED_REFLECTOR_WORDS.each do |word|
53
+ if name == word
54
+ raise Exception, "'#{word}' is reserved by SettingsLord."
55
+ end
56
+ end
57
+ end
58
+
49
59
  def check_bool_flags(options)
50
60
  @meta::BOOL_KEYS.each do |key|
51
61
  if options[key] and not @meta::BOOL_CLASSES.include?(options[key].class)
@@ -3,7 +3,7 @@ module SettingsLord
3
3
 
4
4
  MAJOR = 1
5
5
  MINOR = 0
6
- PATCH = 1
6
+ PATCH = 2
7
7
 
8
8
  STRING = [MAJOR,MINOR,PATCH].compact.join('.')
9
9
 
@@ -7,6 +7,14 @@ class SettingsLordTest < ActiveSupport::TestCase
7
7
  SettingsLord.meta_settings.instance_variable_set :@collection, []
8
8
  end
9
9
 
10
+ test "should not accept reserved reflector word" do
11
+ assert_raise Exception do
12
+ Setting.settings do
13
+ _name
14
+ end
15
+ end
16
+ end
17
+
10
18
  test "should accept only boolean values" do
11
19
  assert_nothing_raised do
12
20
  Setting.settings do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: settings_lord
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
5
+ version: 1.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - pechrorin_andrey
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-30 00:00:00 +04:00
13
+ date: 2011-07-20 00:00:00 +04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16