Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(visitor): allow to create custom object (instead of Hash) according to context #299

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Commits on Nov 18, 2016

  1. feat(psych): allow to generate custom Hash object

    This is necessary to create custom object as mapping instead of Hash.
    For example, if you prefer `hashobj.key` instead of `hashobj['key']`:
    
        ## allows `h.foo` instead of `h['foo']`
        class MagicHash < Hash
          def method_missing(method, *args)
            return super unless args.empty?
            return self[method.to_s]
          end
        end
    
        ## override to generate MagicHash instead of Hash
        class MagicVisitor < Psych::Visitors::ToRuby
          def empty_mapping(o)
            MagicHash.new
          end
        end
    
        ## example to access `ydoc.foo` instead of `ydoc['foo']`
        input = <<'END'
        tables:
          - name: admin_users
            columns:
              - name:  id
                type:  int
                pkey:  true
        END
        tree = Psych.parse(input)
        visitor = MagicVisitor.create
        ydoc = visitor.accept(tree)
        p ydoc.tables[0].columns[0].name   #=> "name"
        p ydoc.tables[0].columns[0].type   #=> "int"
    kwatch committed Nov 18, 2016
    Configuration menu
    Copy the full SHA
    4213ebe View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    c9061f6 View commit details
    Browse the repository at this point in the history

Commits on Nov 19, 2016

  1. feat(psych): add hook point for mapping key and value

    This is necessary to generate custom object as mapping instead of Hash
    according to context. For example:
    
        TableObj  = Struct.new('TableObj', 'name', 'columns')
        ColumnObj = Struct.new('ColumnObj', 'name', 'type', 'pkey')
    
        class CustomVisitor < Psych::Visitors::ToRuby
          def initialize(*args)
            super
            @key_path = []   # ex: [] -> ['tables'] -> ['tables', 'columns']
          end
          def accept_key(k)    # push keys
            key = super k
            @key_path << key
            return key
          end
          def accept_value(v)  # pop keys
            value = super v
            @key_path.pop()
            return value
          end
          def empty_mapping(o)  # generate custom object instead of Hash
            case @key_path.last
            when 'tables'   ; return TableObj.new
            when 'columns'  ; return ColumnObj.new
            else            ; return super o
            end
          end
        end
    
        ## example to generate custom object according to context
        input = <<'END'
        tables:
          - name: admin_users
            columns:
              - name:  id
                type:  int
                pkey:  true
        END
        tree = Psych.parse(input)
        visitor = CustomVisitor.create
        ydoc = visitor.accept(tree)
        p ydoc['tables'][0].class                #=> Struct::TableObj
        p ydoc['tables'][0]['columns'][0].class  #=> Struct::ColumnObj
    kwatch committed Nov 19, 2016
    Configuration menu
    Copy the full SHA
    95eab41 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    080fd60 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    b14d98f View commit details
    Browse the repository at this point in the history
  4. feat(psych): define 'Psych::Visitors::CustomClassVisitor' class

    This new class allows user to generate custom object instead of Hash.
    See document of CustomClassVisitor class for details.
    kwatch committed Nov 19, 2016
    Configuration menu
    Copy the full SHA
    b5d3885 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    b81d44e View commit details
    Browse the repository at this point in the history
  6. feat(psych): add test script

    kwatch committed Nov 19, 2016
    Configuration menu
    Copy the full SHA
    34883b8 View commit details
    Browse the repository at this point in the history