opt_struct 0.3.0 → 0.4.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 +46 -1
- data/lib/opt_struct.rb +8 -4
- data/lib/opt_struct/class_methods.rb +7 -5
- data/opt_struct.gemspec +1 -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: 20cc3d259d8f9a17a7804fa4022e01862483a8b8
         | 
| 4 | 
            +
              data.tar.gz: 92344e5ac8ec8d8abd8a4512fed58584c67041ff
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 349c78d63e1f56eaa801de15ad3294de2e2877c5345997e68568b5a4c985ffcbaee11abf0b01584a5d5a81bc022aa48e8a87e20bf6ca343c1858f37ac6f3ff1e
         | 
| 7 | 
            +
              data.tar.gz: d2d65670ac1c8a1f4e0a09a0ad1836a67190ad6d81d06b5a50613c4921b49431436cc24f1aac7eec69503bad4db5ac2497a203df4f9837342e131a6d402eb19f
         | 
    
        data/README.md
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            # The Opt Struct
         | 
| 2 2 |  | 
| 3 | 
            -
            A struct around a hash
         | 
| 3 | 
            +
            A struct around a hash. Great for encapsulating actions with complex configuration, like interactor/action classes.
         | 
| 4 4 |  | 
| 5 5 | 
             
            ```ruby
         | 
| 6 6 | 
             
            gem "opt_struct"
         | 
| @@ -19,6 +19,9 @@ MyClass.new | |
| 19 19 | 
             
            MyClass.new "foo", "bar"
         | 
| 20 20 | 
             
            # => #<MyClass>
         | 
| 21 21 |  | 
| 22 | 
            +
            MyClass.new foo: "foo", bar: "bar"
         | 
| 23 | 
            +
            # => #<MyClass>
         | 
| 24 | 
            +
             | 
| 22 25 | 
             
            i = MyClass.new "foo", "bar", yin: "yang"
         | 
| 23 26 | 
             
            i.options
         | 
| 24 27 | 
             
            # => {yin: "yang"}
         | 
| @@ -85,3 +88,45 @@ i = MyClass.new("something", bar: "foo") | |
| 85 88 | 
             
            [i.foo, i.bar]
         | 
| 86 89 | 
             
            # => ["something", "foo"]
         | 
| 87 90 | 
             
            ```
         | 
| 91 | 
            +
             | 
| 92 | 
            +
            ## Example 5
         | 
| 93 | 
            +
             | 
| 94 | 
            +
            Both `build` and `new` accept a block.
         | 
| 95 | 
            +
             | 
| 96 | 
            +
            ```ruby
         | 
| 97 | 
            +
            PersonClass = OptStruct.new do
         | 
| 98 | 
            +
              required :first_name
         | 
| 99 | 
            +
              option :last_name
         | 
| 100 | 
            +
             | 
| 101 | 
            +
              def name
         | 
| 102 | 
            +
                [first_name, last_name].compact.join(" ")
         | 
| 103 | 
            +
              end
         | 
| 104 | 
            +
            end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            t = PersonClass.new(first_name: "Trish")
         | 
| 107 | 
            +
            # => #<PersonClass>
         | 
| 108 | 
            +
            t.name
         | 
| 109 | 
            +
            # => "Trish"
         | 
| 110 | 
            +
            t.last_name = "Smith"
         | 
| 111 | 
            +
            t.name
         | 
| 112 | 
            +
            # => "Trish Smith"
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            class CarClass
         | 
| 115 | 
            +
              include OptStruct.build do
         | 
| 116 | 
            +
                required :make, :model
         | 
| 117 | 
            +
                options :year, transmission: "Automatic"
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                def name
         | 
| 120 | 
            +
                  [year, make, model].compact.join(" ")
         | 
| 121 | 
            +
                end
         | 
| 122 | 
            +
              end
         | 
| 123 | 
            +
            end
         | 
| 124 | 
            +
             | 
| 125 | 
            +
            c = CarClass.new(make: "Infiniti", model: "G37", year: 2012)
         | 
| 126 | 
            +
            c.name
         | 
| 127 | 
            +
            # => "2012 Infinit G37"
         | 
| 128 | 
            +
             | 
| 129 | 
            +
            c = CarClass.new(model: "WRX", make: "Subaru")
         | 
| 130 | 
            +
            c.name
         | 
| 131 | 
            +
            # => "Subaru WRX"
         | 
| 132 | 
            +
            ```
         | 
    
        data/lib/opt_struct.rb
    CHANGED
    
    | @@ -16,28 +16,32 @@ module OptStruct | |
