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 Undefine .double in mock Jan 11, 2016
test Undefine .double in mock Jan 11, 2016
.gitignore Gardening Jan 11, 2016
.travis.yml Remove jruby-head from Travis config Jan 11, 2016
Gemfile Release 1.0.0 Mar 20, 2015
LICENSE Release 1.0.0 Mar 20, 2015
README.md Gardening Jan 11, 2016
Rakefile Release 1.0.0 Mar 20, 2015
structure.gemspec Release 1.0.1 Jan 11, 2016

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.