Class: ActsAsConfiguration::Configuration
- Inherits:
-
Object
- Object
- ActsAsConfiguration::Configuration
- Defined in:
- lib/acts_as_configuration.rb
Overview
The object how controll the data
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
-
#method_missing(m, *args, &block) ⇒ Object
The “magic” happen here.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/acts_as_configuration.rb', line 11 def initialize(={}) @model = [:model] @column_name = [:column_name].to_s @columns = [:columns], { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] } @value = get_defaults_values raise "#{@column_name} should by text or string not #{[:model].column_for_attribute(@column_name.to_sym).type}" if not [:text, :stiring].include? [:model].column_for_attribute(@column_name.to_sym).type out = YAML.parse(@model[@column_name].to_s) if out == false db_value = nil else db_value = out.to_ruby end @value.merge! db_value if db_value.kind_of? Hash initialize_values # Raise or not if fail?... @model.attributes[@column_name] = @value @model.save(:validate => false) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
The “magic” happen here. Use the undefined method as a column
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/acts_as_configuration.rb', line 49 def method_missing(m, *args, &block) # If the method don't finish in "=" is fore read if m !~ /\=$/ self[m.to_s] # but if finish with "=" is for wirte else column_name = m.to_s.gsub(/\=$/, '') self[column_name.to_s] = args.first end end |
Instance Method Details
#[](key) ⇒ Object
34 35 36 |
# File 'lib/acts_as_configuration.rb', line 34 def [](key) @value[key.to_s] end |
#[]=(key, value) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/acts_as_configuration.rb', line 38 def []=(key, value) if (@columns[key.to_sym][:type] == :boolean and (not [true.class, false.class].include? value.class)) or ((not [:boolean, nil].include?(@columns[key.to_sym][:type])) and @columns[key.to_sym][:type] != value.class ) raise "#{value.inspect} is not a valid type, expected #{@columns[key.to_sym][:type]}" end @value[key.to_s] = value @model.send :"#{@column_name}=", @value.to_yaml end |