Module: BlockParty::Configuration::Conversion
- Included in:
- BlockParty::Configuration
- Defined in:
- lib/block_party/configuration/conversion.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#as_hash ⇒ Object
protected.
- #create_setter(setter) ⇒ Object
- #dump ⇒ Object
- #hash_representation ⇒ Object
- #invalidate_hash_representation! ⇒ Object
- #load_hash(source = {}) ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
Class Method Details
.included(base) ⇒ Object
8 9 10 |
# File 'lib/block_party/configuration/conversion.rb', line 8 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#as_hash ⇒ Object
protected
66 67 68 |
# File 'lib/block_party/configuration/conversion.rb', line 66 def as_hash @__hash_representation__ ||= hash_representation end |
#create_setter(setter) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/block_party/configuration/conversion.rb', line 89 def create_setter(setter) define_singleton_method setter do |value| invalidate_hash_representation! instance_variable_set :"@#{setter_to_getter(setter)}", value end end |
#dump ⇒ Object
56 57 58 |
# File 'lib/block_party/configuration/conversion.rb', line 56 def dump to_hash.to_json.to_s end |
#hash_representation ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/block_party/configuration/conversion.rb', line 70 def hash_representation unless empty? Hash[ instance_variables.map do |instance_variable_name| key = instance_variable_name.to_s.gsub('@', '').to_sym value = instance_variable_get(instance_variable_name) value = value.hash_representation if value.is_a?(Configuration) [key, value] end ] else {} end end |
#invalidate_hash_representation! ⇒ Object
85 86 87 |
# File 'lib/block_party/configuration/conversion.rb', line 85 def invalidate_hash_representation! @__hash_representation__ = nil end |
#load_hash(source = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/block_party/configuration/conversion.rb', line 39 def load_hash(source={}) source.each do |key, value| if value.is_a? Hash send :"#{key}=", self.class.from_hash(value) else send :"#{key}=", value end end self end |
#to_hash ⇒ Object
50 51 52 53 54 |
# File 'lib/block_party/configuration/conversion.rb', line 50 def to_hash hash_representation.keep_if do |key| settings.include? key end end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/block_party/configuration/conversion.rb', line 60 def to_s to_hash.inspect.to_s end |