Code Monkey home page Code Monkey logo

attribute_struct's Introduction

AttributeStruct

AttributeStruct is a DSL helper library. It provides support for programmatic generation of complex data structures.

How it works

Under the hood, AttributeStruct makes use of Ruby's BasicObject class and the #method_missing method to bring a clean and concise way of generating data structures. Deeply nested Hashes can be built using method chaining, block structures, or both. Evaluation of a structure is performed top down, allowing access to previously set data values within the structure during evaluation.

Examples

Setting values

Method chaining

require 'attribute_struct'

AttributeStruct.new do
  this.is.a.deeply.nested.hash true
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>true}}}}}}

Block nesting

require 'attribute_struct'

AttributeStruct.new do
  this do
    is do
      a do
        deeply do
          nested do
            hash true
          end
        end
      end
    end
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>true}}}}}}

Mixed block nesting and method chaining

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply.nested do
      hash true
    end
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>true}}}}}}

Structure re-entry

Block re-entry

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply.nested do
      hash true
    end
    deeply do
      nested.other_hash true
    end
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>true,"other_hash"=>true}}}}}}

Method re-entry

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply.nested do
      hash true
    end
  end
  this.is.a.deeply.nested.other_hash true
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>true,"other_hash"=>true}}}}}}

Data removal

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply.nested do
      hash true
    end
  end
  this.is.a.deeply.nested.other_hash true
  this.is.a.deeply.nested.delete!(:hash)
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"other_hash"=>true}}}}}}

Data Access

Current data

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply.nested do
      hash 'my_value'
    end
  end
  this.is.a.deeply.nested do
    other_hash data![:hash]
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>"my_value","other_hash"=>"my_value"}}}}}}

Current data keys

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply.nested do
      hash 'my_value'
    end
  end
  this.is.a.deeply.nested do
    other_hash keys!
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nested"=>{"hash"=>"my_value","other_hash"=>["hash"]}}}}}}

Hierarchy access

Parent structure

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply do
      state 'sunny'
      nested do
        hash parent!.state
      end
    end
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"state"=>"sunny", "nested"=>{"hash"=>"sunny"}}}}}}

Root structure

require 'attribute_struct'

AttributeStruct.new do
  this.is.a do
    deeply do
      state 'sunny'
      nested do
        hash root!.this.is.a.deeply.state
      end
    end
  end
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"state"=>"sunny", "nested"=>{"hash"=>"sunny"}}}}}}

Camel case

Camel case keys

require 'attribute_struct'

AttributeStruct.new do
  self._camel_keys = true
  this.is.a.deeply.nested_camel.hash true
end.dump!

# => {"This"=>{"Is"=>{"A"=>{"Deeply"=>{"NestedCamel"=>{"Hash"=>true}}}}}}

Camel case with lead lower

require 'attribute_struct'

AttributeStruct.new do
  self._camel_keys = true
  self._camel_style = :no_leading
  this.is.a.deeply.nested_camel.hash true
end.dump!

# => {"this"=>{"is"=>{"a"=>{"deeply"=>{"nestedCamel"=>{"hash"=>true}}}}}}

Disable camel on individual key

require 'attribute_struct'

AttributeStruct.new do
  self._camel_keys = true
  this.is.a.deeply.nested_camel.hash true
  this.set!('horse'.no_hump!, true)
end.dump!

# => {"This"=>{"Is"=>{"A"=>{"Deeply"=>{"NestedCamel"=>{"Hash"=>true}}}}},"horse" => true}

In the wild

Libraries utilizing AttributeStruct:

Information

attribute_struct's People

Contributors

chrisroberts avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.