Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Hash composition over inheritance #230

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/dependency.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require "./ext/yaml"

module Shards
class Dependency < Hash(String, String)
class Dependency
property name : String

@store = {} of String => String
forward_missing_to @store

def self.new(pull : YAML::PullParser) : self
Dependency.new(pull.read_scalar).tap do |dependency|
pull.each_in_mapping do
Expand All @@ -13,26 +16,24 @@ module Shards
end

def initialize(@name)
super()
end

# DEPRECATED: with no replacement
def initialize(@name, config)
super()
config.each { |k, v| self[k.to_s] = v.to_s }
config.each { |k, v| @store[k.to_s] = v.to_s }
end

def version
fetch("version", "*")
@store.fetch("version", "*")
end

def refs
self["branch"]? || self["tag"]? || self["commit"]?
@store["branch"]? || @store["tag"]? || @store["commit"]?
end

def inspect(io)
io << "#<" << self.class.name << " {" << name << " => "
super
@store.inspect(io)
io << "}>"
end
end
Expand Down
8 changes: 5 additions & 3 deletions src/target.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module Shards
class Target < Hash(String, String)
class Target
property name : String

@store = {} of String => String
forward_missing_to @store

def self.new(pull : YAML::PullParser) : self
Target.new(pull.read_scalar).tap do |target|
pull.each_in_mapping do
Expand All @@ -11,11 +14,10 @@ module Shards
end

def initialize(@name)
super()
end

def main
self["main"]
@store["main"]
end
end
end