Skip to content

Allow passing block to Data definition#358

Open
stephannv wants to merge 1 commit into
yippee-fun:mainfrom
stephannv:data_block_definition
Open

Allow passing block to Data definition#358
stephannv wants to merge 1 commit into
yippee-fun:mainfrom
stephannv:data_block_definition

Conversation

@stephannv

Copy link
Copy Markdown
Contributor

From Ruby docs:

The native Data ::define method accepts an optional block and evaluates it in the context of the newly defined class. That allows to define additional methods:

Measure = Data.define(:amount, :unit) do
  def <=>(other)
    return unless other.is_a?(self.class) && other.unit == unit
    amount <=> other.amount
  end

  include Comparable
end

This will be useful for Literal when it is necessary to define custom properties/methods inside Literal::Data.define, eg:

Success = Literal::Data.define(user: User) do
  def success? = true
  def failure? = false
end

Failure = Literal::Data.define(reason: String, user: User) do
  def success? = false
  def failure? = true
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant