selfies 0.5.0 → 1.0.0
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.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/kernel.rb +0 -5
- data/lib/selfies/self_init.rb +24 -18
- data/lib/selfies/version.rb +1 -1
- data/selfies.gemspec +2 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 2229c9e8739933e8b2f76dc485ff084a193d7b97
         | 
| 4 | 
            +
              data.tar.gz: 0cdae8225e70509672478da1e7cc65bf816a81be
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 6d754742137a6cf137c9f7100bd49e4b49c3ad5c1ac157d4f4ef8da34d333a44055461305cd7147915c82a0aade10e0481b8287819944ec347510592a7878dae
         | 
| 7 | 
            +
              data.tar.gz: b49bd26ddfaeaf4ddbf3f5aeccd656e2135c6003c07bf7dd13150c789294c41f22a48e2773ffd64c0b16c4dc3d1aa14449f8228c8e112e3390979ae5b3c10d6b
         | 
    
        data/README.md
    CHANGED
    
    | @@ -249,6 +249,9 @@ end | |
| 249 249 | 
             
            ***selfie:***
         | 
| 250 250 | 
             
            - Find a suitable syntax that would allow to 'selfie' an instance method that has arguments;
         | 
| 251 251 |  | 
| 252 | 
            +
            ***command line:***
         | 
| 253 | 
            +
            - Write a command that can initialize a whole class, from creating the file to stub the initial setup (idea by Luke Hill)
         | 
| 254 | 
            +
             | 
| 252 255 | 
             
            ***more?:***
         | 
| 253 256 | 
             
            - If you also often write repetitive bolierplate, and would like some code to get smaller, drop me a line and we might be able to add more macros.
         | 
| 254 257 |  | 
    
        data/lib/kernel.rb
    CHANGED
    
    | @@ -10,11 +10,6 @@ module Kernel | |
| 10 10 | 
             
                Selfies.generate_initializer(self, false, *variable_names)
         | 
| 11 11 | 
             
              end
         | 
| 12 12 |  | 
| 13 | 
            -
              def self_init(*variable_names)
         | 
| 14 | 
            -
                attr_reader_init(*variable_names)
         | 
| 15 | 
            -
              end
         | 
| 16 | 
            -
              deprecate :self_init, :attr_reader_init, 2017, 7
         | 
| 17 | 
            -
             | 
| 18 13 | 
             
              def selfie(*method_names)
         | 
| 19 14 | 
             
                Selfies.generate_class_methods(self, *method_names)
         | 
| 20 15 | 
             
              end
         | 
    
        data/lib/selfies/self_init.rb
    CHANGED
    
    | @@ -5,36 +5,35 @@ module Selfies | |
| 5 5 |  | 
| 6 6 | 
             
                  class_name.class_eval do
         | 
| 7 7 | 
             
                    variable_names = SelfInit.variable_names(variables)
         | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
                    else
         | 
| 11 | 
            -
                      attr_reader *variable_names
         | 
| 12 | 
            -
                    end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                    SelfInit.access_variables(class_name, accessor, variable_names)
         | 
| 13 10 |  | 
| 14 11 | 
             
                    define_method(:initialize) do |*args|
         | 
| 15 | 
            -
                       | 
| 16 | 
            -
                        raise ArgumentError, "wrong number of arguments (given #{args.count}, expected #{variable_names.count})"
         | 
| 17 | 
            -
                      end
         | 
| 12 | 
            +
                      argument_count(variables, variable_names.count, args.count)
         | 
| 18 13 |  | 
| 19 14 | 
             
                      variables.each_with_index do |variable, index|
         | 
| 20 15 | 
             
                        variable_name, default = decouple(variable)
         | 
| 21 | 
            -
                         | 
| 22 | 
            -
             | 
| 23 | 
            -
                        else
         | 
| 24 | 
            -
                          instance_variable_set("@#{variable_name}", args[index] || default)
         | 
| 25 | 
            -
                        end
         | 
| 16 | 
            +
                        value = variable_name == :args ? args[index..-1] : args[index]
         | 
| 17 | 
            +
                        instance_variable_set("@#{variable_name}", value || default)
         | 
| 26 18 | 
             
                      end
         | 
| 27 19 | 
             
                    end
         | 
| 28 20 |  | 
| 29 21 | 
             
                    private_class_method
         | 
| 30 22 |  | 
| 31 | 
            -
                    define_method(: | 
| 23 | 
            +
                    define_method(:argument_count) do |all_variables, expected, given|
         | 
| 24 | 
            +
                      unless correct_argument_count?(all_variables, expected, given)
         | 
| 25 | 
            +
                        raise ArgumentError,
         | 
| 26 | 
            +
                              "wrong number of arguments (given #{given}, expected #{expected})"
         | 
| 27 | 
            +
                      end
         | 
| 28 | 
            +
                    end
         | 
| 32 29 |  | 
| 30 | 
            +
                    define_method(:correct_argument_count?) do |all_variables, expected, given|
         | 
| 31 | 
            +
                      final_variable = all_variables.last
         | 
| 33 32 | 
             
                      correct_argument_count = given == expected
         | 
| 34 | 
            -
                      if  | 
| 33 | 
            +
                      if final_variable.is_a? Hash
         | 
| 35 34 | 
             
                        correct_argument_count ||= given == expected - 1
         | 
| 36 | 
            -
                      elsif  | 
| 37 | 
            -
                        at_least =  | 
| 35 | 
            +
                      elsif final_variable == :args
         | 
| 36 | 
            +
                        at_least = all_variables[0..all_variables.index(:args)].count
         | 
| 38 37 | 
             
                        correct_argument_count ||= given >= at_least
         | 
| 39 38 | 
             
                      end
         | 
| 40 39 |  | 
| @@ -42,7 +41,7 @@ module Selfies | |
| 42 41 | 
             
                    end
         | 
| 43 42 |  | 
| 44 43 | 
             
                    define_method(:decouple) do |variable|
         | 
| 45 | 
            -
                      return [variable, nil]  | 
| 44 | 
            +
                      return [variable, nil] unless variable.is_a? Hash
         | 
| 46 45 |  | 
| 47 46 | 
             
                      variable.keys + variable.values
         | 
| 48 47 | 
             
                    end
         | 
| @@ -51,6 +50,13 @@ module Selfies | |
| 51 50 |  | 
| 52 51 | 
             
                private_class_method
         | 
| 53 52 |  | 
| 53 | 
            +
                def self.access_variables(class_name, accessor, variable_names)
         | 
| 54 | 
            +
                  class_name.send(
         | 
| 55 | 
            +
                    (accessor ? :attr_accessor : :attr_reader),
         | 
| 56 | 
            +
                    *(variable_names)
         | 
| 57 | 
            +
                  )
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 54 60 | 
             
                def self.variable_names(variables)
         | 
| 55 61 | 
             
                  variables.collect do |variable|
         | 
| 56 62 | 
             
                    variable.is_a?(Hash) ? variable.keys.first : variable
         | 
    
        data/lib/selfies/version.rb
    CHANGED
    
    
    
        data/selfies.gemspec
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: selfies
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Mario D’Arco
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017- | 
| 11 | 
            +
            date: 2017-07-03 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |