Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Parses data into value objects
Ruby
Failed to load latest commit information.
lib Release 1.0.0
test Release 1.0.0
.gitignore Release 1.0.0
.travis.yml Release 1.0.0
Gemfile Release 1.0.0
LICENSE Release 1.0.0
README.md Release 1.0.0
Rakefile Release 1.0.0
structure.gemspec Release 1.0.0

README.md

Structure

Travis

Structure

Structure is a mixin that helps you write clean, immutable value objects when parsing data in Ruby.

My typical use case is when parsing XML documents—your mileage may vary.

Structure also pretty-inspects the data it stores, which really helps when working in the command line, and comes with a helper to mock when testing.

It should work seamlessly with the various ActiveModel mixins out there.

Usage

A contrived example:

class Name
  include Structure

  SEPARATOR = " "

  def initialize(full)
    @names = full.split(SEPARATOR)
  end

  attribute :first do
    @names.first
  end

  attribute :last do
    @names.last
  end

  attribute :middle do
    @names[1...-1].join(SEPARATOR)
  end

  def full
    [first, middle, last].join(SEPARATOR)
  end
end

name = Name.new("Johann Sebastian Bach")
name.first # => "Johann"
name # => #<Name first="Johann", middle="Sebastian", last="Bach">
name.attributes # => {"first"=>"Johann", "middle"=>"Sebastian", "last"=>"Bach"}

To mock when testing, use .double. This will cast the parser to an object that mimics the former's public interface but replaces the original parsing implementation with an initializer that populates the attributes with a hash.

require "structure/double"
name = Name.double.new(first: 'Johann', middle: "Sebastian", last: "Bach")
name.first # => "Johann"
Something went wrong with that request. Please try again.