Skip to content
Parses data into value objects
Ruby
Latest commit 42b4399 Jan 11, 2016 @hakanensari Release 1.0.1
[skip ci]
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Gemfile
LICENSE
README.md
Rakefile
structure.gemspec

README.md

Structure

Travis

Structure helps you encapsulate data parsing in an immutable value object.

See here for how I use it.

Usage

This is a contrived example:

class Name
  include Structure

  SEPARATOR = " "

  def initialize(data)
    @data = data
  end

  attribute :first do
    @data.first
  end

  attribute :last do
    @data.last
  end

  attribute :middle do
    @data[1...-1].join(SEPARATOR) if @data.size > 2
  end

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

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

When testing, use .double. This will cast the parser to an object that mocks the public interface.

require "structure/double"

name = Name.double.new(first: "Johann", middle: "Sebastian", last: "Bach")
# => #<Name first="Johann", middle="Sebastian", last="Bach">
name.first
# => "Johann"
name.full
# => "Johann Sebastian Bach"
name.attributes
# => {"first"=>"Johann", "middle"=>"Sebastian", "last"=>"Bach"}
Something went wrong with that request. Please try again.