lib/nstore.rb in nstore-0.2.1 vs lib/nstore.rb in nstore-0.3.0

- old
+ new

@@ -14,12 +14,12 @@ # jira: { # board: [:id, :name, user: %i[id name]] # }, # trello: %i[id name] # }, -# prefix: false, -# stringify: false +# prefix: false, +# stringify: true # ... module NStore class Error < StandardError; end def self.included(klass) @@ -27,28 +27,29 @@ end # List of Class methods going to be included above module ClassMethods def nstore(attribute, options) - prefix = options.fetch(:prefix, false) - stringify = options.fetch(:stringify, false) accessors = options[:accessors] + prefix = options.fetch(:prefix, false) + stringify = options.fetch(:stringify, true) flat_accessors = [] deep_flatten(accessors, [], flat_accessors) + attribute = attribute.to_s if stringify _nstore_generate_accessors(attribute, flat_accessors, prefix, stringify) end def _nstore_generate_accessors(attribute, flat_accessors, prefix, stringify) flat_accessors.each do |keys| + keys.map!(&:to_s) if stringify + define_method("#{prefix ? "#{attribute}_" : ''}#{keys.join('_')}=".to_sym) do |value| - keys.map!(&:to_s) if stringify write_nstore_attribute(attribute, keys, value) end define_method("#{prefix ? "#{attribute}_" : ''}#{keys.join('_')}".to_sym) do - keys.map!(&:to_s) if stringify read_nstore_attribute(attribute, keys) end end end @@ -94,7 +95,11 @@ position[keys[-1]] = value end def read_nstore_attribute(attribute, keys) send(attribute).send(:dig, *keys) + end + + def nstore_key_brakets(attribute, keys) + ([attribute] + keys).map { |k| "['#{k}']" }.join end end