Module: ActiveRecord::Acts::Optionable::IntanceMethods
- Defined in:
- lib/acts_as_optionable/acts_as_optionable.rb
Instance Method Summary collapse
-
#delete_option(name) ⇒ Object
Delete a stored option.
-
#get_option(name) ⇒ Object
Get a stored option, or fall back to a default option.
-
#options_and_defaults ⇒ Object
Return a hash of options and defaults.
-
#options_and_defaults_hash ⇒ Object
Return a pure hash filled from the options.
-
#options_values_struct ⇒ Object
Returns an instance of options where option names are callable as methods.
-
#set_option(name, value, opts = {}) ⇒ Object
Store an option persistently and override default option.
Instance Method Details
#delete_option(name) ⇒ Object
Delete a stored option.
35 36 37 38 39 40 41 |
# File 'lib/acts_as_optionable/acts_as_optionable.rb', line 35 def delete_option(name) if option = (:reload).find_by_name(name.to_s) option = option.destroy (:reload) option end end |
#get_option(name) ⇒ Object
Get a stored option, or fall back to a default option
29 30 31 32 |
# File 'lib/acts_as_optionable/acts_as_optionable.rb', line 29 def get_option(name) get_stored_option(name) || get_default_option(name) end |
#options_and_defaults ⇒ Object
Return a hash of options and defaults. Values in hash are Option instances
44 45 46 |
# File 'lib/acts_as_optionable/acts_as_optionable.rb', line 44 def .merge() end |
#options_and_defaults_hash ⇒ Object
Return a pure hash filled from the options. Use this if you want access to the data as a hash without interfacing with Option methods
50 51 52 53 54 55 |
# File 'lib/acts_as_optionable/acts_as_optionable.rb', line 50 def .inject({}) do |memo, option| memo[option[0]] = option[1].to_h memo end end |
#options_values_struct ⇒ Object
Returns an instance of options where option names are callable as methods
Example:
# Where foo is 'FOO' & bar is 'BAR'
= foo.
.foo => "FOO"
. => "BAR"
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/acts_as_optionable/acts_as_optionable.rb', line 64 def = {} .each do |name, option| opt_key = name.to_s [opt_key] = option.value ["#{opt_key}_kind"] = option.kind ["#{opt_key}_display_name"] = option.display_name end OpenStruct.new() end |
#set_option(name, value, opts = {}) ⇒ Object
Store an option persistently and override default option
19 20 21 22 23 24 25 26 |
# File 'lib/acts_as_optionable/acts_as_optionable.rb', line 19 def set_option(name, value, opts = {}) option = get_stored_option(name) || .build(:name => name.to_s, :value => value, :kind => opts[:kind], :display_name => opts[:display_name], :category => opts[:category], :position => opts[:position]) return if !opts[:force_set] && new_option_matches_current?(option, value) option.value = value ret = option.save! (:reload) ret end |