| 16 16 | 
             
                inject_struct(klass)
         | 
| 17 17 | 
             
              end
         | 
| 18 18 |  | 
| 19 | 
            -
              def self.new(*args, **defaults)
         | 
| 19 | 
            +
              def self.new(*args, **defaults, &callback)
         | 
| 20 20 | 
             
                check_for_invalid_args(args)
         | 
| 21 21 | 
             
                args.map!(&:to_sym)
         | 
| 22 | 
            -
                inject_struct(Class.new) do
         | 
| 22 | 
            +
                klass = inject_struct(Class.new) do
         | 
| 23 23 | 
             
                  expect_arguments *args
         | 
| 24 24 | 
             
                  options defaults
         | 
| 25 25 | 
             
                end
         | 
| 26 | 
            +
                klass.class_exec(&callback) if callback
         | 
| 27 | 
            +
                klass
         | 
| 26 28 | 
             
              end
         | 
| 27 29 |  | 
| 28 | 
            -
              def self.build(*args, **defaults)
         | 
| 30 | 
            +
              def self.build(*args, **defaults, &callback)
         | 
| 29 31 | 
             
                check_for_invalid_args(args)
         | 
| 30 32 | 
             
                args.map!(&:to_sym)
         | 
| 31 33 | 
             
                Module.new do
         | 
| 32 34 | 
             
                  @arguments = args
         | 
| 33 35 | 
             
                  @defaults = defaults
         | 
| 36 | 
            +
                  @callback = callback
         | 
| 34 37 |  | 
| 35 38 | 
             
                  def self.included(klass)
         | 
| 36 | 
            -
                    arguments, defaults = @arguments, @defaults
         | 
| 39 | 
            +
                    arguments, defaults, callback = @arguments, @defaults, @callback
         | 
| 37 40 | 
             
                    OptStruct.inject_struct(klass) do
         | 
| 38 41 | 
             
                      expect_arguments *arguments
         | 
| 39 42 | 
             
                      options defaults
         | 
| 40 43 | 
             
                    end
         | 
| 44 | 
            +
                    klass.class_exec(&callback) if callback
         | 
| 41 45 | 
             
                  end
         | 
| 42 46 | 
             
                end
         | 
| 43 47 | 
             
              end
         | 
| @@ -67,16 +67,18 @@ module OptStruct | |
| 67 67 | 
             
                def expect_arguments(*arguments)
         | 
| 68 68 | 
             
                  @expected_arguments = arguments
         | 
| 69 69 | 
             
                  attr_accessor *arguments
         | 
| 70 | 
            -
                   | 
| 70 | 
            +
                  assignment_lines = String.new
         | 
| 71 71 | 
             
                  arguments.each_with_index do |arg, i|
         | 
| 72 | 
            -
                     | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 72 | 
            +
                    assignment_lines << <<~RUBY
         | 
| 73 | 
            +
                      @#{arg} = @options.delete(:#{arg}) if @options.key?(:#{arg})
         | 
| 74 | 
            +
                      @#{arg} = values[#{i}] if values.length > #{i}
         | 
| 75 | 
            +
                      raise ArgumentError, "missing required argument: #{arg}" unless defined?(@#{arg})
         | 
| 76 | 
            +
                    RUBY
         | 
| 75 77 | 
             
                  end
         | 
| 76 78 | 
             
                  self.class_eval <<~RUBY
         | 
| 77 79 | 
             
                    def initialize(*values, **options)
         | 
| 78 80 | 
             
                      @options = self.class.defaults.merge(options)
         | 
| 79 | 
            -
                      #{ | 
| 81 | 
            +
                      #{assignment_lines}
         | 
| 80 82 | 
             
                      check_required_keys
         | 
| 81 83 | 
             
                    end
         | 
| 82 84 | 
             
                  RUBY
         | 
    
        data/opt_struct.gemspec
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: opt_struct
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.4.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Carl Zulauf
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017-05- | 
| 11 | 
            +
            date: 2017-05-29 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |